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 "JackProcessSync.h"
21 #include "JackError.h"
26 void JackProcessSync::Signal()
28 int res
= pthread_cond_signal(&fCond
);
30 jack_error("JackProcessSync::Signal error err = %s", strerror(res
));
33 // TO DO : check thread consistency?
34 void JackProcessSync::LockedSignal()
36 int res
= pthread_mutex_lock(&fMutex
);
38 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res
));
39 res
= pthread_cond_signal(&fCond
);
41 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res
));
42 res
= pthread_mutex_unlock(&fMutex
);
44 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res
));
47 void JackProcessSync::SignalAll()
49 int res
= pthread_cond_broadcast(&fCond
);
51 jack_error("JackProcessSync::SignalAll error err = %s", strerror(res
));
54 // TO DO : check thread consistency?
55 void JackProcessSync::LockedSignalAll()
57 int res
= pthread_mutex_lock(&fMutex
);
59 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res
));
60 res
= pthread_cond_broadcast(&fCond
);
62 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res
));
63 res
= pthread_mutex_unlock(&fMutex
);
65 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res
));
68 void JackProcessSync::Wait()
70 ThrowIf(!pthread_equal(pthread_self(), fOwner
), JackException("JackProcessSync::Wait: a thread has to have locked a mutex before it can wait"));
73 int res
= pthread_cond_wait(&fCond
, &fMutex
);
75 jack_error("JackProcessSync::Wait error err = %s", strerror(res
));
77 fOwner
= pthread_self();
81 // TO DO : check thread consistency?
82 void JackProcessSync::LockedWait()
85 res
= pthread_mutex_lock(&fMutex
);
87 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res
));
88 if ((res
= pthread_cond_wait(&fCond
, &fMutex
)) != 0)
89 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res
));
90 res
= pthread_mutex_unlock(&fMutex
);
92 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res
));
95 bool JackProcessSync::TimedWait(long usec
)
97 ThrowIf(!pthread_equal(pthread_self(), fOwner
), JackException("JackProcessSync::TimedWait: a thread has to have locked a mutex before it can wait"));
100 struct timeval T0
, T1
;
105 jack_log("JackProcessSync::TimedWait time out = %ld", usec
);
106 gettimeofday(&T0
, 0);
108 gettimeofday(&now
, 0);
109 unsigned int next_date_usec
= now
.tv_usec
+ usec
;
110 time
.tv_sec
= now
.tv_sec
+ (next_date_usec
/ 1000000);
111 time
.tv_nsec
= (next_date_usec
% 1000000) * 1000;
113 res
= pthread_cond_timedwait(&fCond
, &fMutex
, &time
);
115 jack_error("JackProcessSync::TimedWait error usec = %ld err = %s", usec
, strerror(res
));
117 fOwner
= pthread_self();
120 gettimeofday(&T1
, 0);
121 jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
122 (1e6
* T1
.tv_sec
- 1e6
* T0
.tv_sec
+ T1
.tv_usec
- T0
.tv_usec
));
127 // TO DO : check thread consistency?
128 bool JackProcessSync::LockedTimedWait(long usec
)
130 struct timeval T0
, T1
;
135 res1
= pthread_mutex_lock(&fMutex
);
137 jack_error("JackProcessSync::LockedTimedWait error err = %s", usec
, strerror(res1
));
139 jack_log("JackProcessSync::TimedWait time out = %ld", usec
);
140 gettimeofday(&T0
, 0);
142 gettimeofday(&now
, 0);
143 unsigned int next_date_usec
= now
.tv_usec
+ usec
;
144 time
.tv_sec
= now
.tv_sec
+ (next_date_usec
/ 1000000);
145 time
.tv_nsec
= (next_date_usec
% 1000000) * 1000;
146 res2
= pthread_cond_timedwait(&fCond
, &fMutex
, &time
);
148 jack_error("JackProcessSync::LockedTimedWait error usec = %ld err = %s", usec
, strerror(res2
));
150 gettimeofday(&T1
, 0);
151 res1
= pthread_mutex_unlock(&fMutex
);
153 jack_error("JackProcessSync::LockedTimedWait error err = %s", usec
, strerror(res1
));
155 jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
156 (1e6
* T1
.tv_sec
- 1e6
* T0
.tv_sec
+ T1
.tv_usec
- T0
.tv_usec
));
162 } // end of namespace