Merge branch 'master' into port_register_notification_defer
[jack2.git] / macosx / JackMachSemaphore.cpp
blobf4b129c86aff844d58e38f85e5fb4cdeb25bb17c
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 "JackMachSemaphore.h"
21 #include "JackConstants.h"
22 #include "JackTools.h"
23 #include "JackError.h"
24 #include <stdio.h>
26 namespace Jack
29 mach_port_t JackMachSemaphore::fBootPort = 0;
31 void JackMachSemaphore::BuildName(const char* client_name, const char* server_name, char* res)
33 char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
34 JackTools::RewriteName(client_name, ext_client_name);
35 sprintf(res, "jack_mach_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
38 bool JackMachSemaphore::Signal()
40 if (!fSemaphore) {
41 jack_error("JackMachSemaphore::Signal name = %s already desallocated!!", fName);
42 return false;
45 if (fFlush)
46 return true;
48 kern_return_t res;
49 if ((res = semaphore_signal(fSemaphore)) != KERN_SUCCESS) {
50 jack_error("JackMachSemaphore::Signal name = %s err = %s", fName, mach_error_string(res));
52 return (res == KERN_SUCCESS);
55 bool JackMachSemaphore::SignalAll()
57 if (!fSemaphore) {
58 jack_error("JackMachSemaphore::SignalAll name = %s already desallocated!!", fName);
59 return false;
62 if (fFlush)
63 return true;
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));
70 return (res == KERN_SUCCESS);
73 bool JackMachSemaphore::Wait()
75 if (!fSemaphore) {
76 jack_error("JackMachSemaphore::Wait name = %s already desallocated!!", fName);
77 return false;
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));
84 return (res == KERN_SUCCESS);
87 bool JackMachSemaphore::TimedWait(long usec)
89 if (!fSemaphore) {
90 jack_error("JackMachSemaphore::TimedWait name = %s already desallocated!!", fName);
91 return false;
94 kern_return_t res;
95 mach_timespec time;
96 time.tv_sec = usec / 1000000;
97 time.tv_nsec = (usec % 1000000) * 1000;
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));
102 return (res == KERN_SUCCESS);
105 // Server side : publish the semaphore in the global namespace
106 bool JackMachSemaphore::Allocate(const char* name, const char* server_name, int value)
108 BuildName(name, server_name, fName);
109 mach_port_t task = mach_task_self();
110 kern_return_t res;
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;
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;
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));
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;
142 return false;
145 jack_log("JackMachSemaphore::Allocate name = %s", fName);
146 return true;
149 // Client side : get the published semaphore from server
150 bool JackMachSemaphore::ConnectInput(const char* name, const char* server_name)
152 BuildName(name, server_name, fName);
153 kern_return_t res;
155 // Temporary... A REVOIR
157 if (fSemaphore > 0) {
158 jack_log("Already connected name = %s", name);
159 return true;
163 if (fBootPort == 0) {
164 if ((res = task_get_bootstrap_port(mach_task_self(), &fBootPort)) != KERN_SUCCESS) {
165 jack_error("Connect: can't find bootstrap port err = %s", mach_error_string(res));
166 return false;
170 if ((res = bootstrap_look_up(fBootPort, fName, &fSemaphore)) != KERN_SUCCESS) {
171 jack_error("Connect: can't find mach semaphore name = %s err = %s", fName, mach_error_string(res));
172 return false;
175 jack_log("JackMachSemaphore::Connect name = %s ", fName);
176 return true;
179 bool JackMachSemaphore::Connect(const char* name, const char* server_name)
181 return ConnectInput(name, server_name);
184 bool JackMachSemaphore::ConnectOutput(const char* name, const char* server_name)
186 return ConnectInput(name, server_name);
189 bool JackMachSemaphore::Disconnect()
191 if (fSemaphore > 0) {
192 jack_log("JackMachSemaphore::Disconnect name = %s", fName);
193 fSemaphore = 0;
195 // Nothing to do
196 return true;
199 // Server side : destroy the JackGlobals
200 void JackMachSemaphore::Destroy()
202 kern_return_t res;
204 if (fSemaphore > 0) {
205 jack_log("JackMachSemaphore::Destroy");
206 if ((res = semaphore_destroy(mach_task_self(), fSemaphore)) != KERN_SUCCESS) {
207 jack_error("JackMachSemaphore::Destroy can't destroy semaphore err = %s", mach_error_string(res));
209 fSemaphore = 0;
210 } else {
211 jack_error("JackMachSemaphore::Destroy semaphore < 0");
215 } // end of namespace