1 |
schoenebeck |
53 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
schoenebeck |
56 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
880 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
schoenebeck |
53 |
* * |
8 |
|
|
* This program is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#ifndef __CONDITIONSERVER_H__ |
25 |
|
|
#define __CONDITIONSERVER_H__ |
26 |
|
|
|
27 |
|
|
#include "Mutex.h" |
28 |
|
|
#include "Condition.h" |
29 |
|
|
#include "global.h" |
30 |
|
|
|
31 |
schoenebeck |
880 |
namespace LinuxSampler { |
32 |
|
|
|
33 |
schoenebeck |
53 |
/** |
34 |
|
|
* Thread safe condition for semi real time operation |
35 |
|
|
* |
36 |
|
|
* Sense behind the ConditionServer is to sync a boolean condition between |
37 |
|
|
* one real time thread (RTT) and n non real time threads (NRTT). |
38 |
|
|
* |
39 |
|
|
* 1 Real Time Thread <--> Condition Object <--> n Non Real Time Threads |
40 |
|
|
* |
41 |
|
|
* The non real time threads set the condition by calling Push(), this |
42 |
|
|
* method will immediately return if the current condition already equals |
43 |
|
|
* the desired condition given with Push(), if it's not equal then Push() |
44 |
|
|
* will cause a request to change the condition and will block until the |
45 |
|
|
* condition actually changed, which happens when the RTT calls the Pop() |
46 |
|
|
* method, which will also return the new condition to the RTT. |
47 |
|
|
* |
48 |
|
|
* Advantage of this technique is that the RTT doesn't has to execute system |
49 |
|
|
* calls whenever it wants to check for a condition change, the RTT only has |
50 |
|
|
* to execute one system call when the condition changed. Disadvantage on |
51 |
|
|
* the other hand is that the NRTTs might be blocked for a long time, but |
52 |
|
|
* this is usually unproblematic for NRTTs. |
53 |
|
|
*/ |
54 |
|
|
class ConditionServer { |
55 |
|
|
public: |
56 |
|
|
ConditionServer(); |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
// methods for non realtime threads (0..n NRTTs) |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* Set condition to \a bCondition. If current condition already |
63 |
|
|
* equals \a bCondition, then this method will immediately return, |
64 |
|
|
* if not it will block the calling thread until the condition |
65 |
|
|
* actually changed to the requested condition (which happens when |
66 |
|
|
* Pop() is called by the real time thread). If there are multiple |
67 |
|
|
* non real time threads calling Push() only one by one will be |
68 |
|
|
* served, all others blocked meanwhile. When the calling thread |
69 |
|
|
* returns from Push() the Push() method will still be blocked |
70 |
|
|
* against other NRTTs, so the thread can safely enter a critical |
71 |
|
|
* section and has to Unlock() right after it left it's critical |
72 |
|
|
* section, so other NRTTs can pass Push(). |
73 |
|
|
* |
74 |
|
|
* @param bCondition - condition to set |
75 |
|
|
* @param TimeoutSeconds - optional: max. wait time in seconds |
76 |
|
|
* (default: 0s) |
77 |
|
|
* @param TimeoutNanoSeconds - optional: max wait time in nano |
78 |
|
|
* seconds (default: 0ns) |
79 |
|
|
* @returns bool pointer with condition before Push() call, NULL if |
80 |
|
|
* timeout exceeded |
81 |
|
|
* @see Unlock() |
82 |
|
|
*/ |
83 |
|
|
bool* Push(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* Same as Push(), except that PushAndUnlock() will unlock the |
87 |
|
|
* ConditionServer right after so that other NRTTs can follow to |
88 |
|
|
* pass push. You should only call this method if you're sure that |
89 |
|
|
* no other NRTT will change the condition, otherwise you should |
90 |
|
|
* call Push() instead, execute the critical section and manually |
91 |
|
|
* unlock at the end of the critical section. |
92 |
|
|
* |
93 |
|
|
* @param bCondition - condition to set |
94 |
|
|
* @param TimeoutSeconds - optional: max. wait time in seconds |
95 |
|
|
* (default: 0s) |
96 |
|
|
* @param TimeoutNanoSeconds - optional: max wait time in nano |
97 |
|
|
* seconds (default: 0ns) |
98 |
|
|
* @returns bool pointer with condition before PushAndUnlock() |
99 |
|
|
* call, NULL if timeout exceeded |
100 |
|
|
*/ |
101 |
|
|
bool* PushAndUnlock(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Should be called by the NRTT after it left it's critical section |
105 |
|
|
* to unlock the ConditionServer and give other NRTTs the chance to |
106 |
|
|
* pass Push(). |
107 |
|
|
*/ |
108 |
|
|
void Unlock(); |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* Returns unsafely the current condition. This method will not |
112 |
|
|
* block and should only be used in a not thread critical context. |
113 |
|
|
* |
114 |
|
|
* @returns current condition (unsafe) |
115 |
|
|
*/ |
116 |
|
|
bool GetUnsafe(); |
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
// method for real time thread (1 RTT) |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Should frequently be called by the real time thread (RTT) to |
124 |
|
|
* check for a condition change. If a NRTT requested a condition |
125 |
|
|
* change by calling Push() ot PushAndUnlock() the NRTT wil be |
126 |
|
|
* blocked until the RTT calls Pop(), means until the RTT actually |
127 |
|
|
* knows about the condition change. |
128 |
|
|
* |
129 |
|
|
* @returns current condition |
130 |
|
|
*/ |
131 |
|
|
bool Pop(); |
132 |
|
|
|
133 |
|
|
protected: |
134 |
|
|
bool bConditionQuick; |
135 |
|
|
bool bChangeRequest; |
136 |
|
|
bool bOldCondition; |
137 |
|
|
Condition SyncCondition; |
138 |
|
|
Mutex PushMutex; |
139 |
|
|
}; |
140 |
|
|
|
141 |
schoenebeck |
880 |
} // namespace LinuxSampler |
142 |
|
|
|
143 |
schoenebeck |
53 |
#endif // __CONDITIONSERVER_H__ |