20 #include "JackWinSemaphore.h"
21 #include "JackConstants.h"
22 #include "JackTools.h"
23 #include "JackError.h"
29 void JackWinSemaphore::BuildName(
const char* client_name,
const char* server_name,
char* res,
int size)
31 char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
32 JackTools::RewriteName(client_name, ext_client_name);
33 _snprintf(res, size,
"jack_pipe.%s_%s", server_name, ext_client_name);
36 bool JackWinSemaphore::Signal()
45 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
46 jack_error(
"JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
52 bool JackWinSemaphore::SignalAll()
61 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
62 jack_error(
"JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
68 bool JackWinSemaphore::Wait()
72 if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
73 jack_error(
"JackWinSemaphore::TimedWait name = %s time_out", fName);
76 return (res == WAIT_OBJECT_0);
79 bool JackWinSemaphore::TimedWait(
long usec)
83 if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
84 jack_error(
"JackWinSemaphore::TimedWait name = %s time_out", fName);
87 return (res == WAIT_OBJECT_0);
91 bool JackWinSemaphore::ConnectInput(
const char* name,
const char* server_name)
93 BuildName(name, server_name, fName,
sizeof(fName));
94 jack_log(
"JackWinSemaphore::Connect %s", fName);
98 jack_log(
"Already connected name = %s", name);
102 if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) {
103 jack_error(
"Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
110 bool JackWinSemaphore::Connect(
const char* name,
const char* server_name)
112 return ConnectInput(name, server_name);
115 bool JackWinSemaphore::ConnectOutput(
const char* name,
const char* server_name)
117 return ConnectInput(name, server_name);
120 bool JackWinSemaphore::Disconnect()
123 jack_log(
"JackWinSemaphore::Disconnect %s", fName);
124 CloseHandle(fSemaphore);
132 bool JackWinSemaphore::Allocate(
const char* name,
const char* server_name,
int value)
134 BuildName(name, server_name, fName,
sizeof(fName));
135 jack_log(
"JackWinSemaphore::Allocate name = %s val = %ld", fName, value);
137 if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) {
138 jack_error(
"Allocate: can't check in named semaphore name = %s err = %ld", fName, GetLastError());
140 }
else if (GetLastError() == ERROR_ALREADY_EXISTS) {
141 jack_error(
"Allocate: named semaphore already exist name = %s", fName);
143 fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, fName);
144 return (fSemaphore != NULL);
150 void JackWinSemaphore::Destroy()
152 if (fSemaphore != NULL) {
153 jack_log(
"JackWinSemaphore::Destroy %s", fName);
154 CloseHandle(fSemaphore);
157 jack_error(
"JackWinSemaphore::Destroy synchro == NULL");
SERVER_EXPORT void jack_error(const char *fmt,...)
SERVER_EXPORT void jack_log(const char *fmt,...)