Add missing methods in JackDebugClient(2).
[jack2.git] / posix / JackPosixProcessSync.cpp
blob04c8785f436630503aaea290ed484c722de7941e
1 /*
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"
23 namespace Jack
26 void JackPosixProcessSync::Signal()
28 int res = pthread_cond_signal(&fCond);
29 if (res != 0) {
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);
38 if (res != 0) {
39 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
41 res = pthread_cond_signal(&fCond);
42 if (res != 0) {
43 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
45 res = pthread_mutex_unlock(&fMutex);
46 if (res != 0) {
47 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
51 void JackPosixProcessSync::SignalAll()
53 int res = pthread_cond_broadcast(&fCond);
54 if (res != 0) {
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);
63 if (res != 0) {
64 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
66 res = pthread_cond_broadcast(&fCond);
67 if (res != 0) {
68 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
70 res = pthread_mutex_unlock(&fMutex);
71 if (res != 0) {
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"));
79 fOwner = 0;
81 int res = pthread_cond_wait(&fCond, &fMutex);
82 if (res != 0) {
83 jack_error("JackPosixProcessSync::Wait error err = %s", strerror(res));
84 } else {
85 fOwner = pthread_self();
89 // TO DO : check thread consistency?
90 void JackPosixProcessSync::LockedWait()
92 int res;
93 res = pthread_mutex_lock(&fMutex);
94 if (res != 0) {
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);
101 if (res != 0) {
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"));
109 fOwner = 0;
111 struct timeval T0, T1;
112 timespec time;
113 struct timeval now;
114 int res;
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);
125 if (res != 0) {
126 jack_error("JackPosixProcessSync::TimedWait error usec = %ld err = %s", usec, strerror(res));
127 } else {
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));
135 return (res == 0);
138 // TO DO : check thread consistency?
139 bool JackPosixProcessSync::LockedTimedWait(long usec)
141 struct timeval T0, T1;
142 timespec time;
143 struct timeval now;
144 int res1, res2;
146 res1 = pthread_mutex_lock(&fMutex);
147 if (res1 != 0) {
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);
159 if (res2 != 0) {
160 jack_error("JackPosixProcessSync::LockedTimedWait error usec = %ld err = %s", usec, strerror(res2));
163 gettimeofday(&T1, 0);
164 res1 = pthread_mutex_unlock(&fMutex);
165 if (res1 != 0) {
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));
172 return (res2 == 0);
176 } // end of namespace