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 "JackWinProcessSync.h"
21 #include "JackError.h"
26 void JackWinProcessSync::Signal()
28 if (!SetEvent(fEvent
)) {
29 jack_error("JackWinProcessSync::Signal SetEvent err = %d", GetLastError());
33 void JackWinProcessSync::LockedSignal()
35 DWORD res
= WaitForSingleObject(fMutex
, INFINITE
);
36 if (res
!= WAIT_OBJECT_0
) {
37 jack_error("JackWinProcessSync::LockedSignal WaitForSingleObject err = %d", GetLastError());
39 if (!SetEvent(fEvent
)) {
40 jack_error("JackWinProcessSync::LockedSignal SetEvent err = %d", GetLastError());
42 if (!ReleaseMutex(fMutex
)) {
43 jack_error("JackWinProcessSync::LockedSignal ReleaseMutex err = %d", GetLastError());
47 void JackWinProcessSync::SignalAll()
52 void JackWinProcessSync::LockedSignalAll()
58 void JackWinProcessSync::Wait()
60 if (!ReleaseMutex(fMutex)) {
61 jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
63 DWORD res = WaitForSingleObject(fEvent, INFINITE);
64 if (res != WAIT_OBJECT_0) {
65 jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
70 void JackWinProcessSync::LockedWait()
72 // Does it make sense on Windows, use non-locked version for now...
77 bool JackWinProcessSync::TimedWait(long usec)
79 if (!ReleaseMutex(fMutex)) {
80 jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
83 DWORD res = WaitForSingleObject(fEvent, usec / 1000);
84 if (res != WAIT_OBJECT_0) {
85 jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
88 return (res == WAIT_OBJECT_0);
91 bool JackWinProcessSync::LockedTimedWait(long usec
)
93 // Does it make sense on Windows, use non-locked version for now...
94 return TimedWait(usec
);
97 void JackWinProcessSync::Wait()
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());
106 // In case Wait is called in a "non-locked" context
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());
115 if (!ResetEvent(fEvent
)) {
116 jack_error("JackWinProcessSync::Wait ResetEvent err = %d", GetLastError());
120 bool JackWinProcessSync::TimedWait(long usec
)
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());
131 // In case TimedWait is called in a "non-locked" context
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());
140 if (!ResetEvent(fEvent
)) {
141 jack_error("JackWinProcessSync::TimedWait ResetEvent err = %d", GetLastError());
144 return (res
== WAIT_OBJECT_0
);
148 // Code from APPLE CAGuard.cpp : does not seem to work as expected...
150 void JackWinProcessSync::Wait()
152 if (!ReleaseMutex(fMutex)) {
153 jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
155 DWORD res = WaitForSingleObject(fEvent, INFINITE);
156 if (res != WAIT_OBJECT_0) {
157 jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
161 // Variant that behaves differently depending of the mutex state
162 void JackWinProcessSync::Wait()
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());
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());
178 if (!ResetEvent(fEvent)) {
179 jack_error("JackWinProcessSync::LockedWait ResetEvent err = %d", GetLastError());
183 void JackWinProcessSync::LockedWait()
185 if (!ReleaseMutex(fMutex)) {
186 jack_error("JackWinProcessSync::LockedWait ReleaseMutex err = %d", GetLastError());
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());
195 if (!ResetEvent(fEvent)) {
196 jack_error("JackWinProcessSync::LockedWait ResetEvent err = %d", GetLastError());
200 bool JackWinProcessSync::TimedWait(long usec)
202 if (!ReleaseMutex(fMutex)) {
203 jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
206 DWORD res = WaitForSingleObject(fEvent, usec / 1000);
207 if (res != WAIT_OBJECT_0) {
208 jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
211 return (res == WAIT_OBJECT_0);
214 // Variant that behaves differently depending of the mutex state
215 bool JackWinProcessSync::TimedWait(long usec)
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());
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());
231 if (!ResetEvent(fEvent)) {
232 jack_error("JackWinProcessSync::LockedTimedWait ResetEvent err = %d", GetLastError());
235 return (res == WAIT_OBJECT_0);
238 bool JackWinProcessSync::LockedTimedWait(long usec)
240 if (!ReleaseMutex(fMutex)) {
241 jack_error("JackWinProcessSync::LockedTimedWait ReleaseMutex err = %d", GetLastError());
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());
250 if (!ResetEvent(fEvent)) {
251 jack_error("JackWinProcessSync::LockedTimedWait ResetEvent err = %d", GetLastError());
254 return (res == WAIT_OBJECT_0);
258 } // end of namespace