|
14 | 14 | * If not, see <http://www.gnu.org/licenses/>. |
15 | 15 | */ |
16 | 16 | #include <stdlib.h> |
| 17 | +#include <stdatomic.h> |
17 | 18 | #include <math.h> |
18 | 19 |
|
19 | | -#include <AvailabilityMacros.h> |
20 | | - |
21 | | -/* stdatomic.h arrived with C11 (GCC 4.9+, Xcode 5+). |
22 | | - * GCD arrived in Mac OS X 10.6 Snow Leopard. |
23 | | - * On older Apple toolchains (e.g. Xcode 3.1 / 10.4-10.5 / PowerPC), |
24 | | - * shim both to legacy OS X primitives: |
25 | | - * - atomic_size_t / atomic_* -> OSAtomic (libkern/OSAtomic.h, 10.4+) |
26 | | - * - dispatch_semaphore_* -> Mach semaphores (mach/semaphore.h) */ |
27 | | -#if !defined(MAC_OS_X_VERSION_10_7) || \ |
28 | | - (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
29 | | - MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7) |
30 | | -#define RARCH_COREAUDIO_LEGACY_APPLE 1 |
31 | | -#endif |
32 | | - |
33 | | -#ifdef RARCH_COREAUDIO_LEGACY_APPLE |
34 | | -#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> |
39 | | -#include <mach/mach.h> |
40 | | -#include <mach/task.h> |
41 | | -#include <mach/semaphore.h> |
42 | | -#include <mach/clock_types.h> |
43 | | -#include <mach/sync_policy.h> |
44 | | -#include <stdint.h> |
45 | | - |
46 | | -/* --- atomic shim (size_t, acquire/release subset actually used) --- */ |
47 | | -typedef volatile int32_t atomic_size_t; |
48 | | -typedef int memory_order; |
49 | | -#define memory_order_acquire 0 |
50 | | -#define memory_order_release 0 |
51 | | -#define atomic_init(ptr, val) \ |
52 | | - (*(ptr) = (int32_t)(val)) |
53 | | -#define atomic_load_explicit(ptr, mo) \ |
54 | | - ((size_t)OSAtomicAdd32Barrier(0, (ptr))) |
55 | | -#define atomic_fetch_add_explicit(ptr, n, mo) \ |
56 | | - ((size_t)(OSAtomicAdd32Barrier((int32_t)(n), (ptr)) - (int32_t)(n))) |
57 | | -#define atomic_fetch_sub_explicit(ptr, n, mo) \ |
58 | | - ((size_t)(OSAtomicAdd32Barrier(-(int32_t)(n), (ptr)) + (int32_t)(n))) |
59 | | - |
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. */ |
65 | | -#ifndef NSEC_PER_MSEC |
66 | | -#define NSEC_PER_MSEC 1000000ULL |
67 | | -#endif |
68 | | -#ifndef NSEC_PER_SEC |
69 | | -#define NSEC_PER_SEC 1000000000ULL |
70 | | -#endif |
71 | | -#define DISPATCH_TIME_NOW 0 |
72 | | - |
73 | | -typedef uintptr_t dispatch_semaphore_t; |
74 | | -typedef uint64_t dispatch_time_t; |
75 | | - |
76 | | -static INLINE dispatch_semaphore_t dispatch_semaphore_create(long value) |
77 | | -{ |
78 | | - semaphore_t s = 0; |
79 | | - if (semaphore_create(mach_task_self(), &s, SYNC_POLICY_FIFO, |
80 | | - (int)value) != KERN_SUCCESS) |
81 | | - return 0; |
82 | | - return (dispatch_semaphore_t)s; |
83 | | -} |
84 | | -static INLINE void dispatch_semaphore_signal(dispatch_semaphore_t s) |
85 | | -{ |
86 | | - if (s) |
87 | | - semaphore_signal((semaphore_t)s); |
88 | | -} |
89 | | -static INLINE dispatch_time_t dispatch_time(dispatch_time_t base, int64_t delta) |
90 | | -{ |
91 | | - (void)base; |
92 | | - return (dispatch_time_t)delta; |
93 | | -} |
94 | | -static INLINE long dispatch_semaphore_wait(dispatch_semaphore_t s, |
95 | | - dispatch_time_t ns) |
96 | | -{ |
97 | | - mach_timespec_t ts; |
98 | | - if (!s) |
99 | | - return -1; |
100 | | - ts.tv_sec = (unsigned int)(ns / NSEC_PER_SEC); |
101 | | - ts.tv_nsec = (clock_res_t)(ns % NSEC_PER_SEC); |
102 | | - return (semaphore_timedwait((semaphore_t)s, ts) == KERN_SUCCESS) ? 0 : 1; |
103 | | -} |
104 | | -static INLINE void dispatch_release(dispatch_semaphore_t s) |
105 | | -{ |
106 | | - if (s) |
107 | | - semaphore_destroy(mach_task_self(), (semaphore_t)s); |
108 | | -} |
109 | | -#else |
110 | | -#include <stdatomic.h> |
111 | 20 | #include <dispatch/dispatch.h> |
112 | | -#endif |
113 | 21 |
|
114 | 22 | #if TARGET_OS_IPHONE |
115 | 23 | #include <AudioToolbox/AudioToolbox.h> |
@@ -879,25 +787,3 @@ audio_driver_t audio_coreaudio = { |
879 | 787 | coreaudio_buffer_size, |
880 | 788 | coreaudio_write_raw |
881 | 789 | }; |
882 | | - |
883 | | -/* Undo the legacy-Apple shim macros so they don't leak into other |
884 | | - * translation units when this file is #included into griffin.c. |
885 | | - * |
886 | | - * Shim typedefs (atomic_size_t, memory_order, dispatch_semaphore_t, |
887 | | - * dispatch_time_t) are left in place: C89/C99 forbid redefining a |
888 | | - * typedef even to the same type, but nothing else in the PPC griffin |
889 | | - * bundle declares these names (task_queue.c's GCD path is gated out |
890 | | - * by HAVE_GCD; the Obj-C users are in griffin_objc.m, a separate TU). |
891 | | - * If a future change adds another GCD/atomic user in griffin.c after |
892 | | - * this point, wrap its typedefs in the same RARCH_COREAUDIO_LEGACY_APPLE |
893 | | - * gate and include the shim there too. */ |
894 | | -#ifdef RARCH_COREAUDIO_LEGACY_APPLE |
895 | | -#undef atomic_init |
896 | | -#undef atomic_load_explicit |
897 | | -#undef atomic_fetch_add_explicit |
898 | | -#undef atomic_fetch_sub_explicit |
899 | | -#undef memory_order_acquire |
900 | | -#undef memory_order_release |
901 | | -#undef DISPATCH_TIME_NOW |
902 | | -#undef RARCH_COREAUDIO_LEGACY_APPLE |
903 | | -#endif |
0 commit comments