Skip to content

Commit f7b280a

Browse files
committed
macos mod-desktop fixup
Signed-off-by: falkTX <falktx@falktx.com>
1 parent bee6bea commit f7b280a

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

common/JackModDesktopDriver.cpp

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include "JackMidiPort.h"
99
#include "JackTools.h"
1010

11-
#ifdef _WIN32
12-
#else
11+
#ifndef _WIN32
1312
# include <fcntl.h>
1413
# include <sys/mman.h>
1514
# ifdef __APPLE__
@@ -34,7 +33,7 @@ namespace Jack
3433
#ifdef __APPLE__
3534
static void terminateHandler(void*)
3635
{
37-
printf("Desktop driver parent has died, terminating ourselves now\n");
36+
printf("MOD Desktop driver parent has died, terminating ourselves now\n");
3837
fflush(stdout);
3938
kill(getpid(), SIGTERM);
4039
}
@@ -95,6 +94,40 @@ class ModDesktopAudioDriver : public JackAudioDriver
9594
mach_port_t task = MACH_PORT_NULL;
9695
semaphore_t sem1 = MACH_PORT_NULL;
9796
semaphore_t sem2 = MACH_PORT_NULL;
97+
mach_port_t port1 = MACH_PORT_NULL;
98+
mach_port_t port2 = MACH_PORT_NULL;
99+
100+
bool connectport(const mach_port_t port, semaphore_t* const sem)
101+
{
102+
mach_port_t reqport;
103+
104+
if (mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &reqport) != KERN_SUCCESS)
105+
return false;
106+
107+
struct {
108+
mach_msg_header_t hdr;
109+
mach_msg_trailer_t trailer;
110+
} msg;
111+
112+
msg.hdr.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
113+
msg.hdr.msgh_local_port = reqport;
114+
msg.hdr.msgh_remote_port = port;
115+
116+
if (mach_msg(&msg.hdr, MACH_SEND_MSG, sizeof(msg.hdr), 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL) != MACH_MSG_SUCCESS)
117+
{
118+
mach_port_destroy(task, reqport);
119+
return false;
120+
}
121+
122+
if (mach_msg(&msg.hdr, MACH_RCV_MSG, 0, sizeof(msg), reqport, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL) != MACH_MSG_SUCCESS)
123+
{
124+
mach_port_destroy(task, reqport);
125+
return false;
126+
}
127+
128+
*sem = msg.hdr.msgh_remote_port;
129+
return true;
130+
}
98131

99132
void post()
100133
{
@@ -289,23 +322,27 @@ class ModDesktopAudioDriver : public JackAudioDriver
289322
#ifdef __APPLE__
290323
task = mach_task_self();
291324

292-
mach_port_t bootport1;
293-
if (task_get_bootstrap_port(task, &bootport1) != KERN_SUCCESS ||
294-
bootstrap_look_up(bootport1, fShmData->bootname1, &sem1) != KERN_SUCCESS)
325+
mach_port_t bootport;
326+
if (task_get_bootstrap_port(task, &bootport) != KERN_SUCCESS)
295327
{
296328
Close();
297329
jack_error("Can't open default MOD Desktop driver 4");
298330
return -1;
299331
}
300332

301-
mach_port_t bootport2;
302-
if (task_get_bootstrap_port(task, &bootport2) != KERN_SUCCESS ||
303-
bootstrap_look_up(bootport2, fShmData->bootname2, &sem2) != KERN_SUCCESS)
333+
if (bootstrap_look_up(bootport, fShmData->bootname1, &port1) != KERN_SUCCESS || !connectport(port1, &sem1))
304334
{
305335
Close();
306336
jack_error("Can't open default MOD Desktop driver 5");
307337
return -1;
308338
}
339+
340+
if (bootstrap_look_up(bootport, fShmData->bootname2, &port2) != KERN_SUCCESS || !connectport(port2, &sem2))
341+
{
342+
Close();
343+
jack_error("Can't open default MOD Desktop driver 6");
344+
return -1;
345+
}
309346
#endif
310347

311348
return 0;
@@ -319,6 +356,18 @@ class ModDesktopAudioDriver : public JackAudioDriver
319356
JackAudioDriver::Close();
320357

321358
#ifdef __APPLE__
359+
if (port1 != MACH_PORT_NULL)
360+
{
361+
mach_port_deallocate(task, port1);
362+
port1 = MACH_PORT_NULL;
363+
}
364+
365+
if (port2 != MACH_PORT_NULL)
366+
{
367+
mach_port_deallocate(task, port2);
368+
port2 = MACH_PORT_NULL;
369+
}
370+
322371
if (sem1 != MACH_PORT_NULL)
323372
{
324373
semaphore_destroy(task, sem1);

macosx/JackMachSemaphore.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ class SERVER_EXPORT JackMachSemaphore : public detail::JackSynchro
4646
* (server) or Connect()ed (client). */
4747
semaphore_t fSemaphore;
4848

49-
/*! \brief The bootstrap port for this task, or MACH_PORT_NULL if not yet obtained. */
50-
mach_port_t fBootPort;
51-
5249
/*! \brief The IPC port used to pass the semaphore port from the server to the client, and
5350
* for the client to request that this occurs. MACH_PORT_NULL if not yet created (server) or
5451
* looked up (client). */
@@ -70,7 +67,6 @@ class SERVER_EXPORT JackMachSemaphore : public detail::JackSynchro
7067
JackMachSemaphore():
7168
JackSynchro(),
7269
fSemaphore(MACH_PORT_NULL),
73-
fBootPort(MACH_PORT_NULL),
7470
fServicePort(MACH_PORT_NULL),
7571
fSemServer(NULL),
7672
fThreadSemServer(NULL)

macosx/JackMachSemaphore.mm

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,12 @@
153153
BuildName(client_name, server_name, fName, sizeof(fName));
154154

155155
mach_port_t task = mach_task_self();
156+
mach_port_t bootport;
156157
kern_return_t res;
157158

158-
if (fBootPort == MACH_PORT_NULL) {
159-
if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
160-
jack_mach_error(res, "can't find bootstrap mach port");
161-
return false;
162-
}
159+
if ((res = task_get_bootstrap_port(task, &bootport)) != KERN_SUCCESS) {
160+
jack_mach_error(res, "can't find bootstrap mach port");
161+
return false;
163162
}
164163

165164
if ((res = semaphore_create(task, &fSemaphore, SYNC_POLICY_FIFO, value)) != KERN_SUCCESS) {
@@ -185,7 +184,7 @@
185184
return false;
186185
}
187186

188-
if ((res = bootstrap_register(fBootPort, fName, fServicePort)) != KERN_SUCCESS) {
187+
if ((res = bootstrap_register(bootport, fName, fServicePort)) != KERN_SUCCESS) {
189188
jack_mach_bootstrap_err(res, "can't register IPC port with bootstrap server", fName);
190189

191190
// Cleanup created semaphore & mach port
@@ -224,21 +223,20 @@
224223
BuildName(client_name, server_name, fName, sizeof(fName));
225224

226225
mach_port_t task = mach_task_self();
226+
mach_port_t bootport;
227227
kern_return_t res;
228228

229229
if (fSemaphore != MACH_PORT_NULL) {
230230
jack_log("JackMachSemaphore::Connect: Already connected name = %s", fName);
231231
return true;
232232
}
233233

234-
if (fBootPort == MACH_PORT_NULL) {
235-
if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
236-
jack_mach_error(res, "can't find bootstrap port");
237-
return false;
238-
}
234+
if ((res = task_get_bootstrap_port(task, &bootport)) != KERN_SUCCESS) {
235+
jack_mach_error(res, "can't find bootstrap port");
236+
return false;
239237
}
240238

241-
if ((res = bootstrap_look_up(fBootPort, fName, &fServicePort)) != KERN_SUCCESS) {
239+
if ((res = bootstrap_look_up(bootport, fName, &fServicePort)) != KERN_SUCCESS) {
242240
jack_mach_bootstrap_err(res, "can't find IPC service port to request semaphore", fName);
243241
return false;
244242
}

0 commit comments

Comments
 (0)