/[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 1221 - (show annotations) (download)
Wed Jun 6 18:50:03 2007 UTC (16 years, 10 months ago) by schoenebeck
File size: 5418 byte(s)
* fixed several issues in fundamental "Thread" class: set scheduling
  policy and priority on thread level, set a minimum stack size for
  thread (TODO: a reasonable value yet to be tested), bugfix: non-RT
  threads simply inherited properties of starting thread instead of
  setting their own policy and priority
* updated and fixed test cases (haven't been touched in a while, but
  are now all running successfully through all cases)

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #include <sys/time.h>
25
26 #include "Condition.h"
27
28 #include "global.h"
29
30 namespace LinuxSampler {
31
32 Condition::Condition(bool bInitialCondition) {
33 pthread_cond_init(&__posix_true_condition, NULL);
34 pthread_cond_init(&__posix_false_condition, NULL);
35 bCondition = bInitialCondition;
36 }
37
38 Condition::~Condition() {
39 pthread_cond_destroy(&__posix_true_condition);
40 pthread_cond_destroy(&__posix_false_condition);
41 }
42
43 int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) {
44 dmsg(7,("Condition::Waitif() -> LOCK()\n"));
45 Lock();
46 dmsg(7,("Condition::Waitif() -> LOCK() passed\n"));
47 int res = 0;
48 if (this->bCondition == bCondition) {
49 if (bCondition) { // wait until condition turned 'false'
50 if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
51 struct timeval now;
52 gettimeofday(&now, 0);
53 timespec timeout;
54 timeout.tv_sec = now.tv_sec + TimeoutSeconds;
55 timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds;
56 dmsg(7,("Condition::Waitif() -> waiting for 'false' condition with timeout\n"));
57 res = pthread_cond_timedwait(&__posix_false_condition, &__posix_mutex, &timeout);
58 dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n"));
59 }
60 else { // wait without timeout
61 dmsg(7,("Condition::Waitif() -> waiting for 'false' condition\n"));
62 pthread_cond_wait(&__posix_false_condition, &__posix_mutex);
63 dmsg(7,("Condition::Waitif() -> awakened from 'false' condition waiting\n"));
64 }
65 }
66 else { // wait until condition turned 'true'
67 if (TimeoutSeconds || TimeoutNanoSeconds) { // wait with timeout
68 struct timeval now;
69 gettimeofday(&now, 0);
70 timespec timeout;
71 timeout.tv_sec = now.tv_sec + TimeoutSeconds;
72 timeout.tv_nsec = now.tv_usec * 1000 + TimeoutNanoSeconds;
73 dmsg(7,("Condition::Waitif() -> waiting for 'true' condition with timeout\n"));
74 res = pthread_cond_timedwait(&__posix_true_condition, &__posix_mutex, &timeout);
75 dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n"));
76 }
77 else { // wait without timeout
78 dmsg(7,("Condition::Waitif() -> waiting for 'true' condition\n"));
79 pthread_cond_wait(&__posix_true_condition, &__posix_mutex);
80 dmsg(7,("Condition::Waitif() -> awakened from 'true' condition waiting\n"));
81 }
82 }
83 }
84 return res;
85 }
86
87 int Condition::WaitAndUnlockIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) {
88 int res = WaitIf(bCondition, TimeoutSeconds, TimeoutNanoSeconds);
89 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK()\n"));
90 Unlock();
91 dmsg(7,("Condition::WaitAndUnlockIf() -> UNLOCK() passed\n"));
92 return res;
93 }
94
95 void Condition::Set(bool bCondition) {
96 dmsg(7,("Condition::Set() -> LOCK()\n"));
97 Lock();
98 dmsg(7,("Condition::Set() -> LOCK() passed\n"));
99 if (this->bCondition != bCondition) {
100 this->bCondition = bCondition;
101 if (bCondition) {
102 dmsg(7,("Condition::Set() -> broadcasting 'true' condition\n"));
103 pthread_cond_broadcast(&__posix_true_condition);
104 }
105 else {
106 dmsg(7,("Condition::Set() -> broadcasting 'false' condition\n"));
107 pthread_cond_broadcast(&__posix_false_condition);
108 }
109 }
110 Unlock();
111 }
112
113 bool Condition::GetUnsafe() {
114 return bCondition;
115 }
116
117 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC