In synchronous mode, if the driver time out is reached, the server may get desynchron...
[jack2.git] / windows / JackWinEvent.cpp
blob1040f4bb30444d2fe9ec1dea18f4805524796d46
1 /*
2 Copyright (C) 2004-2005 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackWinEvent.h"
21 #include "JackError.h"
23 // http://www.codeproject.com/win32/Win32_Event_Handling.asp
24 // http://www.codeproject.com/threads/Synchronization.asp
26 namespace Jack
29 void JackWinEvent::BuildName(const char* name, char* res)
31 sprintf(res, "jack_pipe.%s", name);
34 bool JackWinEvent::Signal()
36 BOOL res;
37 assert(fEvent);
39 if (fFlush)
40 return true;
42 if (!(res = SetEvent(fEvent))) {
43 jack_error("JackWinEvent::Signal name = %s err = %ld", fName, GetLastError());
46 return res;
49 bool JackWinEvent::SignalAll()
51 BOOL res;
52 assert(fEvent);
54 if (fFlush)
55 return true;
57 if (!(res = SetEvent(fEvent))) {
58 jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName, GetLastError());
61 return res;
64 bool JackWinEvent::Wait()
66 DWORD res;
68 if ((res = WaitForSingleObject(fEvent, INFINITE)) == WAIT_TIMEOUT) {
69 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
72 return (res == WAIT_OBJECT_0);
75 bool JackWinEvent::TimedWait(long usec)
77 DWORD res;
79 if ((res = WaitForSingleObject(fEvent, usec / 1000)) == WAIT_TIMEOUT) {
80 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
83 return (res == WAIT_OBJECT_0);
86 // Client side : get the published semaphore from server
87 bool JackWinEvent::ConnectInput(const char* name)
89 BuildName(name, fName);
90 JackLog("JackWinEvent::Connect %s\n", fName);
92 // Temporary...
93 if (fEvent) {
94 JackLog("Already connected name = %s\n", name);
95 return true;
98 if ((fEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, fName)) == NULL) {
99 jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
100 return false;
101 } else {
102 return true;
106 bool JackWinEvent::Connect(const char* name)
108 return ConnectInput(name);
111 bool JackWinEvent::ConnectOutput(const char* name)
113 return ConnectInput(name);
116 bool JackWinEvent::Disconnect()
118 if (fEvent) {
119 JackLog("JackWinEvent::Disconnect %s\n", fName);
120 CloseHandle(fEvent);
121 fEvent = NULL;
122 return true;
123 } else {
124 return false;
128 bool JackWinEvent::Allocate(const char* name, int value)
130 BuildName(name, fName);
131 JackLog("JackWinEvent::Allocate name = %s val = %ld\n", fName, value);
133 /* create an auto reset event */
134 if ((fEvent = CreateEvent(NULL, FALSE, FALSE, fName)) == NULL) {
135 jack_error("Allocate: can't check in named event name = %s err = %ld", fName, GetLastError());
136 return false;
137 } else {
138 return true;
142 void JackWinEvent::Destroy()
144 if (fEvent != NULL) {
145 JackLog("JackWinEvent::Destroy %s\n", fName);
146 CloseHandle(fEvent);
147 fEvent = NULL;
148 } else {
149 jack_error("JackWinEvent::Destroy synchro == NULL");
154 } // end of namespace