Move MIDI common files in server library on OSX.
[jack2.git] / windows / JackWinSemaphore.cpp
blob4500b10ba2b8369ea884634223e9bbcee0e247f0
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.
20 #include "JackWinSemaphore.h"
21 #include "JackConstants.h"
22 #include "JackTools.h"
23 #include "JackError.h"
24 #include <stdio.h>
26 namespace Jack
29 void JackWinSemaphore::BuildName(const char* client_name, const char* server_name, char* res)
31 char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
32 JackTools::RewriteName(client_name, ext_client_name);
33 sprintf(res, "jack_pipe.%s_%s", server_name, ext_client_name);
36 bool JackWinSemaphore::Signal()
38 BOOL res;
39 assert(fSemaphore);
41 if (fFlush)
42 return true;
44 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
45 jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
48 return res;
51 bool JackWinSemaphore::SignalAll()
53 BOOL res;
54 assert(fSemaphore);
56 if (fFlush)
57 return true;
59 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
60 jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
63 return res;
66 bool JackWinSemaphore::Wait()
68 DWORD res;
70 if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
71 jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
74 return (res == WAIT_OBJECT_0);
77 bool JackWinSemaphore::TimedWait(long usec)
79 DWORD res;
81 if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
82 jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
85 return (res == WAIT_OBJECT_0);
88 // Client side : get the published semaphore from server
89 bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name)
91 BuildName(name, server_name, fName);
92 jack_log("JackWinSemaphore::Connect %s", fName);
94 // Temporary...
95 if (fSemaphore) {
96 jack_log("Already connected name = %s", name);
97 return true;
100 if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) {
101 jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
102 return false;
103 } else {
104 return true;
108 bool JackWinSemaphore::Connect(const char* name, const char* server_name)
110 return ConnectInput(name, server_name);
113 bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name)
115 return ConnectInput(name, server_name);
118 bool JackWinSemaphore::Disconnect()
120 if (fSemaphore) {
121 jack_log("JackWinSemaphore::Disconnect %s", fName);
122 CloseHandle(fSemaphore);
123 fSemaphore = NULL;
124 return true;
125 } else {
126 return false;
130 bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value)
132 BuildName(name, server_name, fName);
133 jack_log("JackWinSemaphore::Allocate name = %s val = %ld", fName, value);
135 if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) {
136 jack_error("Allocate: can't check in named semaphore name = %s err = %ld", fName, GetLastError());
137 return false;
138 } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
139 jack_error("Allocate: named semaphore already exist name = %s", fName);
140 // Try to open it
141 fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, fName);
142 return (fSemaphore != NULL);
143 } else {
144 return true;
148 void JackWinSemaphore::Destroy()
150 if (fSemaphore != NULL) {
151 jack_log("JackWinSemaphore::Destroy %s", fName);
152 CloseHandle(fSemaphore);
153 fSemaphore = NULL;
154 } else {
155 jack_error("JackWinSemaphore::Destroy synchro == NULL");
160 } // end of namespace