/[svn]/linuxsampler/trunk/src/common/SynchronizedConfig.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/common/SynchronizedConfig.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1644 - (hide annotations) (download) (as text)
Sat Jan 19 16:55:03 2008 UTC (16 years, 3 months ago) by persson
File MIME type: text/x-c++hdr
File size: 7796 byte(s)
* fixed memory leaks that occurred when liblinuxsampler was unloaded
* fixed a memory leak that could happen when a channel was deleted
  while notes were playing
* fixed memory management bug in ASIO driver
* optimized the SynchronizedConfig class so it doesn't wait
  unnecessarily long after an update

1 persson 840 /***************************************************************************
2     * *
3 persson 1644 * Copyright (C) 2006-2008 Andreas Persson *
4 persson 840 * *
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 persson 846 #include <set>
25 persson 840
26     namespace LinuxSampler {
27    
28     /**
29     * Thread safe management of configuration data, where the data is
30 persson 846 * updated by a single non real time thread and read by a number
31     * of real time threads.
32 persson 840 *
33     * The synchronization is achieved by using two instances of the
34     * configuration data. The non real time thread gets access to the
35 persson 846 * instance not currently in use by the real time threads by
36 persson 840 * 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 persson 846 * The real time threads need one Reader object each to access the
42 persson 1038 * 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 persson 846 * reading the data. (Neither Lock nor Unlock will block the real
46     * time thread, or use any system calls.)
47 persson 840 */
48     template<class T>
49     class SynchronizedConfig {
50 schoenebeck 1424 struct atomic_t { volatile int word; };
51    
52 persson 840 public:
53     SynchronizedConfig();
54    
55     // methods for the real time thread
56    
57 persson 846 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 schoenebeck 1424 atomicSet(&lock, 1);
70     return parent.config[atomicRead(&parent.indexAtomic)];
71 persson 846 }
72 persson 840
73 persson 846 /**
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 persson 1644 atomicSet(&flag, 0);
83 schoenebeck 1424 atomicSet(&lock, 0);
84 persson 846 }
85 persson 840
86 persson 846 Reader(SynchronizedConfig& config);
87     ~Reader();
88     private:
89     friend class SynchronizedConfig;
90     SynchronizedConfig& parent;
91     atomic_t lock;
92 persson 1644 atomic_t flag;
93 persson 846 Reader *next; // only used locally in SwitchConfig
94     };
95 persson 840
96 persson 846
97 persson 840 // 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 persson 846 std::set<Reader*> readers;
130 schoenebeck 1424
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 persson 840 };
139    
140     template<class T> SynchronizedConfig<T>::SynchronizedConfig() {
141 schoenebeck 1424 atomicSet(&indexAtomic, 0);
142 persson 846 updateIndex = 1;
143 persson 840 }
144    
145     template<class T> T& SynchronizedConfig<T>::GetConfigForUpdate() {
146     return config[updateIndex];
147     }
148    
149     template<class T> T& SynchronizedConfig<T>::SwitchConfig() {
150 schoenebeck 1424 atomicSet(&indexAtomic, updateIndex);
151 persson 846
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 persson 1644 atomicSet(&(*iter)->flag, 1);
158     if (atomicRead(&(*iter)->lock) && atomicRead(&(*iter)->flag)) {
159 persson 846 (*iter)->next = lockingReaders;
160     lockingReaders = *iter;
161     }
162     }
163    
164     // wait until there are no locking readers left
165     while (lockingReaders) {
166 persson 840 usleep(50000);
167 persson 846 Reader** prev = &lockingReaders;
168     for (Reader* p = lockingReaders ; p ; p = p->next) {
169 persson 1644 if (atomicRead(&p->lock) && atomicRead(&p->flag)) prev = &p->next;
170 persson 846 else *prev = p->next; // unlink
171     }
172     }
173    
174     updateIndex ^= 1;
175     return config[updateIndex];
176 persson 840 }
177    
178 persson 846
179     // ----- Reader ----
180    
181     template <class T>
182     SynchronizedConfig<T>::Reader::Reader(SynchronizedConfig& config) : parent(config) {
183 schoenebeck 1424 atomicSet(&lock, 0);
184 persson 846 parent.readers.insert(this);
185     }
186    
187     template <class T>
188     SynchronizedConfig<T>::Reader::~Reader() {
189     parent.readers.erase(this);
190     }
191    
192 persson 840 } // namespace LinuxSampler
193    
194     #endif

  ViewVC Help
Powered by ViewVC