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 void JackProcessSync::LockedSignal()
35 int res
= pthread_mutex_lock(&fMutex
);
37 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res
));
38 res
= pthread_cond_signal(&fCond
);
40 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res
));
41 res
= pthread_mutex_unlock(&fMutex
);
43 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res
));
46 void JackProcessSync::SignalAll()
48 int res
= pthread_cond_broadcast(&fCond
);
50 jack_error("JackProcessSync::SignalAll error err = %s", strerror(res
));
53 void JackProcessSync::LockedSignalAll()
55 int res
= pthread_mutex_lock(&fMutex
);
57 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res
));
58 res
= pthread_cond_broadcast(&fCond
);
60 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res
));
61 res
= pthread_mutex_unlock(&fMutex
);
63 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res
));
66 void JackProcessSync::Wait()
69 if ((res
= pthread_cond_wait(&fCond
, &fMutex
)) != 0)
70 jack_error("JackProcessSync::Wait error err = %s", strerror(res
));
73 void JackProcessSync::LockedWait()
76 res
= pthread_mutex_lock(&fMutex
);
78 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res
));
79 if ((res
= pthread_cond_wait(&fCond
, &fMutex
)) != 0)
80 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res
));
81 res
= pthread_mutex_unlock(&fMutex
);
83 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res
));
86 bool JackProcessSync::TimedWait(long usec
)
88 struct timeval T0
, T1
;
93 jack_log("JackProcessSync::TimedWait time out = %ld", usec
);
96 gettimeofday(&now
, 0);
97 unsigned int next_date_usec
= now
.tv_usec
+ usec
;
98 time
.tv_sec
= now
.tv_sec
+ (next_date_usec
/ 1000000);
99 time
.tv_nsec
= (next_date_usec
% 1000000) * 1000;
100 res
= pthread_cond_timedwait(&fCond
, &fMutex
, &time
);
102 jack_error("JackProcessSync::TimedWait error usec = %ld err = %s", usec
, strerror(res
));
104 gettimeofday(&T1
, 0);
105 jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
106 (1e6
* T1
.tv_sec
- 1e6
* T0
.tv_sec
+ T1
.tv_usec
- T0
.tv_usec
));
111 bool JackProcessSync::LockedTimedWait(long usec
)
113 struct timeval T0
, T1
;
118 res1
= pthread_mutex_lock(&fMutex
);
120 jack_error("JackProcessSync::LockedTimedWait error err = %s", usec
, strerror(res1
));
122 jack_log("JackProcessSync::TimedWait time out = %ld", usec
);
123 gettimeofday(&T0
, 0);
125 gettimeofday(&now
, 0);
126 unsigned int next_date_usec
= now
.tv_usec
+ usec
;
127 time
.tv_sec
= now
.tv_sec
+ (next_date_usec
/ 1000000);
128 time
.tv_nsec
= (next_date_usec
% 1000000) * 1000;
129 res2
= pthread_cond_timedwait(&fCond
, &fMutex
, &time
);
131 jack_error("JackProcessSync::LockedTimedWait error usec = %ld err = %s", usec
, strerror(res2
));
133 gettimeofday(&T1
, 0);
134 res1
= pthread_mutex_unlock(&fMutex
);
136 jack_error("JackProcessSync::LockedTimedWait error err = %s", usec
, strerror(res1
));
138 jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
139 (1e6
* T1
.tv_sec
- 1e6
* T0
.tv_sec
+ T1
.tv_usec
- T0
.tv_usec
));
145 } // end of namespace