3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005, 2006 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 * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
#include <sys/time.h> |
25 |
|
|
26 |
#include "Condition.h" |
#include "Condition.h" |
27 |
|
|
28 |
|
#include "global.h" |
29 |
|
|
30 |
|
namespace LinuxSampler { |
31 |
|
|
32 |
Condition::Condition(bool bInitialCondition) { |
Condition::Condition(bool bInitialCondition) { |
33 |
pthread_cond_init(&__posix_true_condition, NULL); |
pthread_cond_init(&__posix_true_condition, NULL); |
34 |
pthread_cond_init(&__posix_false_condition, NULL); |
pthread_cond_init(&__posix_false_condition, NULL); |
41 |
} |
} |
42 |
|
|
43 |
int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
44 |
|
dmsg(7,("Condition::Waitif() -> LOCK()\n")); |
45 |
Lock(); |
Lock(); |
46 |
|
dmsg(7,("Condition::Waitif() -> LOCK() passed\n")); |
47 |
int res = 0; |
int res = 0; |
48 |
if (this->bCondition == bCondition) { |
if (this->bCondition == bCondition) { |
49 |
if (bCondition) { |
if (bCondition) { // wait until condition turned 'false' |
50 |
if (TimeoutSeconds || TimeoutNanoSeconds) { |
if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout |
51 |
|
struct timeval now; |
52 |
|
gettimeofday(&now, 0); |
53 |
timespec timeout; |
timespec timeout; |
54 |
timeout.tv_sec = TimeoutSeconds; |
timeout.tv_sec = now.tv_sec + TimeoutSeconds; |
55 |
timeout.tv_nsec = TimeoutNanoSeconds; |
timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds; |
56 |
|
dmsg(7,("Condition::Waitif() -> waiting for 'false' condition with timeout\n")); |
57 |
res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout); |
res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout); |
58 |
|
dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n")); |
59 |
|
} |
60 |
|
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 |
} |
} |
|
else pthread_cond_wait(&__posix_false_condition, &__posix_mutex); |
|
65 |
} |
} |
66 |
else { |
else { // wait until condition turned 'true' |
67 |
if (TimeoutSeconds || TimeoutNanoSeconds) { |
if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout |
68 |
|
struct timeval now; |
69 |
|
gettimeofday(&now, 0); |
70 |
timespec timeout; |
timespec timeout; |
71 |
timeout.tv_sec = TimeoutSeconds; |
timeout.tv_sec = now.tv_sec + TimeoutSeconds; |
72 |
timeout.tv_nsec = TimeoutNanoSeconds; |
timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds; |
73 |
|
dmsg(7,("Condition::Waitif() -> waiting for 'true' condition with timeout\n")); |
74 |
res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout); |
res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout); |
75 |
|
dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n")); |
76 |
|
} |
77 |
|
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 |
} |
} |
|
else pthread_cond_wait(&__posix_true_condition, &__posix_mutex); |
|
82 |
} |
} |
83 |
} |
} |
84 |
return res; |
return res; |
86 |
|
|
87 |
int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
88 |
int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds); |
int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds); |
89 |
|
dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK()\n")); |
90 |
Unlock(); |
Unlock(); |
91 |
|
dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK() passed\n")); |
92 |
return res; |
return res; |
93 |
} |
} |
94 |
|
|
95 |
void Condition::Set(bool bCondition) { |
void Condition::Set(bool bCondition) { |
96 |
|
dmsg(7,("Condition::Set() -> LOCK()\n")); |
97 |
Lock(); |
Lock(); |
98 |
|
dmsg(7,("Condition::Set() -> LOCK() passed\n")); |
99 |
if (this->bCondition != bCondition) { |
if (this->bCondition != bCondition) { |
100 |
this->bCondition = bCondition; |
this->bCondition = bCondition; |
101 |
if (bCondition) pthread_cond_broadcast(&__posix_true_condition); |
if (bCondition) { |
102 |
else pthread_cond_broadcast(&__posix_false_condition); |
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 |
} |
} |
110 |
else Unlock(); |
Unlock(); |
111 |
} |
} |
112 |
|
|
113 |
|
} // namespace LinuxSampler |