Jack2 1.9.12

JackWinSemaphore.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3
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.
8
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.
13
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.
17
18 */
19
20 #include "JackWinSemaphore.h"
21 #include "JackConstants.h"
22 #include "JackTools.h"
23 #include "JackError.h"
24 #include <stdio.h>
25
26 namespace Jack
27 {
28
29 void JackWinSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
30 {
31 char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
32 JackTools::RewriteName(client_name, ext_client_name);
33 _snprintf(res, size, "jack_pipe.%s_%s", server_name, ext_client_name);
34 }
35
36 bool JackWinSemaphore::Signal()
37 {
38 BOOL res;
39 assert(fSemaphore);
40
41 if (fFlush) {
42 return true;
43 }
44
45 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
46 jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
47 }
48
49 return res;
50 }
51
52 bool JackWinSemaphore::SignalAll()
53 {
54 BOOL res;
55 assert(fSemaphore);
56
57 if (fFlush) {
58 return true;
59 }
60
61 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
62 jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
63 }
64
65 return res;
66 }
67
68 bool JackWinSemaphore::Wait()
69 {
70 DWORD res;
71
72 if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
73 jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
74 }
75
76 return (res == WAIT_OBJECT_0);
77 }
78
79 bool JackWinSemaphore::TimedWait(long usec)
80 {
81 DWORD res;
82
83 if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
84 jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
85 }
86
87 return (res == WAIT_OBJECT_0);
88 }
89
90 // Client side : get the published semaphore from server
91 bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name)
92 {
93 BuildName(name, server_name, fName, sizeof(fName));
94 jack_log("JackWinSemaphore::Connect %s", fName);
95
96 // Temporary...
97 if (fSemaphore) {
98 jack_log("Already connected name = %s", name);
99 return true;
100 }
101
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());
104 return false;
105 } else {
106 return true;
107 }
108 }
109
110 bool JackWinSemaphore::Connect(const char* name, const char* server_name)
111 {
112 return ConnectInput(name, server_name);
113 }
114
115 bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name)
116 {
117 return ConnectInput(name, server_name);
118 }
119
120 bool JackWinSemaphore::Disconnect()
121 {
122 if (fSemaphore) {
123 jack_log("JackWinSemaphore::Disconnect %s", fName);
124 CloseHandle(fSemaphore);
125 fSemaphore = NULL;
126 return true;
127 } else {
128 return false;
129 }
130 }
131
132 bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value)
133 {
134 BuildName(name, server_name, fName, sizeof(fName));
135 jack_log("JackWinSemaphore::Allocate name = %s val = %ld", fName, value);
136
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());
139 return false;
140 } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
141 jack_error("Allocate: named semaphore already exist name = %s", fName);
142 // Try to open it
143 fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, fName);
144 return (fSemaphore != NULL);
145 } else {
146 return true;
147 }
148 }
149
150 void JackWinSemaphore::Destroy()
151 {
152 if (fSemaphore != NULL) {
153 jack_log("JackWinSemaphore::Destroy %s", fName);
154 CloseHandle(fSemaphore);
155 fSemaphore = NULL;
156 } else {
157 jack_error("JackWinSemaphore::Destroy synchro == NULL");
158 }
159 }
160
161
162 } // end of namespace
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108