1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2006-2008 Andreas Persson * |
4 |
* * |
5 |
* 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 * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
18 |
* MA 02110-1301 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#ifndef __SYNCHRONIZEDCONFIG_H__ |
22 |
#define __SYNCHRONIZEDCONFIG_H__ |
23 |
|
24 |
#include <set> |
25 |
|
26 |
namespace LinuxSampler { |
27 |
|
28 |
/** |
29 |
* Thread safe management of configuration data, where the data is |
30 |
* updated by a single non real time thread and read by a number |
31 |
* of real time threads. |
32 |
* |
33 |
* The synchronization is achieved by using two instances of the |
34 |
* configuration data. The non real time thread gets access to the |
35 |
* instance not currently in use by the real time threads by |
36 |
* calling GetConfigForUpdate(). After the data is updated, the |
37 |
* non real time thread must call SwitchConfig() and redo the |
38 |
* update on the other instance. SwitchConfig() blocks until it is |
39 |
* safe to modify the other instance. |
40 |
* |
41 |
* The real time threads need one Reader object each to access the |
42 |
* configuration data. This object must be created outside the |
43 |
* real time thread. The Lock() function returns a reference to |
44 |
* the data to be read, and Unlock() must be called when finished |
45 |
* reading the data. (Neither Lock nor Unlock will block the real |
46 |
* time thread, or use any system calls.) |
47 |
*/ |
48 |
template<class T> |
49 |
class SynchronizedConfig { |
50 |
struct atomic_t { volatile int word; }; |
51 |
|
52 |
public: |
53 |
SynchronizedConfig(); |
54 |
|
55 |
// methods for the real time thread |
56 |
|
57 |
class Reader { |
58 |
public: |
59 |
/** |
60 |
* Gets the configuration object for use by the |
61 |
* real time thread. The object is safe to use |
62 |
* (read only) until Unlock() is called. |
63 |
* |
64 |
* @returns a reference to the configuration |
65 |
* object to be read by the real time |
66 |
* thread |
67 |
*/ |
68 |
const T& Lock() { |
69 |
atomicSet(&lock, 1); |
70 |
return parent.config[atomicRead(&parent.indexAtomic)]; |
71 |
} |
72 |
|
73 |
/** |
74 |
* Unlock the configuration object. Unlock() must |
75 |
* be called by the real time thread after it has |
76 |
* finished reading the configuration object. If |
77 |
* the non real time thread is waiting in |
78 |
* SwitchConfig() it will be awaken when no real |
79 |
* time threads are locked anymore. |
80 |
*/ |
81 |
void Unlock() { |
82 |
atomicSet(&flag, 0); |
83 |
atomicSet(&lock, 0); |
84 |
} |
85 |
|
86 |
Reader(SynchronizedConfig& config); |
87 |
~Reader(); |
88 |
private: |
89 |
friend class SynchronizedConfig; |
90 |
SynchronizedConfig& parent; |
91 |
atomic_t lock; |
92 |
atomic_t flag; |
93 |
Reader *next; // only used locally in SwitchConfig |
94 |
}; |
95 |
|
96 |
|
97 |
// methods for the non real time thread |
98 |
|
99 |
/** |
100 |
* Gets the configuration object for use by the non real |
101 |
* time thread. The object returned is not in use by the |
102 |
* real time thread, so it can safely be updated. After |
103 |
* the update is done, the non real time thread must call |
104 |
* SwitchConfig() and the same update must be done again. |
105 |
* |
106 |
* @returns a reference to the configuration object to be |
107 |
* updated by the non real time thread |
108 |
*/ |
109 |
T& GetConfigForUpdate(); |
110 |
|
111 |
/** |
112 |
* Atomically switch the newly updated configuration |
113 |
* object with the one used by the real time thread, then |
114 |
* wait for the real time thread to finish working with |
115 |
* the old object before returning the old object. |
116 |
* SwitchConfig() must be called by the non real time |
117 |
* thread after an update has been done, and the object |
118 |
* returned must be updated in the same way as the first. |
119 |
* |
120 |
* @returns a reference to the configuration object to be |
121 |
* updated by the non real time thread |
122 |
*/ |
123 |
T& SwitchConfig(); |
124 |
|
125 |
private: |
126 |
atomic_t indexAtomic; |
127 |
int updateIndex; |
128 |
T config[2]; |
129 |
std::set<Reader*> readers; |
130 |
|
131 |
static int atomicRead(atomic_t* pSharedVariable) { |
132 |
return pSharedVariable->word; |
133 |
} |
134 |
|
135 |
static void atomicSet(atomic_t* pSharedVariable, int value) { |
136 |
pSharedVariable->word = value; |
137 |
} |
138 |
}; |
139 |
|
140 |
template<class T> SynchronizedConfig<T>::SynchronizedConfig() { |
141 |
atomicSet(&indexAtomic, 0); |
142 |
updateIndex = 1; |
143 |
} |
144 |
|
145 |
template<class T> T& SynchronizedConfig<T>::GetConfigForUpdate() { |
146 |
return config[updateIndex]; |
147 |
} |
148 |
|
149 |
template<class T> T& SynchronizedConfig<T>::SwitchConfig() { |
150 |
atomicSet(&indexAtomic, updateIndex); |
151 |
|
152 |
// first put all locking readers in a linked list |
153 |
Reader* lockingReaders = 0; |
154 |
for (typename std::set<Reader*>::iterator iter = readers.begin() ; |
155 |
iter != readers.end() ; |
156 |
iter++) { |
157 |
atomicSet(&(*iter)->flag, 1); |
158 |
if (atomicRead(&(*iter)->lock) && atomicRead(&(*iter)->flag)) { |
159 |
(*iter)->next = lockingReaders; |
160 |
lockingReaders = *iter; |
161 |
} |
162 |
} |
163 |
|
164 |
// wait until there are no locking readers left |
165 |
while (lockingReaders) { |
166 |
usleep(50000); |
167 |
Reader** prev = &lockingReaders; |
168 |
for (Reader* p = lockingReaders ; p ; p = p->next) { |
169 |
if (atomicRead(&p->lock) && atomicRead(&p->flag)) prev = &p->next; |
170 |
else *prev = p->next; // unlink |
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) : parent(config) { |
183 |
atomicSet(&lock, 0); |
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 |
193 |
|
194 |
#endif |