Skip to content

Commit 1c5c124

Browse files
committed
coreaudio: make legacy-Apple shim robust to mach header exposure quirks
The pre-10.7 shim added in 24157a0 typedef'd dispatch_semaphore_t from semaphore_t at file scope, expecting <mach/mach.h> and <mach/semaphore.h> to expose the underlying typedef transitively. On some older SDK and compiler combinations that chain doesn't settle by the time we read it, producing: coreaudio.c:66: error: syntax error before dispatch_semaphore_t Two defensive changes: 1) Include <mach/mach_types.h> (canonical home of semaphore_t) and <mach/sync_policy.h> (SYNC_POLICY_FIFO) explicitly rather than relying on transitive inclusion from other mach headers. 2) Declare dispatch_semaphore_t as uintptr_t so its file-scope typedef depends on nothing from the mach headers. semaphore_t is still used inside function bodies, where the full header chain has been processed regardless of ordering quirks; the uintptr_t handle is cast to semaphore_t at each call site. Modern SDKs take the existing <stdatomic.h> / <dispatch/dispatch.h> path and are unaffected.
1 parent 24157a0 commit 1c5c124

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

audio/drivers/coreaudio.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@
3232

3333
#ifdef RARCH_COREAUDIO_LEGACY_APPLE
3434
#include <libkern/OSAtomic.h>
35+
/* Pull mach_types first; on some older SDKs <mach/mach.h> does not
36+
* reliably re-expose the typedefs by the time subsequent user headers
37+
* poke at them. Then <mach/mach.h> gives us the semaphore_* calls. */
38+
#include <mach/mach_types.h>
3539
#include <mach/mach.h>
36-
#include <mach/semaphore.h>
3740
#include <mach/task.h>
41+
#include <mach/semaphore.h>
3842
#include <mach/clock_types.h>
43+
#include <mach/sync_policy.h>
44+
#include <stdint.h>
3945

4046
/* --- atomic shim (size_t, acquire/release subset actually used) --- */
4147
typedef volatile int32_t atomic_size_t;
@@ -51,7 +57,11 @@ typedef int memory_order;
5157
#define atomic_fetch_sub_explicit(ptr, n, mo) \
5258
((size_t)(OSAtomicAdd32Barrier(-(int32_t)(n), (ptr)) + (int32_t)(n)))
5359

54-
/* --- GCD semaphore shim (thin wrapper over Mach semaphore_t) --- */
60+
/* --- GCD semaphore shim (thin wrapper over Mach semaphore_t) ---
61+
* dispatch_semaphore_t is declared as uintptr_t so its own typedef
62+
* depends on nothing from the mach headers. The actual mach calls
63+
* still take/return semaphore_t, but those calls only appear inside
64+
* function bodies below, not at file scope. */
5565
#ifndef NSEC_PER_MSEC
5666
#define NSEC_PER_MSEC 1000000ULL
5767
#endif
@@ -60,21 +70,21 @@ typedef int memory_order;
6070
#endif
6171
#define DISPATCH_TIME_NOW 0
6272

63-
typedef semaphore_t dispatch_semaphore_t;
64-
typedef uint64_t dispatch_time_t;
73+
typedef uintptr_t dispatch_semaphore_t;
74+
typedef uint64_t dispatch_time_t;
6575

6676
static INLINE dispatch_semaphore_t dispatch_semaphore_create(long value)
6777
{
68-
semaphore_t s;
78+
semaphore_t s = 0;
6979
if (semaphore_create(mach_task_self(), &s, SYNC_POLICY_FIFO,
7080
(int)value) != KERN_SUCCESS)
7181
return 0;
72-
return s;
82+
return (dispatch_semaphore_t)s;
7383
}
7484
static INLINE void dispatch_semaphore_signal(dispatch_semaphore_t s)
7585
{
7686
if (s)
77-
semaphore_signal(s);
87+
semaphore_signal((semaphore_t)s);
7888
}
7989
static INLINE dispatch_time_t dispatch_time(dispatch_time_t base, int64_t delta)
8090
{
@@ -89,12 +99,12 @@ static INLINE long dispatch_semaphore_wait(dispatch_semaphore_t s,
8999
return -1;
90100
ts.tv_sec = (unsigned int)(ns / NSEC_PER_SEC);
91101
ts.tv_nsec = (clock_res_t)(ns % NSEC_PER_SEC);
92-
return (semaphore_timedwait(s, ts) == KERN_SUCCESS) ? 0 : 1;
102+
return (semaphore_timedwait((semaphore_t)s, ts) == KERN_SUCCESS) ? 0 : 1;
93103
}
94104
static INLINE void dispatch_release(dispatch_semaphore_t s)
95105
{
96106
if (s)
97-
semaphore_destroy(mach_task_self(), s);
107+
semaphore_destroy(mach_task_self(), (semaphore_t)s);
98108
}
99109
#else
100110
#include <stdatomic.h>

0 commit comments

Comments
 (0)