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 |
53 |
* * |
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 __CONDITION_H__ |
24 |
|
|
#define __CONDITION_H__ |
25 |
|
|
|
26 |
|
|
#include "Mutex.h" |
27 |
|
|
|
28 |
|
|
/** |
29 |
|
|
* Thread safe boolean condition. |
30 |
|
|
* |
31 |
|
|
* This is not meant to be used for real time operation! |
32 |
|
|
*/ |
33 |
|
|
class Condition : public Mutex { |
34 |
|
|
public: |
35 |
|
|
/** |
36 |
|
|
* Constructor |
37 |
|
|
* |
38 |
|
|
* @param bInitialCondition - optional: starting condition |
39 |
|
|
* (default = false) |
40 |
|
|
*/ |
41 |
|
|
Condition(bool bInitialCondition = false); |
42 |
|
|
|
43 |
|
|
/** |
44 |
|
|
* Destructor |
45 |
|
|
*/ |
46 |
|
|
~Condition(); |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* Blocks the calling thread if current condition equals |
50 |
|
|
* \a bCondition, in this case the calling thread will be blocked |
51 |
|
|
* until condition turns. Upon successful return the Condition |
52 |
|
|
* object is locked, so the calling thread can safely run it's |
53 |
|
|
* critical section and has to explicitly call Unlock() right after |
54 |
|
|
* it left it's critcal section. |
55 |
|
|
* |
56 |
|
|
* @param bCondition - block in case of this condition |
57 |
|
|
* @param TimeoutSeconds - optional: max. wait time in seconds |
58 |
|
|
* (default: 0s) |
59 |
|
|
* @param TimeoutNanoSeconds - optional: max wait time in nano |
60 |
|
|
* seconds (default: 0ns) |
61 |
|
|
* @returns 0 on success, a value less than 0 if timeout exceeded |
62 |
|
|
*/ |
63 |
|
|
int WaitIf(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
64 |
|
|
|
65 |
|
|
/** |
66 |
|
|
* Same as WaitIf(), except that WaitAndUnlockIf() will unlock the |
67 |
|
|
* Condition object, so only use this call if you don't need to |
68 |
|
|
* enter a thread critical section, otherwise use WaitIf() instead! |
69 |
|
|
* |
70 |
|
|
* @param bCondition - block in case of this condition |
71 |
|
|
* @param TimeoutSeconds - optional: max. wait time in seconds |
72 |
|
|
* (default: 0s) |
73 |
|
|
* @param TimeoutNanoSeconds - optional: max wait time in nano |
74 |
|
|
* seconds (default: 0ns) |
75 |
|
|
* @returns 0 on success, a value less than 0 if timeout exceeded |
76 |
|
|
* @see WaitIf() |
77 |
|
|
*/ |
78 |
|
|
int WaitAndUnlockIf(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* Set Condition object to \a bCondition. Upon change of the |
82 |
|
|
* condition, other threads waiting for \a bCondition will be |
83 |
|
|
* awakened. |
84 |
|
|
* |
85 |
|
|
* @param bCondition - new condition |
86 |
|
|
*/ |
87 |
|
|
void Set(bool bCondition); |
88 |
|
|
|
89 |
|
|
protected: |
90 |
|
|
pthread_cond_t __posix_true_condition; |
91 |
|
|
pthread_cond_t __posix_false_condition; |
92 |
|
|
bool bCondition; |
93 |
|
|
}; |
94 |
|
|
|
95 |
|
|
#endif // __CONDITION_H__ |