20 #include "JackMachSemaphore.h"
21 #include "JackConstants.h"
22 #include "JackTools.h"
23 #include "JackError.h"
29 void JackMachSemaphore::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_mach_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
36 bool JackMachSemaphore::Signal()
39 jack_error(
"JackMachSemaphore::Signal name = %s already deallocated!!", fName);
48 if ((res = semaphore_signal(fSemaphore)) != KERN_SUCCESS) {
49 jack_error(
"JackMachSemaphore::Signal name = %s err = %s", fName, mach_error_string(res));
51 return (res == KERN_SUCCESS);
54 bool JackMachSemaphore::SignalAll()
57 jack_error(
"JackMachSemaphore::SignalAll name = %s already deallocated!!", fName);
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()
76 jack_error(
"JackMachSemaphore::Wait name = %s already deallocated!!", fName);
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)
90 jack_error(
"JackMachSemaphore::TimedWait name = %s already deallocated!!", fName);
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);
106 bool JackMachSemaphore::Allocate(
const char* name,
const char* server_name,
int value)
108 BuildName(name, server_name, fName,
sizeof(fName));
109 mach_port_t task = mach_task_self();
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));
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));
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));
128 case BOOTSTRAP_SUCCESS :
131 case BOOTSTRAP_NOT_PRIVILEGED :
132 jack_log(
"bootstrap_register(): bootstrap not privileged");
134 case BOOTSTRAP_SERVICE_ACTIVE :
135 jack_log(
"bootstrap_register(): bootstrap service active");
138 jack_log(
"bootstrap_register() err = %s", mach_error_string(res));
145 jack_log(
"JackMachSemaphore::Allocate name = %s", fName);
150 bool JackMachSemaphore::ConnectInput(
const char* name,
const char* server_name)
152 BuildName(name, server_name, fName,
sizeof(fName));
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));
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));
167 jack_log(
"JackMachSemaphore::Connect name = %s ", fName);
171 bool JackMachSemaphore::Connect(
const char* name,
const char* server_name)
173 return ConnectInput(name, server_name);
176 bool JackMachSemaphore::ConnectOutput(
const char* name,
const char* server_name)
178 return ConnectInput(name, server_name);
181 bool JackMachSemaphore::Disconnect()
183 if (fSemaphore > 0) {
184 jack_log(
"JackMachSemaphore::Disconnect name = %s", fName);
192 void JackMachSemaphore::Destroy()
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));
203 jack_error(
"JackMachSemaphore::Destroy semaphore < 0");
SERVER_EXPORT void jack_error(const char *fmt,...)
SERVER_EXPORT void jack_log(const char *fmt,...)