1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2006 Andreas Persson * |
* Copyright (C) 2006, 2007 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 * |
21 |
#ifndef __SYNCHRONIZEDCONFIG_H__ |
#ifndef __SYNCHRONIZEDCONFIG_H__ |
22 |
#define __SYNCHRONIZEDCONFIG_H__ |
#define __SYNCHRONIZEDCONFIG_H__ |
23 |
|
|
|
#include "atomic.h" |
|
24 |
#include <set> |
#include <set> |
25 |
|
|
26 |
namespace LinuxSampler { |
namespace LinuxSampler { |
47 |
*/ |
*/ |
48 |
template<class T> |
template<class T> |
49 |
class SynchronizedConfig { |
class SynchronizedConfig { |
50 |
|
struct atomic_t { volatile int word; }; |
51 |
|
|
52 |
public: |
public: |
53 |
SynchronizedConfig(); |
SynchronizedConfig(); |
54 |
|
|
66 |
* thread |
* thread |
67 |
*/ |
*/ |
68 |
const T& Lock() { |
const T& Lock() { |
69 |
atomic_set(&lock, 1); |
atomicSet(&lock, 1); |
70 |
return parent.config[atomic_read(&parent.indexAtomic)]; |
return parent.config[atomicRead(&parent.indexAtomic)]; |
71 |
} |
} |
72 |
|
|
73 |
/** |
/** |
79 |
* time threads are locked anymore. |
* time threads are locked anymore. |
80 |
*/ |
*/ |
81 |
void Unlock() { |
void Unlock() { |
82 |
atomic_set(&lock, 0); |
atomicSet(&lock, 0); |
83 |
} |
} |
84 |
|
|
85 |
Reader(SynchronizedConfig& config); |
Reader(SynchronizedConfig& config); |
125 |
int updateIndex; |
int updateIndex; |
126 |
T config[2]; |
T config[2]; |
127 |
std::set<Reader*> readers; |
std::set<Reader*> readers; |
128 |
|
|
129 |
|
static int atomicRead(atomic_t* pSharedVariable) { |
130 |
|
return pSharedVariable->word; |
131 |
|
} |
132 |
|
|
133 |
|
static void atomicSet(atomic_t* pSharedVariable, int value) { |
134 |
|
pSharedVariable->word = value; |
135 |
|
} |
136 |
}; |
}; |
137 |
|
|
138 |
template<class T> SynchronizedConfig<T>::SynchronizedConfig() { |
template<class T> SynchronizedConfig<T>::SynchronizedConfig() { |
139 |
atomic_set(&indexAtomic, 0); |
atomicSet(&indexAtomic, 0); |
140 |
updateIndex = 1; |
updateIndex = 1; |
141 |
} |
} |
142 |
|
|
145 |
} |
} |
146 |
|
|
147 |
template<class T> T& SynchronizedConfig<T>::SwitchConfig() { |
template<class T> T& SynchronizedConfig<T>::SwitchConfig() { |
148 |
atomic_set(&indexAtomic, updateIndex); |
atomicSet(&indexAtomic, updateIndex); |
149 |
|
|
150 |
// first put all locking readers in a linked list |
// first put all locking readers in a linked list |
151 |
Reader* lockingReaders = 0; |
Reader* lockingReaders = 0; |
152 |
for (typename std::set<Reader*>::iterator iter = readers.begin() ; |
for (typename std::set<Reader*>::iterator iter = readers.begin() ; |
153 |
iter != readers.end() ; |
iter != readers.end() ; |
154 |
iter++) { |
iter++) { |
155 |
if (atomic_read(&(*iter)->lock)) { |
if (atomicRead(&(*iter)->lock)) { |
156 |
(*iter)->next = lockingReaders; |
(*iter)->next = lockingReaders; |
157 |
lockingReaders = *iter; |
lockingReaders = *iter; |
158 |
} |
} |
163 |
usleep(50000); |
usleep(50000); |
164 |
Reader** prev = &lockingReaders; |
Reader** prev = &lockingReaders; |
165 |
for (Reader* p = lockingReaders ; p ; p = p->next) { |
for (Reader* p = lockingReaders ; p ; p = p->next) { |
166 |
if (atomic_read(&p->lock)) prev = &p->next; |
if (atomicRead(&p->lock)) prev = &p->next; |
167 |
else *prev = p->next; // unlink |
else *prev = p->next; // unlink |
168 |
} |
} |
169 |
} |
} |
177 |
|
|
178 |
template <class T> |
template <class T> |
179 |
SynchronizedConfig<T>::Reader::Reader(SynchronizedConfig& config) : parent(config) { |
SynchronizedConfig<T>::Reader::Reader(SynchronizedConfig& config) : parent(config) { |
180 |
atomic_set(&lock, 0); |
atomicSet(&lock, 0); |
181 |
parent.readers.insert(this); |
parent.readers.insert(this); |
182 |
} |
} |
183 |
|
|