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 |
persson |
417 |
#include <sys/time.h> |
25 |
|
|
|
26 |
schoenebeck |
53 |
#include "Condition.h" |
27 |
|
|
|
28 |
schoenebeck |
63 |
#include "global.h" |
29 |
|
|
|
30 |
schoenebeck |
880 |
namespace LinuxSampler { |
31 |
|
|
|
32 |
schoenebeck |
53 |
Condition::Condition(bool bInitialCondition) { |
33 |
|
|
pthread_cond_init(&__posix_true_condition, NULL); |
34 |
|
|
pthread_cond_init(&__posix_false_condition, NULL); |
35 |
|
|
bCondition = bInitialCondition; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
Condition::~Condition() { |
39 |
|
|
pthread_cond_destroy(&__posix_true_condition); |
40 |
|
|
pthread_cond_destroy(&__posix_false_condition); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
44 |
schoenebeck |
63 |
dmsg(7,("Condition::Waitif() -> LOCK()\n")); |
45 |
schoenebeck |
53 |
Lock(); |
46 |
schoenebeck |
63 |
dmsg(7,("Condition::Waitif() -> LOCK() passed\n")); |
47 |
schoenebeck |
53 |
int res = 0; |
48 |
|
|
if (this->bCondition == bCondition) { |
49 |
schoenebeck |
63 |
if (bCondition) { // wait until condition turned 'false' |
50 |
|
|
if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout |
51 |
persson |
417 |
struct timeval now; |
52 |
|
|
gettimeofday(&now, 0); |
53 |
schoenebeck |
53 |
timespec timeout; |
54 |
persson |
417 |
timeout.tv_sec = now.tv_sec + TimeoutSeconds; |
55 |
|
|
timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds; |
56 |
schoenebeck |
63 |
dmsg(7,("Condition::Waitif() -> waiting for 'false' condition with timeout\n")); |
57 |
schoenebeck |
53 |
res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout); |
58 |
schoenebeck |
63 |
dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n")); |
59 |
schoenebeck |
53 |
} |
60 |
schoenebeck |
63 |
else { // wait without timeout |
61 |
|
|
dmsg(7,("Condition::Waitif() -> waiting for 'false' condition\n")); |
62 |
|
|
pthread_cond_wait(&__posix_false_condition, &__posix_mutex); |
63 |
|
|
dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n")); |
64 |
|
|
} |
65 |
schoenebeck |
53 |
} |
66 |
schoenebeck |
63 |
else { // wait until condition turned 'true' |
67 |
|
|
if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout |
68 |
persson |
417 |
struct timeval now; |
69 |
|
|
gettimeofday(&now, 0); |
70 |
schoenebeck |
53 |
timespec timeout; |
71 |
persson |
417 |
timeout.tv_sec = now.tv_sec + TimeoutSeconds; |
72 |
|
|
timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds; |
73 |
schoenebeck |
63 |
dmsg(7,("Condition::Waitif() -> waiting for 'true' condition with timeout\n")); |
74 |
schoenebeck |
53 |
res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout); |
75 |
schoenebeck |
63 |
dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n")); |
76 |
schoenebeck |
53 |
} |
77 |
schoenebeck |
63 |
else { // wait without timeout |
78 |
|
|
dmsg(7,("Condition::Waitif() -> waiting for 'true' condition\n")); |
79 |
|
|
pthread_cond_wait(&__posix_true_condition, &__posix_mutex); |
80 |
|
|
dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n")); |
81 |
|
|
} |
82 |
schoenebeck |
53 |
} |
83 |
|
|
} |
84 |
|
|
return res; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
88 |
|
|
int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds); |
89 |
schoenebeck |
63 |
dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK()\n")); |
90 |
schoenebeck |
53 |
Unlock(); |
91 |
schoenebeck |
63 |
dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK() passed\n")); |
92 |
schoenebeck |
53 |
return res; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
void Condition::Set(bool bCondition) { |
96 |
schoenebeck |
63 |
dmsg(7,("Condition::Set() -> LOCK()\n")); |
97 |
schoenebeck |
53 |
Lock(); |
98 |
schoenebeck |
63 |
dmsg(7,("Condition::Set() -> LOCK() passed\n")); |
99 |
schoenebeck |
53 |
if (this->bCondition != bCondition) { |
100 |
|
|
this->bCondition = bCondition; |
101 |
schoenebeck |
63 |
if (bCondition) { |
102 |
|
|
dmsg(7,("Condition::Set() -> broadcasting 'true' condition\n")); |
103 |
|
|
pthread_cond_broadcast(&__posix_true_condition); |
104 |
|
|
} |
105 |
|
|
else { |
106 |
|
|
dmsg(7,("Condition::Set() -> broadcasting 'false' condition\n")); |
107 |
|
|
pthread_cond_broadcast(&__posix_false_condition); |
108 |
|
|
} |
109 |
schoenebeck |
53 |
} |
110 |
schoenebeck |
63 |
Unlock(); |
111 |
schoenebeck |
53 |
} |
112 |
schoenebeck |
880 |
|
113 |
schoenebeck |
1221 |
bool Condition::GetUnsafe() { |
114 |
|
|
return bCondition; |
115 |
|
|
} |
116 |
|
|
|
117 |
schoenebeck |
880 |
} // namespace LinuxSampler |