/[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 417 - (show annotations) (download)
Tue Mar 1 21:55:51 2005 UTC (19 years, 1 month ago) by persson
File size: 5230 byte(s)
* fixed a bug in the low-level thread synchronizing code, that made
  loading of very small gig files sometimes fail

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

  ViewVC Help
Powered by ViewVC