Jack2  1.9.10
JackWinProcessSync.cpp
00001 /*
00002  Copyright (C) 2004-2008 Grame
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as published by
00006  the Free Software Foundation; either version 2.1 of the License, or
00007  (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public License
00015  along with this program; if not, write to the Free Software
00016  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018  */
00019 
00020 #include "JackWinProcessSync.h"
00021 #include "JackError.h"
00022 
00023 namespace Jack
00024 {
00025 
00026 void JackWinProcessSync::Signal()
00027 {
00028     if (!SetEvent(fEvent)) {
00029         jack_error("JackWinProcessSync::Signal SetEvent err = %d", GetLastError());
00030     }
00031 }
00032 
00033 void JackWinProcessSync::LockedSignal()
00034 {
00035     DWORD res = WaitForSingleObject(fMutex, INFINITE);
00036     if (res != WAIT_OBJECT_0) {
00037         jack_error("JackWinProcessSync::LockedSignal WaitForSingleObject err = %d", GetLastError());
00038     }
00039     if (!SetEvent(fEvent)) {
00040         jack_error("JackWinProcessSync::LockedSignal SetEvent err = %d", GetLastError());
00041     }
00042     if (!ReleaseMutex(fMutex)) {
00043         jack_error("JackWinProcessSync::LockedSignal ReleaseMutex err = %d", GetLastError());
00044     }
00045 }
00046 
00047 void JackWinProcessSync::SignalAll()
00048 {
00049     Signal();
00050 }
00051 
00052 void JackWinProcessSync::LockedSignalAll()
00053 {
00054     LockedSignal();
00055 }
00056 
00057 /*
00058 void JackWinProcessSync::Wait()
00059 {
00060     if (!ReleaseMutex(fMutex)) {
00061         jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
00062     }
00063         DWORD res = WaitForSingleObject(fEvent, INFINITE);
00064     if (res != WAIT_OBJECT_0) {
00065         jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
00066     }
00067 }
00068 */
00069 
00070 void JackWinProcessSync::LockedWait()
00071 {
00072     // Does it make sense on Windows, use non-locked version for now...
00073         Wait();
00074 }
00075 
00076 /*
00077 bool JackWinProcessSync::TimedWait(long usec)
00078 {
00079     if (!ReleaseMutex(fMutex)) {
00080         jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
00081     }
00082 
00083         DWORD res = WaitForSingleObject(fEvent, usec / 1000);
00084     if (res != WAIT_OBJECT_0) {
00085         jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
00086     }
00087 
00088         return (res == WAIT_OBJECT_0);
00089 }
00090 */
00091 bool JackWinProcessSync::LockedTimedWait(long usec)
00092 {
00093     // Does it make sense on Windows, use non-locked version for now...
00094     return TimedWait(usec);
00095 }
00096 
00097 void JackWinProcessSync::Wait()
00098 {
00099     // In case Wait is called in a "locked" context
00100     if (ReleaseMutex(fMutex)) {
00101         HANDLE handles[] = { fMutex, fEvent };
00102         DWORD res = WaitForMultipleObjects(2, handles, true, INFINITE);
00103         if (res != WAIT_OBJECT_0) {
00104             jack_error("JackWinProcessSync::Wait WaitForMultipleObjects err = %d", GetLastError());
00105         }
00106     // In case Wait is called in a "non-locked" context
00107     } else {
00108         jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
00109         DWORD res = WaitForSingleObject(fEvent, INFINITE);
00110         if (res != WAIT_OBJECT_0) {
00111             jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
00112         }
00113     }
00114 
00115     if (!ResetEvent(fEvent)) {
00116         jack_error("JackWinProcessSync::Wait ResetEvent err = %d", GetLastError());
00117     }
00118 }
00119 
00120 bool JackWinProcessSync::TimedWait(long usec)
00121 {
00122     DWORD res = 0;
00123 
00124     // In case TimedWait is called in a "locked" context
00125         if (ReleaseMutex(fMutex)) {
00126         HANDLE handles[] = { fMutex, fEvent };
00127         res = WaitForMultipleObjects(2, handles, true, usec / 1000);
00128         if ((res != WAIT_OBJECT_0) && (res != WAIT_TIMEOUT)) {
00129             jack_error("JackWinProcessSync::TimedWait WaitForMultipleObjects err = %d", GetLastError());
00130         }
00131     // In case TimedWait is called in a "non-locked" context
00132     } else {
00133         jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
00134         res = WaitForSingleObject(fEvent, usec / 1000);
00135         if (res != WAIT_OBJECT_0) {
00136             jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
00137         }
00138     }
00139 
00140         if (!ResetEvent(fEvent)) {
00141         jack_error("JackWinProcessSync::TimedWait ResetEvent err = %d", GetLastError());
00142     }
00143 
00144         return (res == WAIT_OBJECT_0);
00145 }
00146 
00147 /*
00148 // Code from APPLE CAGuard.cpp : does not seem to work as expected...
00149 
00150 void JackWinProcessSync::Wait()
00151 {
00152     if (!ReleaseMutex(fMutex)) {
00153         jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
00154     }
00155         DWORD res = WaitForSingleObject(fEvent, INFINITE);
00156     if (res != WAIT_OBJECT_0) {
00157         jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
00158     }
00159 }
00160 
00161 // Variant that behaves differently depending of the mutex state
00162 void JackWinProcessSync::Wait()
00163 {
00164     if (ReleaseMutex(fMutex)) {
00165         HANDLE handles[] = { fMutex, fEvent };
00166         DWORD res = WaitForMultipleObjects(2, handles, true, INFINITE);
00167         if (res != WAIT_OBJECT_0) {
00168             jack_error("JackWinProcessSync::LockedWait WaitForMultipleObjects err = %d", GetLastError());
00169         }
00170     } else {
00171         jack_error("JackWinProcessSync::Wait ReleaseMutex err = %d", GetLastError());
00172         DWORD res = WaitForSingleObject(fEvent, INFINITE);
00173         if (res != WAIT_OBJECT_0) {
00174             jack_error("JackWinProcessSync::Wait WaitForSingleObject err = %d", GetLastError());
00175         }
00176     }
00177 
00178     if (!ResetEvent(fEvent)) {
00179         jack_error("JackWinProcessSync::LockedWait ResetEvent err = %d", GetLastError());
00180     }
00181 }
00182 
00183 void JackWinProcessSync::LockedWait()
00184 {
00185     if (!ReleaseMutex(fMutex)) {
00186         jack_error("JackWinProcessSync::LockedWait ReleaseMutex err = %d", GetLastError());
00187     }
00188 
00189         HANDLE handles[] = { fMutex, fEvent };
00190         DWORD res = WaitForMultipleObjects(2, handles, true, INFINITE);
00191         if (res != WAIT_OBJECT_0) {
00192         jack_error("JackWinProcessSync::LockedWait WaitForMultipleObjects err = %d", GetLastError());
00193     }
00194 
00195     if (!ResetEvent(fEvent)) {
00196         jack_error("JackWinProcessSync::LockedWait ResetEvent err = %d", GetLastError());
00197     }
00198 }
00199 
00200 bool JackWinProcessSync::TimedWait(long usec)
00201 {
00202         if (!ReleaseMutex(fMutex)) {
00203         jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
00204     }
00205 
00206         DWORD res = WaitForSingleObject(fEvent, usec / 1000);
00207     if (res != WAIT_OBJECT_0) {
00208         jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
00209     }
00210 
00211         return (res == WAIT_OBJECT_0);
00212 }
00213 
00214 // Variant that behaves differently depending of the mutex state
00215 bool JackWinProcessSync::TimedWait(long usec)
00216 {
00217         if (ReleaseMutex(fMutex)) {
00218         HANDLE handles[] = { fMutex, fEvent };
00219         DWORD res = WaitForMultipleObjects(2, handles, true, usec / 1000);
00220         if ((res != WAIT_OBJECT_0) && (res != WAIT_TIMEOUT)) {
00221             jack_error("JackWinProcessSync::LockedTimedWait WaitForMultipleObjects err = %d", GetLastError());
00222         }
00223     } else {
00224         jack_error("JackWinProcessSync::TimedWait ReleaseMutex err = %d", GetLastError());
00225         DWORD res = WaitForSingleObject(fEvent, usec / 1000);
00226         if (res != WAIT_OBJECT_0) {
00227             jack_error("JackWinProcessSync::TimedWait WaitForSingleObject err = %d", GetLastError());
00228         }
00229     }
00230 
00231         if (!ResetEvent(fEvent)) {
00232         jack_error("JackWinProcessSync::LockedTimedWait ResetEvent err = %d", GetLastError());
00233     }
00234 
00235         return (res == WAIT_OBJECT_0);
00236 }
00237 
00238 bool JackWinProcessSync::LockedTimedWait(long usec)
00239 {
00240    if (!ReleaseMutex(fMutex)) {
00241         jack_error("JackWinProcessSync::LockedTimedWait ReleaseMutex err = %d", GetLastError());
00242     }
00243 
00244         HANDLE handles[] = { fMutex, fEvent };
00245         DWORD res = WaitForMultipleObjects(2, handles, true, usec / 1000);
00246         if ((res != WAIT_OBJECT_0) && (res != WAIT_TIMEOUT)) {
00247         jack_error("JackWinProcessSync::LockedTimedWait WaitForMultipleObjects err = %d", GetLastError());
00248     }
00249 
00250     if (!ResetEvent(fEvent)) {
00251         jack_error("JackWinProcessSync::LockedTimedWait ResetEvent err = %d", GetLastError());
00252     }
00253 
00254     return (res == WAIT_OBJECT_0);
00255 }
00256 */
00257 
00258 } // end of namespace
00259 
00260 
00261