2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
25 |
#define __CONDITIONSERVER_H__ |
#define __CONDITIONSERVER_H__ |
26 |
|
|
27 |
#include "Mutex.h" |
#include "Mutex.h" |
28 |
#include "Condition.h" |
#include "SynchronizedConfig.h" |
29 |
#include "global.h" |
|
30 |
|
namespace LinuxSampler { |
31 |
|
|
32 |
/** |
/** |
33 |
* Thread safe condition for semi real time operation |
* Thread safe condition for semi real time operation |
41 |
* method will immediately return if the current condition already equals |
* method will immediately return if the current condition already equals |
42 |
* the desired condition given with Push(), if it's not equal then Push() |
* the desired condition given with Push(), if it's not equal then Push() |
43 |
* will cause a request to change the condition and will block until the |
* will cause a request to change the condition and will block until the |
44 |
* condition actually changed, which happens when the RTT calls the Pop() |
* condition actually changed, which happens when the RTT is outside its |
45 |
* method, which will also return the new condition to the RTT. |
* critical region, or has called the Pop() method, which will also return |
46 |
|
* the new condition to the RTT. |
47 |
* |
* |
48 |
* Advantage of this technique is that the RTT doesn't has to execute system |
* 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 |
* calls whenever it wants to check for a condition change. Disadvantage on |
50 |
* to execute one system call when the condition changed. Disadvantage on |
* the other hand is that the NRTTs might be blocked for some time, but this |
51 |
* the other hand is that the NRTTs might be blocked for a long time, but |
* is usually unproblematic for NRTTs. |
|
* this is usually unproblematic for NRTTs. |
|
52 |
*/ |
*/ |
53 |
class ConditionServer { |
class ConditionServer { |
54 |
public: |
public: |
62 |
* equals \a bCondition, then this method will immediately return, |
* equals \a bCondition, then this method will immediately return, |
63 |
* if not it will block the calling thread until the condition |
* if not it will block the calling thread until the condition |
64 |
* actually changed to the requested condition (which happens when |
* actually changed to the requested condition (which happens when |
65 |
* Pop() is called by the real time thread). If there are multiple |
* Pop() has been called by the real time thread, or when the real |
66 |
|
* time thread is outside its critical region). If there are multiple |
67 |
* non real time threads calling Push() only one by one will be |
* non real time threads calling Push() only one by one will be |
68 |
* served, all others blocked meanwhile. When the calling thread |
* served, all others blocked meanwhile. When the calling thread |
69 |
* returns from Push() the Push() method will still be blocked |
* returns from Push() the Push() method will still be blocked |
95 |
* (default: 0s) |
* (default: 0s) |
96 |
* @param TimeoutNanoSeconds - optional: max wait time in nano |
* @param TimeoutNanoSeconds - optional: max wait time in nano |
97 |
* seconds (default: 0ns) |
* seconds (default: 0ns) |
98 |
|
* @param bAlreadyLocked - optional: you must set this to true if |
99 |
|
* you have called Push() before and are |
100 |
|
* using PushAndUnlock to end the |
101 |
|
* critical region (default: false) |
102 |
* @returns bool pointer with condition before PushAndUnlock() |
* @returns bool pointer with condition before PushAndUnlock() |
103 |
* call, NULL if timeout exceeded |
* call, NULL if timeout exceeded |
104 |
*/ |
*/ |
105 |
bool* PushAndUnlock(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
bool* PushAndUnlock(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L, bool bAlreadyLocked = false); |
106 |
|
|
107 |
|
void PushAndUnlock2(bool bCondition); |
108 |
|
|
109 |
/** |
/** |
110 |
* Should be called by the NRTT after it left it's critical section |
* Should be called by the NRTT after it left it's critical section |
126 |
// method for real time thread (1 RTT) |
// method for real time thread (1 RTT) |
127 |
|
|
128 |
/** |
/** |
129 |
* Should frequently be called by the real time thread (RTT) to |
* Should be called by the real time thread (RTT) at the |
130 |
* check for a condition change. If a NRTT requested a condition |
* beginning of the critical region to check for a condition |
131 |
* change by calling Push() ot PushAndUnlock() the NRTT wil be |
* change. If an NRTT requested a condition change by calling |
132 |
* blocked until the RTT calls Pop(), means until the RTT actually |
* Push() or PushAndUnlock() the NRTT will be blocked until |
133 |
* knows about the condition change. |
* it knows that the RTT will not go into its critical region |
134 |
|
* without being notified about the condition change. |
135 |
* |
* |
136 |
* @returns current condition |
* @returns current condition |
137 |
*/ |
*/ |
138 |
bool Pop(); |
bool Pop() { |
139 |
|
return Reader.Lock(); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
|
* Should be called by the real time thread (RTT) at the end of |
144 |
|
* the critical region. |
145 |
|
*/ |
146 |
|
void RttDone() { |
147 |
|
Reader.Unlock(); |
148 |
|
} |
149 |
|
|
150 |
protected: |
protected: |
151 |
bool bConditionQuick; |
SynchronizedConfig<bool> Condition; |
152 |
bool bChangeRequest; |
SynchronizedConfig<bool>::Reader Reader; |
153 |
bool bOldCondition; |
bool bOldCondition; |
|
Condition SyncCondition; |
|
154 |
Mutex PushMutex; |
Mutex PushMutex; |
155 |
}; |
}; |
156 |
|
|
157 |
|
} // namespace LinuxSampler |
158 |
|
|
159 |
#endif // __CONDITIONSERVER_H__ |
#endif // __CONDITIONSERVER_H__ |