2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackPosixProcessSync.h"
21 #include "JackError.h"
26 void JackPosixProcessSync::Signal()
28 int res
= pthread_cond_signal(&fCond
);
30 jack_error("JackPosixProcessSync::Signal error err = %s", strerror(res
));
34 // TO DO : check thread consistency?
35 void JackPosixProcessSync::LockedSignal()
37 int res
= pthread_mutex_lock(&fMutex
);
39 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res
));
41 res
= pthread_cond_signal(&fCond
);
43 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res
));
45 res
= pthread_mutex_unlock(&fMutex
);
47 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res
));
51 void JackPosixProcessSync::SignalAll()
53 int res
= pthread_cond_broadcast(&fCond
);
55 jack_error("JackPosixProcessSync::SignalAll error err = %s", strerror(res
));
59 // TO DO : check thread consistency?
60 void JackPosixProcessSync::LockedSignalAll()
62 int res
= pthread_mutex_lock(&fMutex
);
64 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res
));
66 res
= pthread_cond_broadcast(&fCond
);
68 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res
));
70 res
= pthread_mutex_unlock(&fMutex
);
72 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res
));
76 void JackPosixProcessSync::Wait()
78 ThrowIf(!pthread_equal(pthread_self(), fOwner
), JackException("JackPosixProcessSync::Wait: a thread has to have locked a mutex before it can wait"));
81 int res
= pthread_cond_wait(&fCond
, &fMutex
);
83 jack_error("JackPosixProcessSync::Wait error err = %s", strerror(res
));
85 fOwner
= pthread_self();
89 // TO DO : check thread consistency?
90 void JackPosixProcessSync::LockedWait()
93 res
= pthread_mutex_lock(&fMutex
);
95 jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res
));
97 if ((res
= pthread_cond_wait(&fCond
, &fMutex
)) != 0) {
98 jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res
));
100 res
= pthread_mutex_unlock(&fMutex
);
102 jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res
));
106 bool JackPosixProcessSync::TimedWait(long usec
)
108 ThrowIf(!pthread_equal(pthread_self(), fOwner
), JackException("JackPosixProcessSync::TimedWait: a thread has to have locked a mutex before it can wait"));
111 struct timeval T0
, T1
;
116 jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec
);
117 gettimeofday(&T0
, 0);
119 gettimeofday(&now
, 0);
120 unsigned int next_date_usec
= now
.tv_usec
+ usec
;
121 time
.tv_sec
= now
.tv_sec
+ (next_date_usec
/ 1000000);
122 time
.tv_nsec
= (next_date_usec
% 1000000) * 1000;
124 res
= pthread_cond_timedwait(&fCond
, &fMutex
, &time
);
126 jack_error("JackPosixProcessSync::TimedWait error usec = %ld err = %s", usec
, strerror(res
));
128 fOwner
= pthread_self();
131 gettimeofday(&T1
, 0);
132 jack_log("JackPosixProcessSync::TimedWait finished delta = %5.1lf",
133 (1e6
* T1
.tv_sec
- 1e6
* T0
.tv_sec
+ T1
.tv_usec
- T0
.tv_usec
));
138 // TO DO : check thread consistency?
139 bool JackPosixProcessSync::LockedTimedWait(long usec
)
141 struct timeval T0
, T1
;
146 res1
= pthread_mutex_lock(&fMutex
);
148 jack_error("JackPosixProcessSync::LockedTimedWait error err = %s", usec
, strerror(res1
));
151 jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec
);
152 gettimeofday(&T0
, 0);
154 gettimeofday(&now
, 0);
155 unsigned int next_date_usec
= now
.tv_usec
+ usec
;
156 time
.tv_sec
= now
.tv_sec
+ (next_date_usec
/ 1000000);
157 time
.tv_nsec
= (next_date_usec
% 1000000) * 1000;
158 res2
= pthread_cond_timedwait(&fCond
, &fMutex
, &time
);
160 jack_error("JackPosixProcessSync::LockedTimedWait error usec = %ld err = %s", usec
, strerror(res2
));
163 gettimeofday(&T1
, 0);
164 res1
= pthread_mutex_unlock(&fMutex
);
166 jack_error("JackPosixProcessSync::LockedTimedWait error err = %s", usec
, strerror(res1
));
169 jack_log("JackPosixProcessSync::TimedWait finished delta = %5.1lf",
170 (1e6
* T1
.tv_sec
- 1e6
* T0
.tv_sec
+ T1
.tv_usec
- T0
.tv_usec
));
176 } // end of namespace