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 |
1221 |
* Copyright (C) 2005 - 2007 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 __CONDITION_H__ |
25 |
|
|
#define __CONDITION_H__ |
26 |
|
|
|
27 |
|
|
#include "Mutex.h" |
28 |
|
|
|
29 |
senoner |
1481 |
#if defined(WIN32) |
30 |
|
|
#include <windows.h> |
31 |
|
|
typedef HANDLE win32thread_mutex_t; |
32 |
|
|
|
33 |
|
|
typedef struct |
34 |
|
|
{ |
35 |
|
|
int waiters_count_; |
36 |
|
|
// Number of waiting threads. |
37 |
|
|
|
38 |
|
|
CRITICAL_SECTION waiters_count_lock_; |
39 |
|
|
// Serialize access to <waiters_count_>. |
40 |
|
|
|
41 |
|
|
HANDLE sema_; |
42 |
|
|
// Semaphore used to queue up threads waiting for the condition to |
43 |
|
|
// become signaled. |
44 |
|
|
|
45 |
|
|
HANDLE waiters_done_; |
46 |
|
|
// An auto-reset event used by the broadcast/signal thread to wait |
47 |
|
|
// for all the waiting thread(s) to wake up and be released from the |
48 |
|
|
// semaphore. |
49 |
|
|
|
50 |
|
|
size_t was_broadcast_; |
51 |
|
|
// Keeps track of whether we were broadcasting or signaling. This |
52 |
|
|
// allows us to optimize the code if we're just signaling. |
53 |
|
|
} win32thread_cond_t; |
54 |
|
|
#endif |
55 |
|
|
|
56 |
schoenebeck |
880 |
namespace LinuxSampler { |
57 |
|
|
|
58 |
schoenebeck |
53 |
/** |
59 |
|
|
* Thread safe boolean condition. |
60 |
|
|
* |
61 |
|
|
* This is not meant to be used for real time operation! |
62 |
|
|
*/ |
63 |
|
|
class Condition : public Mutex { |
64 |
|
|
public: |
65 |
|
|
/** |
66 |
|
|
* Constructor |
67 |
|
|
* |
68 |
|
|
* @param bInitialCondition - optional: starting condition |
69 |
|
|
* (default = false) |
70 |
|
|
*/ |
71 |
|
|
Condition(bool bInitialCondition = false); |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Destructor |
75 |
|
|
*/ |
76 |
letz |
502 |
virtual ~Condition(); |
77 |
schoenebeck |
53 |
|
78 |
|
|
/** |
79 |
|
|
* Blocks the calling thread if current condition equals |
80 |
|
|
* \a bCondition, in this case the calling thread will be blocked |
81 |
|
|
* until condition turns. Upon successful return the Condition |
82 |
|
|
* object is locked, so the calling thread can safely run it's |
83 |
|
|
* critical section and has to explicitly call Unlock() right after |
84 |
|
|
* it left it's critcal section. |
85 |
|
|
* |
86 |
schoenebeck |
1221 |
* @e Note: If you don't provide a timeout value or if you provide a |
87 |
|
|
* timeout value of exactly 0s and 0ns, then this call will block |
88 |
schoenebeck |
1319 |
* without any timeout, or in other words: @e infinity! |
89 |
schoenebeck |
1221 |
* |
90 |
schoenebeck |
53 |
* @param bCondition - block in case of this condition |
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 0 on success, a value less than 0 if timeout exceeded |
96 |
|
|
*/ |
97 |
|
|
int WaitIf(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* Same as WaitIf(), except that WaitAndUnlockIf() will unlock the |
101 |
|
|
* Condition object, so only use this call if you don't need to |
102 |
|
|
* enter a thread critical section, otherwise use WaitIf() instead! |
103 |
|
|
* |
104 |
schoenebeck |
1221 |
* @e Note: If you don't provide a timeout value or if you provide a |
105 |
|
|
* timeout value of exactly 0s and 0ns, then this call will block |
106 |
schoenebeck |
1319 |
* without any timeout, or in other words: @e infinity! |
107 |
schoenebeck |
1221 |
* |
108 |
schoenebeck |
53 |
* @param bCondition - block in case of this condition |
109 |
|
|
* @param TimeoutSeconds - optional: max. wait time in seconds |
110 |
|
|
* (default: 0s) |
111 |
|
|
* @param TimeoutNanoSeconds - optional: max wait time in nano |
112 |
|
|
* seconds (default: 0ns) |
113 |
|
|
* @returns 0 on success, a value less than 0 if timeout exceeded |
114 |
|
|
* @see WaitIf() |
115 |
|
|
*/ |
116 |
|
|
int WaitAndUnlockIf(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
117 |
|
|
|
118 |
|
|
/** |
119 |
|
|
* Set Condition object to \a bCondition. Upon change of the |
120 |
|
|
* condition, other threads waiting for \a bCondition will be |
121 |
schoenebeck |
63 |
* awakened. (Note the condition will not be locked for the calling |
122 |
|
|
* thread after this method returns!) |
123 |
schoenebeck |
53 |
* |
124 |
|
|
* @param bCondition - new condition |
125 |
|
|
*/ |
126 |
|
|
void Set(bool bCondition); |
127 |
|
|
|
128 |
schoenebeck |
1221 |
/** |
129 |
schoenebeck |
1319 |
* Returns the current boolean state of this condition object. This |
130 |
|
|
* method never blocks, it returns immediately and doesn't use any |
131 |
|
|
* system calls. |
132 |
|
|
* |
133 |
schoenebeck |
1231 |
* @e Caution: this method is not thread safe! If you need to use |
134 |
|
|
* the condition state in a thread critical context you must call |
135 |
|
|
* @c Lock() and @c Unlock() respectively by yourself! |
136 |
|
|
*/ |
137 |
schoenebeck |
1221 |
bool GetUnsafe(); |
138 |
|
|
|
139 |
schoenebeck |
53 |
protected: |
140 |
senoner |
1481 |
#if defined(WIN32) |
141 |
|
|
win32thread_cond_t __win32_true_condition; |
142 |
|
|
win32thread_cond_t __win32_false_condition; |
143 |
|
|
#else |
144 |
schoenebeck |
53 |
pthread_cond_t __posix_true_condition; |
145 |
|
|
pthread_cond_t __posix_false_condition; |
146 |
senoner |
1481 |
#endif |
147 |
schoenebeck |
53 |
bool bCondition; |
148 |
|
|
}; |
149 |
|
|
|
150 |
schoenebeck |
880 |
} // namespace LinuxSampler |
151 |
|
|
|
152 |
schoenebeck |
53 |
#endif // __CONDITION_H__ |