Skip to content

Commit e306002

Browse files
committed
Update mod-desktop driver to also use macos private APIs
Signed-off-by: falkTX <falktx@falktx.com>
1 parent f7b280a commit e306002

File tree

1 file changed

+33
-119
lines changed

1 file changed

+33
-119
lines changed

common/JackModDesktopDriver.cpp

Lines changed: 33 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
#include "JackTools.h"
1010

1111
#ifndef _WIN32
12+
# include <cerrno>
1213
# include <fcntl.h>
1314
# include <sys/mman.h>
1415
# ifdef __APPLE__
1516
# include <dispatch/dispatch.h>
16-
# include <mach/mach.h>
17-
# include <mach/semaphore.h>
18-
# include <servers/bootstrap.h>
17+
extern "C" {
18+
int __ulock_wait(uint32_t operation, void* addr, uint64_t value, uint32_t timeout_us);
19+
int __ulock_wake(uint32_t operation, void* addr, uint64_t value);
20+
}
1921
# else
20-
# include <cerrno>
2122
# include <syscall.h>
2223
# include <sys/prctl.h>
2324
# include <sys/time.h>
@@ -62,10 +63,7 @@ class ModDesktopAudioDriver : public JackAudioDriver
6263
struct Data {
6364
uint32_t magic;
6465
int32_t padding1;
65-
#if defined(__APPLE__)
66-
char bootname1[32];
67-
char bootname2[32];
68-
#elif defined(_WIN32)
66+
#ifdef _WIN32
6967
HANDLE sem1;
7068
HANDLE sem2;
7169
#else
@@ -90,56 +88,7 @@ class ModDesktopAudioDriver : public JackAudioDriver
9088
int fShmFd;
9189
#endif
9290

93-
#if defined(__APPLE__)
94-
mach_port_t task = MACH_PORT_NULL;
95-
semaphore_t sem1 = MACH_PORT_NULL;
96-
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-
}
131-
132-
void post()
133-
{
134-
semaphore_signal(sem2);
135-
}
136-
137-
bool wait()
138-
{
139-
const mach_timespec timeout = { 1, 0 };
140-
return semaphore_timedwait(sem1, timeout) == KERN_SUCCESS;
141-
}
142-
#elif defined(_WIN32)
91+
#ifdef _WIN32
14392
void post()
14493
{
14594
ReleaseSemaphore(fShmData->sem2, 1, nullptr);
@@ -155,19 +104,31 @@ class ModDesktopAudioDriver : public JackAudioDriver
155104
const bool unlocked = __sync_bool_compare_and_swap(&fShmData->sem2, 0, 1);
156105
if (! unlocked)
157106
return;
158-
::syscall(__NR_futex, &fShmData->sem2, FUTEX_WAKE, 1, nullptr, nullptr, 0);
107+
#ifdef __APPLE__
108+
__ulock_wake(0x1000003, &fShmData->sem2, 0);
109+
#else
110+
syscall(__NR_futex, &fShmData->sem2, FUTEX_WAKE, 1, nullptr, nullptr, 0);
111+
#endif
159112
}
160113

161114
bool wait()
162115
{
116+
#ifdef __APPLE__
117+
const uint32_t timeout = 1000000;
118+
#else
163119
const timespec timeout = { 1, 0 };
120+
#endif
164121

165122
for (;;)
166123
{
167124
if (__sync_bool_compare_and_swap(&fShmData->sem1, 1, 0))
168125
return true;
169126

170-
if (::syscall(__NR_futex, &fShmData->sem1, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0)
127+
#ifdef __APPLE__
128+
if (__ulock_wait(0x3, &fShmData->sem1, 0, timeout) != 0)
129+
#else
130+
if (syscall(__NR_futex, &fShmData->sem1, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0)
131+
#endif
171132
if (errno != EAGAIN && errno != EINTR)
172133
return false;
173134
}
@@ -319,32 +280,6 @@ class ModDesktopAudioDriver : public JackAudioDriver
319280
return -1;
320281
}
321282

322-
#ifdef __APPLE__
323-
task = mach_task_self();
324-
325-
mach_port_t bootport;
326-
if (task_get_bootstrap_port(task, &bootport) != KERN_SUCCESS)
327-
{
328-
Close();
329-
jack_error("Can't open default MOD Desktop driver 4");
330-
return -1;
331-
}
332-
333-
if (bootstrap_look_up(bootport, fShmData->bootname1, &port1) != KERN_SUCCESS || !connectport(port1, &sem1))
334-
{
335-
Close();
336-
jack_error("Can't open default MOD Desktop driver 5");
337-
return -1;
338-
}
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-
}
346-
#endif
347-
348283
return 0;
349284
}
350285

@@ -355,32 +290,6 @@ class ModDesktopAudioDriver : public JackAudioDriver
355290

356291
JackAudioDriver::Close();
357292

358-
#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-
371-
if (sem1 != MACH_PORT_NULL)
372-
{
373-
semaphore_destroy(task, sem1);
374-
sem1 = MACH_PORT_NULL;
375-
}
376-
377-
if (sem2 != MACH_PORT_NULL)
378-
{
379-
semaphore_destroy(task, sem2);
380-
sem2 = MACH_PORT_NULL;
381-
}
382-
#endif
383-
384293
#ifdef _WIN32
385294
if (fShmData != nullptr)
386295
{
@@ -467,16 +376,21 @@ class ModDesktopAudioDriver : public JackAudioDriver
467376
if (JackAudioDriver::Start() != 0)
468377
return -1;
469378

470-
#if 0 // def __APPLE__
471-
fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000;
472-
fEngineControl->fComputation = JackTools::ComputationMicroSec(fEngineControl->fBufferSize) * 1000;
473-
fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
474-
#endif
475-
476379
fIsProcessing = fIsRunning = true;
477380

478381
if (JackThread::StartImp(&fProcessThread, 80, 1, on_process, this) == 0)
479-
return true;
382+
{
383+
#ifdef __APPLE__
384+
fEngineControl->fPeriod = fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
385+
fEngineControl->fComputation = JackTools::ComputationMicroSec(fEngineControl->fBufferSize) * 1000;
386+
387+
JackThread::AcquireRealTimeImp(fProcessThread,
388+
fEngineControl->fPeriod,
389+
fEngineControl->fComputation,
390+
fEngineControl->fConstraint);
391+
#endif
392+
return 0;
393+
}
480394

481395
return JackThread::StartImp(&fProcessThread, 0, 0, on_process, this);
482396
}

0 commit comments

Comments
 (0)