Jack2  1.9.9
JackWinProcessSync.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 "JackWinProcessSync.h"
21 #include "JackError.h"
22 
23 namespace Jack
24 {
25 
26 void JackWinProcessSync::Signal()
27 {
28  if (!SetEvent(fEvent)) {
29  jack_error("JackWinProcessSync::Signal SetEvent err = %d", GetLastError());
30  }
31 }
32 
33 void JackWinProcessSync::LockedSignal()
34 {
35  DWORD res = WaitForSingleObject(fMutex, INFINITE);
36  if (res != WAIT_OBJECT_0) {
37  jack_error("JackWinProcessSync::LockedSignal WaitForSingleObject err = %d", GetLastError());
38  }
39  if (!SetEvent(fEvent)) {
40  jack_error("JackWinProcessSync::LockedSignal SetEvent err = %d", GetLastError());
41  }
42  if (!ReleaseMutex(fMutex)) {
43  jack_error("JackWinProcessSync::LockedSignal ReleaseMutex err = %d", GetLastError());
44  }
45 }
46 
47 void JackWinProcessSync::SignalAll()
48 {
49  Signal();
50 }
51 
52 void JackWinProcessSync::LockedSignalAll()
53 {
54  LockedSignal();
55 }
56 
57 /*
58 void JackWinProcessSync::Wait()
59 {
60  if (!ReleaseMutex(fMutex)) {
61  jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
62  }
63  DWORD res = WaitForSingleObject(fEvent, INFINITE);
64  if (res != WAIT_OBJECT_0) {
65  jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
66  }
67 }
68 */
69 
70 void JackWinProcessSync::LockedWait()
71 {
72  // Does it make sense on Windows, use non-locked version for now...
73  Wait();
74 }
75 
76 /*
77 bool JackWinProcessSync::TimedWait(long usec)
78 {
79  if (!ReleaseMutex(fMutex)) {
80  jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
81  }
82 
83  DWORD res = WaitForSingleObject(fEvent, usec / 1000);
84  if (res != WAIT_OBJECT_0) {
85  jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
86  }
87 
88  return (res == WAIT_OBJECT_0);
89 }
90 */
91 bool JackWinProcessSync::LockedTimedWait(long usec)
92 {
93  // Does it make sense on Windows, use non-locked version for now...
94  return TimedWait(usec);
95 }
96 
97 void JackWinProcessSync::Wait()
98 {
99  // In case Wait is called in a "locked" context
100  if (ReleaseMutex(fMutex)) {
101  HANDLE handles[] = { fMutex, fEvent };
102  DWORD res = WaitForMultipleObjects(2, handles, true, INFINITE);
103  if (res != WAIT_OBJECT_0) {
104  jack_error("JackWinProcessSync::Wait WaitForMultipleObjects err = %d", GetLastError());
105  }
106  // In case Wait is called in a "non-locked" context
107  } else {
108  jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
109  DWORD res = WaitForSingleObject(fEvent, INFINITE);
110  if (res != WAIT_OBJECT_0) {
111  jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
112  }
113  }
114 
115  if (!ResetEvent(fEvent)) {
116  jack_error("JackWinProcessSync::Wait ResetEvent err = %d", GetLastError());
117  }
118 }
119 
120 bool JackWinProcessSync::TimedWait(long usec)
121 {
122  DWORD res = 0;
123 
124  // In case TimedWait is called in a "locked" context
125  if (ReleaseMutex(fMutex)) {
126  HANDLE handles[] = { fMutex, fEvent };
127  res = WaitForMultipleObjects(2, handles, true, usec / 1000);
128  if ((res != WAIT_OBJECT_0) && (res != WAIT_TIMEOUT)) {
129  jack_error("JackWinProcessSync::TimedWait WaitForMultipleObjects err = %d", GetLastError());
130  }
131  // In case TimedWait is called in a "non-locked" context
132  } else {
133  jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
134  res = WaitForSingleObject(fEvent, usec / 1000);
135  if (res != WAIT_OBJECT_0) {
136  jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
137  }
138  }
139 
140  if (!ResetEvent(fEvent)) {
141  jack_error("JackWinProcessSync::TimedWait ResetEvent err = %d", GetLastError());
142  }
143 
144  return (res == WAIT_OBJECT_0);
145 }
146 
147 /*
148 // Code from APPLE CAGuard.cpp : does not seem to work as expected...
149 
150 void JackWinProcessSync::Wait()
151 {
152  if (!ReleaseMutex(fMutex)) {
153  jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
154  }
155  DWORD res = WaitForSingleObject(fEvent, INFINITE);
156  if (res != WAIT_OBJECT_0) {
157  jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
158  }
159 }
160 
161 // Variant that behaves differently depending of the mutex state
162 void JackWinProcessSync::Wait()
163 {
164  if (ReleaseMutex(fMutex)) {
165  HANDLE handles[] = { fMutex, fEvent };
166  DWORD res = WaitForMultipleObjects(2, handles, true, INFINITE);
167  if (res != WAIT_OBJECT_0) {
168  jack_error("JackWinProcessSync::LockedWait WaitForMultipleObjects err = %d", GetLastError());
169  }
170  } else {
171  jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
172  DWORD res = WaitForSingleObject(fEvent, INFINITE);
173  if (res != WAIT_OBJECT_0) {
174  jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
175  }
176  }
177 
178  if (!ResetEvent(fEvent)) {
179  jack_error("JackWinProcessSync::LockedWait ResetEvent err = %d", GetLastError());
180  }
181 }
182 
183 void JackWinProcessSync::LockedWait()
184 {
185  if (!ReleaseMutex(fMutex)) {
186  jack_error("JackWinProcessSync::LockedWait ReleaseMutex err = %d", GetLastError());
187  }
188 
189  HANDLE handles[] = { fMutex, fEvent };
190  DWORD res = WaitForMultipleObjects(2, handles, true, INFINITE);
191  if (res != WAIT_OBJECT_0) {
192  jack_error("JackWinProcessSync::LockedWait WaitForMultipleObjects err = %d", GetLastError());
193  }
194 
195  if (!ResetEvent(fEvent)) {
196  jack_error("JackWinProcessSync::LockedWait ResetEvent err = %d", GetLastError());
197  }
198 }
199 
200 bool JackWinProcessSync::TimedWait(long usec)
201 {
202  if (!ReleaseMutex(fMutex)) {
203  jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
204  }
205 
206  DWORD res = WaitForSingleObject(fEvent, usec / 1000);
207  if (res != WAIT_OBJECT_0) {
208  jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
209  }
210 
211  return (res == WAIT_OBJECT_0);
212 }
213 
214 // Variant that behaves differently depending of the mutex state
215 bool JackWinProcessSync::TimedWait(long usec)
216 {
217  if (ReleaseMutex(fMutex)) {
218  HANDLE handles[] = { fMutex, fEvent };
219  DWORD res = WaitForMultipleObjects(2, handles, true, usec / 1000);
220  if ((res != WAIT_OBJECT_0) && (res != WAIT_TIMEOUT)) {
221  jack_error("JackWinProcessSync::LockedTimedWait WaitForMultipleObjects err = %d", GetLastError());
222  }
223  } else {
224  jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
225  DWORD res = WaitForSingleObject(fEvent, usec / 1000);
226  if (res != WAIT_OBJECT_0) {
227  jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
228  }
229  }
230 
231  if (!ResetEvent(fEvent)) {
232  jack_error("JackWinProcessSync::LockedTimedWait ResetEvent err = %d", GetLastError());
233  }
234 
235  return (res == WAIT_OBJECT_0);
236 }
237 
238 bool JackWinProcessSync::LockedTimedWait(long usec)
239 {
240  if (!ReleaseMutex(fMutex)) {
241  jack_error("JackWinProcessSync::LockedTimedWait ReleaseMutex err = %d", GetLastError());
242  }
243 
244  HANDLE handles[] = { fMutex, fEvent };
245  DWORD res = WaitForMultipleObjects(2, handles, true, usec / 1000);
246  if ((res != WAIT_OBJECT_0) && (res != WAIT_TIMEOUT)) {
247  jack_error("JackWinProcessSync::LockedTimedWait WaitForMultipleObjects err = %d", GetLastError());
248  }
249 
250  if (!ResetEvent(fEvent)) {
251  jack_error("JackWinProcessSync::LockedTimedWait ResetEvent err = %d", GetLastError());
252  }
253 
254  return (res == WAIT_OBJECT_0);
255 }
256 */
257 
258 } // end of namespace
259 
260 
261 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91