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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (hide 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 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     #include "Condition.h"
24    
25 schoenebeck 63 #include "global.h"
26    
27 schoenebeck 53 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 schoenebeck 63 dmsg(7,("Condition::Waitif() -> LOCK()\n"));
40 schoenebeck 53 Lock();
41 schoenebeck 63 dmsg(7,("Condition::Waitif() -> LOCK() passed\n"));
42 schoenebeck 53 int res = 0;
43     if (this->bCondition == bCondition) {
44 schoenebeck 63 if (bCondition) { // wait until condition turned 'false'
45     if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
46 schoenebeck 53 timespec timeout;
47     timeout.tv_sec = TimeoutSeconds;
48     timeout.tv_nsec = TimeoutNanoSeconds;
49 schoenebeck 63 dmsg(7,("Condition::Waitif() -> waiting for 'false' condition with timeout\n"));
50 schoenebeck 53 res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout);
51 schoenebeck 63 dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n"));
52 schoenebeck 53 }
53 schoenebeck 63 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 schoenebeck 53 }
59 schoenebeck 63 else { // wait until condition turned 'true'
60     if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
61 schoenebeck 53 timespec timeout;
62     timeout.tv_sec = TimeoutSeconds;
63     timeout.tv_nsec = TimeoutNanoSeconds;
64 schoenebeck 63 dmsg(7,("Condition::Waitif() -> waiting for 'true' condition with timeout\n"));
65 schoenebeck 53 res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout);
66 schoenebeck 63 dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n"));
67 schoenebeck 53 }
68 schoenebeck 63 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 schoenebeck 53 }
74     }
75     return res;
76     }
77    
78     int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) {
79     int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds);
80 schoenebeck 63 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK()\n"));
81 schoenebeck 53 Unlock();
82 schoenebeck 63 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK() passed\n"));
83 schoenebeck 53 return res;
84     }
85    
86     void Condition::Set(bool bCondition) {
87 schoenebeck 63 dmsg(7,("Condition::Set() -> LOCK()\n"));
88 schoenebeck 53 Lock();
89 schoenebeck 63 dmsg(7,("Condition::Set() -> LOCK() passed\n"));
90 schoenebeck 53 if (this->bCondition != bCondition) {
91     this->bCondition = bCondition;
92 schoenebeck 63 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 schoenebeck 53 }
101 schoenebeck 63 Unlock();
102 schoenebeck 53 }

  ViewVC Help
Powered by ViewVC