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