Jack2: JackPosixProcessSync.cpp Source File

Jack2 1.9.12

JackPosixProcessSync.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3
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.
8
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.
13
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.
17
18 */
19
20 #include "JackPosixProcessSync.h"
21 #include "JackError.h"
22
23 namespace Jack
24 {
25
26 void JackPosixProcessSync::Signal()
27 {
28 int res = pthread_cond_signal(&fCond);
29 if (res != 0) {
30 jack_error("JackPosixProcessSync::Signal error err = %s", strerror(res));
31 }
32 }
33
34 // TO DO : check thread consistency?
35 void JackPosixProcessSync::LockedSignal()
36 {
37 int res = pthread_mutex_lock(&fMutex);
38 if (res != 0) {
39 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
40 }
41 res = pthread_cond_signal(&fCond);
42 if (res != 0) {
43 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
44 }
45 res = pthread_mutex_unlock(&fMutex);
46 if (res != 0) {
47 jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
48 }
49 }
50
51 void JackPosixProcessSync::SignalAll()
52 {
53 int res = pthread_cond_broadcast(&fCond);
54 if (res != 0) {
55 jack_error("JackPosixProcessSync::SignalAll error err = %s", strerror(res));
56 }
57 }
58
59 // TO DO : check thread consistency?
60 void JackPosixProcessSync::LockedSignalAll()
61 {
62 int res = pthread_mutex_lock(&fMutex);
63 if (res != 0) {
64 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
65 }
66 res = pthread_cond_broadcast(&fCond);
67 if (res != 0) {
68 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
69 }
70 res = pthread_mutex_unlock(&fMutex);
71 if (res != 0) {
72 jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
73 }
74 }
75
76 void JackPosixProcessSync::Wait()
77 {
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;
80
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();
86 }
87 }
88
89 // TO DO : check thread consistency?
90 void JackPosixProcessSync::LockedWait()
91 {
92 int res;
93 res = pthread_mutex_lock(&fMutex);
94 if (res != 0) {
95 jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
96 }
97 if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0) {
98 jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
99 }
100 res = pthread_mutex_unlock(&fMutex);
101 if (res != 0) {
102 jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
103 }
104 }
105
106 bool JackPosixProcessSync::TimedWait(long usec)
107 {
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;
110
111 struct timeval T0, T1;
112 timespec time;
113 struct timeval now;
114 int res;
115
116 jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec);
117 gettimeofday(&T0, 0);
118
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;
123
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();
129 }
130
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));
134
135 return (res == 0);
136 }
137
138 // TO DO : check thread consistency?
139 bool JackPosixProcessSync::LockedTimedWait(long usec)
140 {
141 struct timeval T0, T1;
142 timespec time;
143 struct timeval now;
144 int res1, res2;
145
146 res1 = pthread_mutex_lock(&fMutex);
147 if (res1 != 0) {
148 jack_error("JackPosixProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
149 }
150
151 jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec);
152 gettimeofday(&T0, 0);
153
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));
161 }
162
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));
167 }
168
169 jack_log("JackPosixProcessSync::TimedWait finished delta = %5.1lf",
170 (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
171
172 return (res2 == 0);
173 }
174
175
176 } // end of namespace
177
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108