Tagging the 1.9.8 release of the 'jack2' project.
[jack2.git] / windows / JackWinEvent.cpp
blob9c04465ca1807fb3313852d440c4171b7fa4abbd
1 /*
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.
21 #include "JackWinEvent.h"
22 #include "JackTools.h"
23 #include "JackError.h"
24 #include <assert.h>
26 // http://www.codeproject.com/win32/Win32_Event_Handling.asp
27 // http://www.codeproject.com/threads/Synchronization.asp
29 namespace Jack
32 void JackWinEvent::BuildName(const char* name, const char* server_name, char* res, int size)
34 snprintf(res, size, "jack_pipe.%s_%s", server_name, name);
37 bool JackWinEvent::Signal()
39 BOOL res;
40 assert(fEvent);
42 if (fFlush)
43 return true;
45 if (!(res = SetEvent(fEvent))) {
46 jack_error("JackWinEvent::Signal name = %s err = %ld", fName, GetLastError());
49 return res;
52 bool JackWinEvent::SignalAll()
54 BOOL res;
55 assert(fEvent);
57 if (fFlush)
58 return true;
60 if (!(res = SetEvent(fEvent))) {
61 jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName, GetLastError());
64 return res;
67 bool JackWinEvent::Wait()
69 DWORD res;
71 if ((res = WaitForSingleObject(fEvent, INFINITE)) == WAIT_TIMEOUT) {
72 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
75 return (res == WAIT_OBJECT_0);
78 bool JackWinEvent::TimedWait(long usec)
80 DWORD res;
82 if ((res = WaitForSingleObject(fEvent, usec / 1000)) == WAIT_TIMEOUT) {
83 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
86 return (res == WAIT_OBJECT_0);
89 // Client side : get the published semaphore from server
90 bool JackWinEvent::ConnectInput(const char* name, const char* server_name)
92 BuildName(name, server_name, fName, sizeof(fName));
93 jack_log("JackWinEvent::Connect %s", fName);
95 // Temporary...
96 if (fEvent) {
97 jack_log("Already connected name = %s", name);
98 return true;
101 if ((fEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, fName)) == NULL) {
102 jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
103 return false;
104 } else {
105 return true;
109 bool JackWinEvent::Connect(const char* name, const char* server_name)
111 return ConnectInput(name, server_name);
114 bool JackWinEvent::ConnectOutput(const char* name, const char* server_name)
116 return ConnectInput(name, server_name);
119 bool JackWinEvent::Disconnect()
121 if (fEvent) {
122 jack_log("JackWinEvent::Disconnect %s", fName);
123 CloseHandle(fEvent);
124 fEvent = NULL;
125 return true;
126 } else {
127 return false;
131 bool JackWinEvent::Allocate(const char* name, const char* server_name, int value)
133 BuildName(name, server_name, fName, sizeof(fName));
134 jack_log("JackWinEvent::Allocate name = %s val = %ld", fName, value);
136 /* create an auto reset event */
137 if ((fEvent = CreateEvent(NULL, FALSE, FALSE, fName)) == NULL) {
138 jack_error("Allocate: can't check in named event name = %s err = %ld", fName, GetLastError());
139 return false;
140 } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
141 jack_error("Allocate: named event already exist name = %s", fName);
142 CloseHandle(fEvent);
143 fEvent = NULL;
144 return false;
145 } else {
146 return true;
150 void JackWinEvent::Destroy()
152 if (fEvent != NULL) {
153 jack_log("JackWinEvent::Destroy %s", fName);
154 CloseHandle(fEvent);
155 fEvent = NULL;
156 } else {
157 jack_error("JackWinEvent::Destroy synchro == NULL");
162 } // end of namespace