1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2006 Andreas Persson * |
* Copyright (C) 2006-2009 Andreas Persson * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
18 |
* MA 02110-1301 USA * |
* MA 02110-1301 USA * |
19 |
***************************************************************************/ |
***************************************************************************/ |
20 |
|
|
21 |
#ifndef __SYNCHRONIZEDCONFIG_H__ |
#ifndef SYNCHRONIZEDCONFIG_H |
22 |
#define __SYNCHRONIZEDCONFIG_H__ |
#define SYNCHRONIZEDCONFIG_H |
23 |
|
|
24 |
#include "atomic.h" |
#include <set> |
25 |
|
#include <unistd.h> |
26 |
|
#include "lsatomic.h" |
27 |
|
|
28 |
namespace LinuxSampler { |
namespace LinuxSampler { |
29 |
|
|
30 |
/** |
/** |
31 |
* Thread safe management of configuration data, where the data is |
* Thread safe management of configuration data, where the data is |
32 |
* updated by a single non real time thread and read by a single |
* updated by a single non real time thread and read by a number |
33 |
* real time thread. |
* of real time threads. |
34 |
* |
* |
35 |
* The synchronization is achieved by using two instances of the |
* The synchronization is achieved by using two instances of the |
36 |
* configuration data. The non real time thread gets access to the |
* configuration data. The non real time thread gets access to the |
37 |
* instance not currently in use by the real time thread by |
* instance not currently in use by the real time threads by |
38 |
* calling GetConfigForUpdate(). After the data is updated, the |
* calling GetConfigForUpdate(). After the data is updated, the |
39 |
* non real time thread must call SwitchConfig() and redo the |
* non real time thread must call SwitchConfig() and redo the |
40 |
* update on the other instance. SwitchConfig() blocks until it is |
* update on the other instance. SwitchConfig() blocks until it is |
41 |
* safe to modify the other instance. |
* safe to modify the other instance. |
42 |
* |
* |
43 |
* The real time thread calls Lock() to get access to the data, |
* The real time threads need one Reader object each to access the |
44 |
* and Unlock() when it is finished reading the data. (Neither |
* configuration data. This object must be created outside the |
45 |
* Lock nor Unlock will block the real time thread, or use any |
* real time thread. The Lock() function returns a reference to |
46 |
* system calls.) |
* the data to be read, and Unlock() must be called when finished |
47 |
|
* reading the data. (Neither Lock nor Unlock will block the real |
48 |
|
* time thread, or use any system calls.) |
49 |
*/ |
*/ |
50 |
template<class T> |
template<class T> |
51 |
class SynchronizedConfig { |
class SynchronizedConfig { |
54 |
|
|
55 |
// methods for the real time thread |
// methods for the real time thread |
56 |
|
|
57 |
/** |
class Reader { |
58 |
* Gets the configuration object for use by the real time |
public: |
59 |
* thread. The object is safe to use (read only) until |
/** |
60 |
* Unlock() is called. |
* Gets the configuration object for use by the |
61 |
* |
* real time thread. The object is safe to use |
62 |
* @returns a reference to the configuration object to be |
* (read only) until Unlock() is called. |
63 |
* read by the real time thread |
* |
64 |
*/ |
* @returns a reference to the configuration |
65 |
const T& Lock() { |
* object to be read by the real time |
66 |
atomic_set(&lock, 1); |
* thread |
67 |
return config[atomic_read(&indexAtomic)]; |
*/ |
68 |
} |
const T& Lock() { |
69 |
|
lock.store(lockCount += 2, memory_order_relaxed); |
70 |
/** |
atomic_thread_fence(memory_order_seq_cst); |
71 |
* Unlock the configuration object. Unlock() must be |
return parent.config[parent.indexAtomic.load( |
72 |
* called by the real time thread after it has finished |
memory_order_acquire)]; |
73 |
* reading the configuration object. If the non real time |
} |
74 |
* thread is waiting in SwitchConfig() it will be awaken. |
|
75 |
*/ |
/** |
76 |
void Unlock() { |
* Unlock the configuration object. Unlock() must |
77 |
atomic_set(&lock, 0); |
* be called by the real time thread after it has |
78 |
} |
* finished reading the configuration object. If |
79 |
|
* the non real time thread is waiting in |
80 |
|
* SwitchConfig() it will be awaken when no real |
81 |
|
* time threads are locked anymore. |
82 |
|
*/ |
83 |
|
void Unlock() { |
84 |
|
lock.store(0, memory_order_release); |
85 |
|
} |
86 |
|
|
87 |
|
Reader(SynchronizedConfig& config); |
88 |
|
~Reader(); |
89 |
|
private: |
90 |
|
friend class SynchronizedConfig; |
91 |
|
SynchronizedConfig& parent; |
92 |
|
int lockCount; // increased in every Lock(), |
93 |
|
// lowest bit is always set. |
94 |
|
atomic<int> lock; // equals lockCount when inside |
95 |
|
// critical region, otherwise 0 |
96 |
|
Reader* next; // only used locally in SwitchConfig |
97 |
|
int prevLock; // only used locally in SwitchConfig |
98 |
|
}; |
99 |
|
|
100 |
|
|
101 |
// methods for the non real time thread |
// methods for the non real time thread |
127 |
T& SwitchConfig(); |
T& SwitchConfig(); |
128 |
|
|
129 |
private: |
private: |
130 |
atomic_t lock; |
atomic<int> indexAtomic; |
|
atomic_t indexAtomic; |
|
131 |
int updateIndex; |
int updateIndex; |
132 |
T config[2]; |
T config[2]; |
133 |
|
std::set<Reader*> readers; |
134 |
}; |
}; |
135 |
|
|
136 |
template<class T> SynchronizedConfig<T>::SynchronizedConfig() { |
template<class T> SynchronizedConfig<T>::SynchronizedConfig() : |
137 |
atomic_set(&lock, 0); |
indexAtomic(0) { |
138 |
atomic_set(&indexAtomic, 0); |
updateIndex = 1; |
139 |
} |
} |
140 |
|
|
141 |
template<class T> T& SynchronizedConfig<T>::GetConfigForUpdate() { |
template<class T> T& SynchronizedConfig<T>::GetConfigForUpdate() { |
|
updateIndex = atomic_read(&indexAtomic) ^ 1; |
|
142 |
return config[updateIndex]; |
return config[updateIndex]; |
143 |
} |
} |
144 |
|
|
145 |
template<class T> T& SynchronizedConfig<T>::SwitchConfig() { |
template<class T> T& SynchronizedConfig<T>::SwitchConfig() { |
146 |
atomic_set(&indexAtomic, updateIndex); |
indexAtomic.store(updateIndex, memory_order_release); |
147 |
while (atomic_read(&lock)) |
atomic_thread_fence(memory_order_seq_cst); |
148 |
|
|
149 |
|
// first put all locking readers in a linked list |
150 |
|
Reader* lockingReaders = 0; |
151 |
|
for (typename std::set<Reader*>::iterator iter = readers.begin() ; |
152 |
|
iter != readers.end() ; |
153 |
|
iter++) { |
154 |
|
(*iter)->prevLock = (*iter)->lock.load(memory_order_acquire); |
155 |
|
if ((*iter)->prevLock) { |
156 |
|
(*iter)->next = lockingReaders; |
157 |
|
lockingReaders = *iter; |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
// wait until there are no locking readers left |
162 |
|
while (lockingReaders) { |
163 |
usleep(50000); |
usleep(50000); |
164 |
return config[updateIndex ^ 1]; |
Reader** prev = &lockingReaders; |
165 |
|
for (Reader* p = lockingReaders ; p ; p = p->next) { |
166 |
|
if (p->lock.load(memory_order_acquire) == p->prevLock) { |
167 |
|
prev = &p->next; |
168 |
|
} else { |
169 |
|
*prev = p->next; // unlink |
170 |
|
} |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
updateIndex ^= 1; |
175 |
|
return config[updateIndex]; |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
// ----- Reader ---- |
180 |
|
|
181 |
|
template <class T> |
182 |
|
SynchronizedConfig<T>::Reader::Reader(SynchronizedConfig& config) : |
183 |
|
parent(config), lock(0), lockCount(1) { |
184 |
|
parent.readers.insert(this); |
185 |
|
} |
186 |
|
|
187 |
|
template <class T> |
188 |
|
SynchronizedConfig<T>::Reader::~Reader() { |
189 |
|
parent.readers.erase(this); |
190 |
} |
} |
191 |
|
|
192 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |