22 |
|
|
23 |
#include "Condition.h" |
#include "Condition.h" |
24 |
|
|
25 |
|
#include "global.h" |
26 |
|
|
27 |
Condition::Condition(bool bInitialCondition) { |
Condition::Condition(bool bInitialCondition) { |
28 |
pthread_cond_init(&__posix_true_condition, NULL); |
pthread_cond_init(&__posix_true_condition, NULL); |
29 |
pthread_cond_init(&__posix_false_condition, NULL); |
pthread_cond_init(&__posix_false_condition, NULL); |
36 |
} |
} |
37 |
|
|
38 |
int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
39 |
|
dmsg(7,("Condition::Waitif() -> LOCK()\n")); |
40 |
Lock(); |
Lock(); |
41 |
|
dmsg(7,("Condition::Waitif() -> LOCK() passed\n")); |
42 |
int res = 0; |
int res = 0; |
43 |
if (this->bCondition == bCondition) { |
if (this->bCondition == bCondition) { |
44 |
if (bCondition) { |
if (bCondition) { // wait until condition turned 'false' |
45 |
if (TimeoutSeconds || TimeoutNanoSeconds) { |
if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout |
46 |
timespec timeout; |
timespec timeout; |
47 |
timeout.tv_sec = TimeoutSeconds; |
timeout.tv_sec = TimeoutSeconds; |
48 |
timeout.tv_nsec = TimeoutNanoSeconds; |
timeout.tv_nsec = TimeoutNanoSeconds; |
49 |
|
dmsg(7,("Condition::Waitif() -> waiting for 'false' condition with timeout\n")); |
50 |
res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout); |
res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout); |
51 |
|
dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n")); |
52 |
|
} |
53 |
|
else { // wait without timeout |
54 |
|
dmsg(7,("Condition::Waitif() -> waiting for 'false' condition\n")); |
55 |
|
pthread_cond_wait(&__posix_false_condition, &__posix_mutex); |
56 |
|
dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n")); |
57 |
} |
} |
|
else pthread_cond_wait(&__posix_false_condition, &__posix_mutex); |
|
58 |
} |
} |
59 |
else { |
else { // wait until condition turned 'true' |
60 |
if (TimeoutSeconds || TimeoutNanoSeconds) { |
if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout |
61 |
timespec timeout; |
timespec timeout; |
62 |
timeout.tv_sec = TimeoutSeconds; |
timeout.tv_sec = TimeoutSeconds; |
63 |
timeout.tv_nsec = TimeoutNanoSeconds; |
timeout.tv_nsec = TimeoutNanoSeconds; |
64 |
|
dmsg(7,("Condition::Waitif() -> waiting for 'true' condition with timeout\n")); |
65 |
res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout); |
res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout); |
66 |
|
dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n")); |
67 |
|
} |
68 |
|
else { // wait without timeout |
69 |
|
dmsg(7,("Condition::Waitif() -> waiting for 'true' condition\n")); |
70 |
|
pthread_cond_wait(&__posix_true_condition, &__posix_mutex); |
71 |
|
dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n")); |
72 |
} |
} |
|
else pthread_cond_wait(&__posix_true_condition, &__posix_mutex); |
|
73 |
} |
} |
74 |
} |
} |
75 |
return res; |
return res; |
77 |
|
|
78 |
int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
79 |
int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds); |
int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds); |
80 |
|
dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK()\n")); |
81 |
Unlock(); |
Unlock(); |
82 |
|
dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK() passed\n")); |
83 |
return res; |
return res; |
84 |
} |
} |
85 |
|
|
86 |
void Condition::Set(bool bCondition) { |
void Condition::Set(bool bCondition) { |
87 |
|
dmsg(7,("Condition::Set() -> LOCK()\n")); |
88 |
Lock(); |
Lock(); |
89 |
|
dmsg(7,("Condition::Set() -> LOCK() passed\n")); |
90 |
if (this->bCondition != bCondition) { |
if (this->bCondition != bCondition) { |
91 |
this->bCondition = bCondition; |
this->bCondition = bCondition; |
92 |
if (bCondition) pthread_cond_broadcast(&__posix_true_condition); |
if (bCondition) { |
93 |
else pthread_cond_broadcast(&__posix_false_condition); |
dmsg(7,("Condition::Set() -> broadcasting 'true' condition\n")); |
94 |
|
pthread_cond_broadcast(&__posix_true_condition); |
95 |
|
} |
96 |
|
else { |
97 |
|
dmsg(7,("Condition::Set() -> broadcasting 'false' condition\n")); |
98 |
|
pthread_cond_broadcast(&__posix_false_condition); |
99 |
|
} |
100 |
} |
} |
101 |
else Unlock(); |
Unlock(); |
102 |
} |
} |