/[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 417 - (hide 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 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 persson 417 #include <sys/time.h>
24    
25 schoenebeck 53 #include "Condition.h"
26    
27 schoenebeck 63 #include "global.h"
28    
29 schoenebeck 53 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 schoenebeck 63 dmsg(7,("Condition::Waitif() -> LOCK()\n"));
42 schoenebeck 53 Lock();
43 schoenebeck 63 dmsg(7,("Condition::Waitif() -> LOCK() passed\n"));
44 schoenebeck 53 int res = 0;
45     if (this->bCondition == bCondition) {
46 schoenebeck 63 if (bCondition) { // wait until condition turned 'false'
47     if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
48 persson 417 struct timeval now;
49     gettimeofday(&now, 0);
50 schoenebeck 53 timespec timeout;
51 persson 417 timeout.tv_sec = now.tv_sec + TimeoutSeconds;
52     timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds;
53 schoenebeck 63 dmsg(7,("Condition::Waitif() -> waiting for 'false' condition with timeout\n"));
54 schoenebeck 53 res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout);
55 schoenebeck 63 dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n"));
56 schoenebeck 53 }
57 schoenebeck 63 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 schoenebeck 53 }
63 schoenebeck 63 else { // wait until condition turned 'true'
64     if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
65 persson 417 struct timeval now;
66     gettimeofday(&now, 0);
67 schoenebeck 53 timespec timeout;
68 persson 417 timeout.tv_sec = now.tv_sec + TimeoutSeconds;
69     timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds;
70 schoenebeck 63 dmsg(7,("Condition::Waitif() -> waiting for 'true' condition with timeout\n"));
71 schoenebeck 53 res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout);
72 schoenebeck 63 dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n"));
73 schoenebeck 53 }
74 schoenebeck 63 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 schoenebeck 53 }
80     }
81     return res;
82     }
83    
84     int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) {
85     int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds);
86 schoenebeck 63 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK()\n"));
87 schoenebeck 53 Unlock();
88 schoenebeck 63 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK() passed\n"));
89 schoenebeck 53 return res;
90     }
91    
92     void Condition::Set(bool bCondition) {
93 schoenebeck 63 dmsg(7,("Condition::Set() -> LOCK()\n"));
94 schoenebeck 53 Lock();
95 schoenebeck 63 dmsg(7,("Condition::Set() -> LOCK() passed\n"));
96 schoenebeck 53 if (this->bCondition != bCondition) {
97     this->bCondition = bCondition;
98 schoenebeck 63 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 schoenebeck 53 }
107 schoenebeck 63 Unlock();
108 schoenebeck 53 }

  ViewVC Help
Powered by ViewVC