/[svn]/linuxsampler/trunk/src/common/Condition.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/common/Condition.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (show annotations) (download)
Tue May 4 18:52:24 2004 UTC (19 years, 11 months ago) by schoenebeck
File size: 4989 byte(s)
* src/common/Thread.cpp: threads are now stoppable even if they are
  waiting for a condition
* src/common/Condition.cpp: fixed little misbehavior of Set() method,
  which locked the Condition object on return
* src/testcases: added a couple of new unit tests (against classes
  'Mutex', 'Condition' and 'Thread')

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
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 #include "Condition.h"
24
25 #include "global.h"
26
27 Condition::Condition(bool bInitialCondition) {
28 pthread_cond_init(&__posix_true_condition, NULL);
29 pthread_cond_init(&__posix_false_condition, NULL);
30 bCondition = bInitialCondition;
31 }
32
33 Condition::~Condition() {
34 pthread_cond_destroy(&__posix_true_condition);
35 pthread_cond_destroy(&__posix_false_condition);
36 }
37
38 int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) {
39 dmsg(7,("Condition::Waitif() -> LOCK()\n"));
40 Lock();
41 dmsg(7,("Condition::Waitif() -> LOCK() passed\n"));
42 int res = 0;
43 if (this->bCondition == bCondition) {
44 if (bCondition) { // wait until condition turned 'false'
45 if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
46 timespec timeout;
47 timeout.tv_sec = TimeoutSeconds;
48 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);
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 }
58 }
59 else { // wait until condition turned 'true'
60 if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
61 timespec timeout;
62 timeout.tv_sec = TimeoutSeconds;
63 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);
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 }
73 }
74 }
75 return res;
76 }
77
78 int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) {
79 int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds);
80 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK()\n"));
81 Unlock();
82 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK() passed\n"));
83 return res;
84 }
85
86 void Condition::Set(bool bCondition) {
87 dmsg(7,("Condition::Set() -> LOCK()\n"));
88 Lock();
89 dmsg(7,("Condition::Set() -> LOCK() passed\n"));
90 if (this->bCondition != bCondition) {
91 this->bCondition = bCondition;
92 if (bCondition) {
93 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 Unlock();
102 }

  ViewVC Help
Powered by ViewVC