Jack2  1.9.9
JackMachSemaphore.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 "JackMachSemaphore.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 JackMachSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
30 {
31  char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
32  JackTools::RewriteName(client_name, ext_client_name);
33  snprintf(res, size, "jack_mach_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
34 }
35 
36 bool JackMachSemaphore::Signal()
37 {
38  if (!fSemaphore) {
39  jack_error("JackMachSemaphore::Signal name = %s already deallocated!!", fName);
40  return false;
41  }
42 
43  if (fFlush) {
44  return true;
45  }
46 
47  kern_return_t res;
48  if ((res = semaphore_signal(fSemaphore)) != KERN_SUCCESS) {
49  jack_error("JackMachSemaphore::Signal name = %s err = %s", fName, mach_error_string(res));
50  }
51  return (res == KERN_SUCCESS);
52 }
53 
54 bool JackMachSemaphore::SignalAll()
55 {
56  if (!fSemaphore) {
57  jack_error("JackMachSemaphore::SignalAll name = %s already deallocated!!", fName);
58  return false;
59  }
60 
61  if (fFlush) {
62  return true;
63  }
64 
65  kern_return_t res;
66  // When signaled several times, do not accumulate signals...
67  if ((res = semaphore_signal_all(fSemaphore)) != KERN_SUCCESS) {
68  jack_error("JackMachSemaphore::SignalAll name = %s err = %s", fName, mach_error_string(res));
69  }
70  return (res == KERN_SUCCESS);
71 }
72 
73 bool JackMachSemaphore::Wait()
74 {
75  if (!fSemaphore) {
76  jack_error("JackMachSemaphore::Wait name = %s already deallocated!!", fName);
77  return false;
78  }
79 
80  kern_return_t res;
81  if ((res = semaphore_wait(fSemaphore)) != KERN_SUCCESS) {
82  jack_error("JackMachSemaphore::Wait name = %s err = %s", fName, mach_error_string(res));
83  }
84  return (res == KERN_SUCCESS);
85 }
86 
87 bool JackMachSemaphore::TimedWait(long usec)
88 {
89  if (!fSemaphore) {
90  jack_error("JackMachSemaphore::TimedWait name = %s already deallocated!!", fName);
91  return false;
92  }
93 
94  kern_return_t res;
95  mach_timespec time;
96  time.tv_sec = usec / 1000000;
97  time.tv_nsec = (usec % 1000000) * 1000;
98 
99  if ((res = semaphore_timedwait(fSemaphore, time)) != KERN_SUCCESS) {
100  jack_error("JackMachSemaphore::TimedWait name = %s usec = %ld err = %s", fName, usec, mach_error_string(res));
101  }
102  return (res == KERN_SUCCESS);
103 }
104 
105 // Server side : publish the semaphore in the global namespace
106 bool JackMachSemaphore::Allocate(const char* name, const char* server_name, int value)
107 {
108  BuildName(name, server_name, fName, sizeof(fName));
109  mach_port_t task = mach_task_self();
110  kern_return_t res;
111 
112  if (fBootPort == 0) {
113  if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
114  jack_error("Allocate: Can't find bootstrap mach port err = %s", mach_error_string(res));
115  return false;
116  }
117  }
118 
119  if ((res = semaphore_create(task, &fSemaphore, SYNC_POLICY_FIFO, value)) != KERN_SUCCESS) {
120  jack_error("Allocate: can create semaphore err = %s", mach_error_string(res));
121  return false;
122  }
123 
124  if ((res = bootstrap_register(fBootPort, fName, fSemaphore)) != KERN_SUCCESS) {
125  jack_error("Allocate: can't check in mach semaphore name = %s err = %s", fName, mach_error_string(res));
126 
127  switch (res) {
128  case BOOTSTRAP_SUCCESS :
129  /* service not currently registered, "a good thing" (tm) */
130  break;
131  case BOOTSTRAP_NOT_PRIVILEGED :
132  jack_log("bootstrap_register(): bootstrap not privileged");
133  break;
134  case BOOTSTRAP_SERVICE_ACTIVE :
135  jack_log("bootstrap_register(): bootstrap service active");
136  break;
137  default :
138  jack_log("bootstrap_register() err = %s", mach_error_string(res));
139  break;
140  }
141 
142  return false;
143  }
144 
145  jack_log("JackMachSemaphore::Allocate name = %s", fName);
146  return true;
147 }
148 
149 // Client side : get the published semaphore from server
150 bool JackMachSemaphore::ConnectInput(const char* name, const char* server_name)
151 {
152  BuildName(name, server_name, fName, sizeof(fName));
153  kern_return_t res;
154 
155  if (fBootPort == 0) {
156  if ((res = task_get_bootstrap_port(mach_task_self(), &fBootPort)) != KERN_SUCCESS) {
157  jack_error("Connect: can't find bootstrap port err = %s", mach_error_string(res));
158  return false;
159  }
160  }
161 
162  if ((res = bootstrap_look_up(fBootPort, fName, &fSemaphore)) != KERN_SUCCESS) {
163  jack_error("Connect: can't find mach semaphore name = %s err = %s", fName, mach_error_string(res));
164  return false;
165  }
166 
167  jack_log("JackMachSemaphore::Connect name = %s ", fName);
168  return true;
169 }
170 
171 bool JackMachSemaphore::Connect(const char* name, const char* server_name)
172 {
173  return ConnectInput(name, server_name);
174 }
175 
176 bool JackMachSemaphore::ConnectOutput(const char* name, const char* server_name)
177 {
178  return ConnectInput(name, server_name);
179 }
180 
181 bool JackMachSemaphore::Disconnect()
182 {
183  if (fSemaphore > 0) {
184  jack_log("JackMachSemaphore::Disconnect name = %s", fName);
185  fSemaphore = 0;
186  }
187  // Nothing to do
188  return true;
189 }
190 
191 // Server side : destroy the JackGlobals
192 void JackMachSemaphore::Destroy()
193 {
194  kern_return_t res;
195 
196  if (fSemaphore > 0) {
197  jack_log("JackMachSemaphore::Destroy name = %s", fName);
198  if ((res = semaphore_destroy(mach_task_self(), fSemaphore)) != KERN_SUCCESS) {
199  jack_error("JackMachSemaphore::Destroy can't destroy semaphore err = %s", mach_error_string(res));
200  }
201  fSemaphore = 0;
202  } else {
203  jack_error("JackMachSemaphore::Destroy semaphore < 0");
204  }
205 }
206 
207 } // end of namespace
208 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107