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