2 * Copyright (c) 2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 /***********************************************************************
26 * OS portability layer.
27 **********************************************************************/
32 #include <TargetConditionals.h>
33 #include "objc-config.h"
36 # define WORD_SHIFT 3UL
37 # define WORD_MASK 7UL
40 # define WORD_SHIFT 2UL
41 # define WORD_MASK 3UL
45 static inline uint32_t word_align(uint32_t x
) {
46 return (x
+ WORD_MASK
) & ~WORD_MASK
;
48 static inline size_t word_align(size_t x
) {
49 return (x
+ WORD_MASK
) & ~WORD_MASK
;
53 // Mix-in for classes that must not be copied.
56 nocopy_t(const nocopy_t
&) = delete;
57 const nocopy_t
& operator=(const nocopy_t
&) = delete;
59 constexpr nocopy_t() = default;
60 ~nocopy_t() = default;
66 # define OS_UNFAIR_LOCK_INLINE 1
68 # ifndef __STDC_LIMIT_MACROS
69 # define __STDC_LIMIT_MACROS
86 # include <crt_externs.h>
88 # include <Availability.h>
89 # include <TargetConditionals.h>
90 # include <sys/mman.h>
91 # include <sys/time.h>
92 # include <sys/stat.h>
93 # include <sys/param.h>
94 # include <sys/reason.h>
95 # include <mach/mach.h>
96 # include <mach/vm_param.h>
97 # include <mach/mach_time.h>
98 # include <mach-o/dyld.h>
99 # include <mach-o/ldsyms.h>
100 # include <mach-o/loader.h>
101 # include <mach-o/getsect.h>
102 # include <mach-o/dyld_priv.h>
103 # include <malloc/malloc.h>
104 # include <os/lock_private.h>
105 # include <libkern/OSAtomic.h>
106 # include <libkern/OSCacheControl.h>
107 # include <System/pthread_machdep.h>
108 # include "objc-probes.h" // generated dtrace probe definitions.
110 // Some libc functions call objc_msgSend()
111 // so we can't use them without deadlocks.
112 void syslog(int, const char *, ...) UNAVAILABLE_ATTRIBUTE
;
113 void vsyslog(int, const char *, va_list) UNAVAILABLE_ATTRIBUTE
;
116 #define ALWAYS_INLINE inline __attribute__((always_inline))
117 #define NEVER_INLINE inline __attribute__((noinline))
119 #define fastpath(x) (__builtin_expect(bool(x), 1))
120 #define slowpath(x) (__builtin_expect(bool(x), 0))
123 static ALWAYS_INLINE
uintptr_t
124 addc(uintptr_t lhs
, uintptr_t rhs
, uintptr_t carryin
, uintptr_t *carryout
)
126 return __builtin_addcl(lhs
, rhs
, carryin
, carryout
);
129 static ALWAYS_INLINE
uintptr_t
130 subc(uintptr_t lhs
, uintptr_t rhs
, uintptr_t carryin
, uintptr_t *carryout
)
132 return __builtin_subcl(lhs
, rhs
, carryin
, carryout
);
135 #if __arm64__ && !__arm64e__
139 LoadExclusive(uintptr_t *src
)
141 return __builtin_arm_ldrex(src
);
146 StoreExclusive(uintptr_t *dst
, uintptr_t oldvalue __unused
, uintptr_t value
)
148 return !__builtin_arm_strex(value
, dst
);
154 StoreReleaseExclusive(uintptr_t *dst
, uintptr_t oldvalue __unused
, uintptr_t value
)
156 return !__builtin_arm_stlex(value
, dst
);
161 ClearExclusive(uintptr_t *dst __unused
)
163 __builtin_arm_clrex();
170 LoadExclusive(uintptr_t *src
)
172 return __c11_atomic_load((_Atomic(uintptr_t) *)src
, __ATOMIC_RELAXED
);
177 StoreExclusive(uintptr_t *dst
, uintptr_t oldvalue
, uintptr_t value
)
179 return __c11_atomic_compare_exchange_weak((_Atomic(uintptr_t) *)dst
, &oldvalue
, value
, __ATOMIC_RELAXED
, __ATOMIC_RELAXED
);
185 StoreReleaseExclusive(uintptr_t *dst
, uintptr_t oldvalue
, uintptr_t value
)
187 return __c11_atomic_compare_exchange_weak((_Atomic(uintptr_t) *)dst
, &oldvalue
, value
, __ATOMIC_RELEASE
, __ATOMIC_RELAXED
);
192 ClearExclusive(uintptr_t *dst __unused
)
199 #if !TARGET_OS_IPHONE
200 # include <CrashReporterClient.h>
202 // CrashReporterClient not yet available on iOS
204 extern const char *CRSetCrashLogMessage(const char *msg
);
205 extern const char *CRGetCrashLogMessage(void);
211 # include <algorithm>
212 # include <functional>
216 # define PRIVATE_EXTERN __attribute__((visibility("hidden")))
217 # undef __private_extern__
218 # define __private_extern__ use_PRIVATE_EXTERN_instead
219 # undef private_extern
220 # define private_extern use_PRIVATE_EXTERN_instead
222 /* Use this for functions that are intended to be breakpoint hooks.
223 If you do not, the compiler may optimize them away.
224 BREAKPOINT_FUNCTION( void stop_on_error(void) ); */
225 # define BREAKPOINT_FUNCTION(prototype) \
226 OBJC_EXTERN __attribute__((noinline, used, visibility("hidden"))) \
227 prototype { asm(""); }
229 #elif TARGET_OS_WIN32
231 # define WINVER 0x0501 // target Windows XP and later
232 # define _WIN32_WINNT 0x0501 // target Windows XP and later
233 # define WIN32_LEAN_AND_MEAN
234 // hack: windef.h typedefs BOOL as int
235 # define BOOL WINBOOL
236 # include <windows.h>
246 # include <Availability.h>
250 # include <algorithm>
251 # include <functional>
253 # define __BEGIN_DECLS extern "C" {
254 # define __END_DECLS }
256 # define __BEGIN_DECLS /*empty*/
257 # define __END_DECLS /*empty*/
260 # define PRIVATE_EXTERN
261 # define __attribute__(x)
262 # define inline __inline
264 /* Use this for functions that are intended to be breakpoint hooks.
265 If you do not, the compiler may optimize them away.
266 BREAKPOINT_FUNCTION( void MyBreakpointFunction(void) ); */
267 # define BREAKPOINT_FUNCTION(prototype) \
268 __declspec(noinline) prototype { __asm { } }
270 /* stub out dtrace probes */
271 # define OBJC_RUNTIME_OBJC_EXCEPTION_RETHROW() do {} while(0)
272 # define OBJC_RUNTIME_OBJC_EXCEPTION_THROW(arg0) do {} while(0)
279 #include <objc/objc.h>
280 #include <objc/objc-api.h>
282 extern void _objc_fatal(const char *fmt
, ...)
283 __attribute__((noreturn
, format (printf
, 1, 2)));
284 extern void _objc_fatal_with_reason(uint64_t reason
, uint64_t flags
,
285 const char *fmt
, ...)
286 __attribute__((noreturn
, format (printf
, 3, 4)));
288 #define INIT_ONCE_PTR(var, create, delete) \
291 typeof(var) v = create; \
293 if (OSAtomicCompareAndSwapPtrBarrier(0, (void*)v, (void**)&var)){ \
301 #define INIT_ONCE_32(var, create, delete) \
304 typeof(var) v = create; \
306 if (OSAtomicCompareAndSwap32Barrier(0, v, (volatile int32_t *)&var)) { \
315 // Thread keys reserved by libc for our use.
316 #if defined(__PTK_FRAMEWORK_OBJC_KEY0)
317 # define SUPPORT_DIRECT_THREAD_KEYS 1
318 # define TLS_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY0)
319 # define SYNC_DATA_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY1)
320 # define SYNC_COUNT_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY2)
321 # define AUTORELEASE_POOL_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY3)
322 # if SUPPORT_RETURN_AUTORELEASE
323 # define RETURN_DISPOSITION_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY4)
326 # define SUPPORT_DIRECT_THREAD_KEYS 0
332 // Compiler compatibility
336 #define strdup _strdup
338 #define issetugid() 0
340 #define MIN(x, y) ((x) < (y) ? (x) : (y))
342 static __inline
void bcopy(const void *src
, void *dst
, size_t size
) { memcpy(dst
, src
, size
); }
343 static __inline
void bzero(void *dst
, size_t size
) { memset(dst
, 0, size
); }
345 int asprintf(char **dstp
, const char *format
, ...);
347 typedef void * malloc_zone_t
;
349 static __inline malloc_zone_t
malloc_default_zone(void) { return (malloc_zone_t
)-1; }
350 static __inline
void *malloc_zone_malloc(malloc_zone_t z
, size_t size
) { return malloc(size
); }
351 static __inline
void *malloc_zone_calloc(malloc_zone_t z
, size_t size
, size_t count
) { return calloc(size
, count
); }
352 static __inline
void *malloc_zone_realloc(malloc_zone_t z
, void *p
, size_t size
) { return realloc(p
, size
); }
353 static __inline
void malloc_zone_free(malloc_zone_t z
, void *p
) { free(p
); }
354 static __inline malloc_zone_t
malloc_zone_from_ptr(const void *p
) { return (malloc_zone_t
)-1; }
355 static __inline
size_t malloc_size(const void *p
) { return _msize((void*)p
); /* fixme invalid pointer check? */ }
360 static __inline BOOL
OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
)
362 // fixme barrier is overkill
363 long original
= InterlockedCompareExchange(dst
, newl
, oldl
);
364 return (original
== oldl
);
367 static __inline BOOL
OSAtomicCompareAndSwapPtrBarrier(void *oldp
, void *newp
, void * volatile *dst
)
369 void *original
= InterlockedCompareExchangePointer(dst
, newp
, oldp
);
370 return (original
== oldp
);
373 static __inline BOOL
OSAtomicCompareAndSwap32Barrier(int32_t oldl
, int32_t newl
, int32_t volatile *dst
)
375 long original
= InterlockedCompareExchange((volatile long *)dst
, newl
, oldl
);
376 return (original
== oldl
);
379 static __inline
int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst
)
381 return InterlockedDecrement((volatile long *)dst
);
384 static __inline
int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst
)
386 return InterlockedIncrement((volatile long *)dst
);
390 // Internal data types
392 typedef DWORD objc_thread_t
; // thread ID
393 static __inline
int thread_equal(objc_thread_t t1
, objc_thread_t t2
) {
396 static __inline objc_thread_t
thread_self(void) {
397 return GetCurrentThreadId();
402 void (*dtor
)(void *);
404 static __inline tls_key_t
tls_create(void (*dtor
)(void*)) {
405 // fixme need dtor registry for DllMain to call on thread detach
411 static __inline
void *tls_get(tls_key_t k
) {
412 return TlsGetValue(k
.key
);
414 static __inline
void tls_set(tls_key_t k
, void *value
) {
415 TlsSetValue(k
.key
, value
);
419 CRITICAL_SECTION
*lock
;
421 #define MUTEX_INITIALIZER {0};
422 extern void mutex_init(mutex_t
*m
);
423 static __inline
int _mutex_lock_nodebug(mutex_t
*m
) {
428 EnterCriticalSection(m
->lock
);
431 static __inline
bool _mutex_try_lock_nodebug(mutex_t
*m
) {
436 return TryEnterCriticalSection(m
->lock
);
438 static __inline
int _mutex_unlock_nodebug(mutex_t
*m
) {
440 LeaveCriticalSection(m
->lock
);
445 typedef mutex_t spinlock_t
;
446 #define spinlock_lock(l) mutex_lock(l)
447 #define spinlock_unlock(l) mutex_unlock(l)
448 #define SPINLOCK_INITIALIZER MUTEX_INITIALIZER
454 #define RECURSIVE_MUTEX_INITIALIZER {0};
455 #define RECURSIVE_MUTEX_NOT_LOCKED 1
456 extern void recursive_mutex_init(recursive_mutex_t
*m
);
457 static __inline
int _recursive_mutex_lock_nodebug(recursive_mutex_t
*m
) {
459 return WaitForSingleObject(m
->mutex
, INFINITE
);
461 static __inline
bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t
*m
) {
463 return (WAIT_OBJECT_0
== WaitForSingleObject(m
->mutex
, 0));
465 static __inline
int _recursive_mutex_unlock_nodebug(recursive_mutex_t
*m
) {
467 return ReleaseMutex(m
->mutex
) ? 0 : RECURSIVE_MUTEX_NOT_LOCKED
;
472 typedef HANDLE mutex_t;
473 static inline void mutex_init(HANDLE *m) { *m = CreateMutex(NULL, FALSE, NULL); }
474 static inline void _mutex_lock(mutex_t *m) { WaitForSingleObject(*m, INFINITE); }
475 static inline bool mutex_try_lock(mutex_t *m) { return WaitForSingleObject(*m, 0) == WAIT_OBJECT_0; }
476 static inline void _mutex_unlock(mutex_t *m) { ReleaseMutex(*m); }
479 // based on http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
480 // Vista-only CONDITION_VARIABLE would be better
483 HANDLE waiters
; // semaphore for those in cond_wait()
484 HANDLE waitersDone
; // auto-reset event after everyone gets a broadcast
485 CRITICAL_SECTION waitCountLock
; // guards waitCount and didBroadcast
486 unsigned int waitCount
;
489 #define MONITOR_INITIALIZER { 0 }
490 #define MONITOR_NOT_ENTERED 1
491 extern int monitor_init(monitor_t
*c
);
493 static inline int _monitor_enter_nodebug(monitor_t
*c
) {
495 int err
= monitor_init(c
);
498 return WaitForSingleObject(c
->mutex
, INFINITE
);
500 static inline int _monitor_leave_nodebug(monitor_t
*c
) {
501 if (!ReleaseMutex(c
->mutex
)) return MONITOR_NOT_ENTERED
;
504 static inline int _monitor_wait_nodebug(monitor_t
*c
) {
506 EnterCriticalSection(&c
->waitCountLock
);
508 LeaveCriticalSection(&c
->waitCountLock
);
510 SignalObjectAndWait(c
->mutex
, c
->waiters
, INFINITE
, FALSE
);
512 EnterCriticalSection(&c
->waitCountLock
);
514 last
= c
->didBroadcast
&& c
->waitCount
== 0;
515 LeaveCriticalSection(&c
->waitCountLock
);
518 // tell broadcaster that all waiters have awoken
519 SignalObjectAndWait(c
->waitersDone
, c
->mutex
, INFINITE
, FALSE
);
521 WaitForSingleObject(c
->mutex
, INFINITE
);
524 // fixme error checking
527 static inline int monitor_notify(monitor_t
*c
) {
530 EnterCriticalSection(&c
->waitCountLock
);
531 haveWaiters
= c
->waitCount
> 0;
532 LeaveCriticalSection(&c
->waitCountLock
);
535 ReleaseSemaphore(c
->waiters
, 1, 0);
538 // fixme error checking
541 static inline int monitor_notifyAll(monitor_t
*c
) {
542 EnterCriticalSection(&c
->waitCountLock
);
543 if (c
->waitCount
== 0) {
544 LeaveCriticalSection(&c
->waitCountLock
);
548 ReleaseSemaphore(c
->waiters
, c
->waitCount
, 0);
549 LeaveCriticalSection(&c
->waitCountLock
);
551 // fairness: wait for everyone to move from waiters to mutex
552 WaitForSingleObject(c
->waitersDone
, INFINITE
);
553 // not under waitCountLock, but still under mutex
556 // fixme error checking
561 typedef IMAGE_DOS_HEADER headerType
;
562 // fixme YES bundle? NO bundle? sometimes?
563 #define headerIsBundle(hi) YES
564 OBJC_EXTERN IMAGE_DOS_HEADER __ImageBase
;
565 #define libobjc_header ((headerType *)&__ImageBase)
574 #include <mach-o/loader.h>
576 # define SEGMENT_CMD LC_SEGMENT
578 # define SEGMENT_CMD LC_SEGMENT_64
581 #ifndef VM_MEMORY_OBJC_DISPATCHERS
582 # define VM_MEMORY_OBJC_DISPATCHERS 0
586 // Compiler compatibility
590 static inline uint64_t nanoseconds() {
591 return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW
);
594 // Internal data types
596 typedef pthread_t objc_thread_t
;
598 static __inline
int thread_equal(objc_thread_t t1
, objc_thread_t t2
) {
599 return pthread_equal(t1
, t2
);
601 static __inline objc_thread_t
thread_self(void) {
602 return pthread_self();
606 typedef pthread_key_t tls_key_t
;
608 static inline tls_key_t
tls_create(void (*dtor
)(void*)) {
610 pthread_key_create(&k
, dtor
);
613 static inline void *tls_get(tls_key_t k
) {
614 return pthread_getspecific(k
);
616 static inline void tls_set(tls_key_t k
, void *value
) {
617 pthread_setspecific(k
, value
);
620 #if SUPPORT_DIRECT_THREAD_KEYS
623 static bool is_valid_direct_key(tls_key_t k
) {
624 return ( k
== SYNC_DATA_DIRECT_KEY
625 || k
== SYNC_COUNT_DIRECT_KEY
626 || k
== AUTORELEASE_POOL_KEY
627 # if SUPPORT_RETURN_AUTORELEASE
628 || k
== RETURN_DISPOSITION_KEY
634 static inline void *tls_get_direct(tls_key_t k
)
636 assert(is_valid_direct_key(k
));
638 if (_pthread_has_direct_tsd()) {
639 return _pthread_getspecific_direct(k
);
641 return pthread_getspecific(k
);
644 static inline void tls_set_direct(tls_key_t k
, void *value
)
646 assert(is_valid_direct_key(k
));
648 if (_pthread_has_direct_tsd()) {
649 _pthread_setspecific_direct(k
, value
);
651 pthread_setspecific(k
, value
);
655 // SUPPORT_DIRECT_THREAD_KEYS
659 static inline pthread_t
pthread_self_direct()
662 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF
);
665 static inline mach_port_t
mach_thread_self_direct()
667 return (mach_port_t
)(uintptr_t)
668 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
);
672 template <bool Debug
> class mutex_tt
;
673 template <bool Debug
> class monitor_tt
;
674 template <bool Debug
> class recursive_mutex_tt
;
682 using spinlock_t
= mutex_tt
<LOCKDEBUG
>;
683 using mutex_t
= mutex_tt
<LOCKDEBUG
>;
684 using monitor_t
= monitor_tt
<LOCKDEBUG
>;
685 using recursive_mutex_t
= recursive_mutex_tt
<LOCKDEBUG
>;
687 // Use fork_unsafe_lock to get a lock that isn't
688 // acquired and released around fork().
689 // All fork-safe locks are checked in debug builds.
690 struct fork_unsafe_lock_t
{
691 constexpr fork_unsafe_lock_t() = default;
693 extern const fork_unsafe_lock_t fork_unsafe_lock
;
695 #include "objc-lockdebug.h"
697 template <bool Debug
>
698 class mutex_tt
: nocopy_t
{
699 os_unfair_lock mLock
;
701 constexpr mutex_tt() : mLock(OS_UNFAIR_LOCK_INIT
) {
702 lockdebug_remember_mutex(this);
705 constexpr mutex_tt(const fork_unsafe_lock_t unsafe
) : mLock(OS_UNFAIR_LOCK_INIT
) { }
708 lockdebug_mutex_lock(this);
710 os_unfair_lock_lock_with_options_inline
711 (&mLock
, OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION
);
715 lockdebug_mutex_unlock(this);
717 os_unfair_lock_unlock_inline(&mLock
);
721 lockdebug_mutex_unlock(this);
723 bzero(&mLock
, sizeof(mLock
));
724 mLock
= os_unfair_lock OS_UNFAIR_LOCK_INIT
;
727 void assertLocked() {
728 lockdebug_mutex_assert_locked(this);
731 void assertUnlocked() {
732 lockdebug_mutex_assert_unlocked(this);
736 // Address-ordered lock discipline for a pair of locks.
738 static void lockTwo(mutex_tt
*lock1
, mutex_tt
*lock2
) {
744 if (lock2
!= lock1
) lock1
->lock();
748 static void unlockTwo(mutex_tt
*lock1
, mutex_tt
*lock2
) {
750 if (lock2
!= lock1
) lock2
->unlock();
753 // Scoped lock and unlock
754 class locker
: nocopy_t
{
757 locker(mutex_tt
& newLock
)
758 : lock(newLock
) { lock
.lock(); }
759 ~locker() { lock
.unlock(); }
762 // Either scoped lock and unlock, or NOP.
763 class conditional_locker
: nocopy_t
{
767 conditional_locker(mutex_tt
& newLock
, bool shouldLock
)
768 : lock(newLock
), didLock(shouldLock
)
770 if (shouldLock
) lock
.lock();
772 ~conditional_locker() { if (didLock
) lock
.unlock(); }
776 using mutex_locker_t
= mutex_tt
<LOCKDEBUG
>::locker
;
777 using conditional_mutex_locker_t
= mutex_tt
<LOCKDEBUG
>::conditional_locker
;
780 template <bool Debug
>
781 class recursive_mutex_tt
: nocopy_t
{
782 os_unfair_recursive_lock mLock
;
785 constexpr recursive_mutex_tt() : mLock(OS_UNFAIR_RECURSIVE_LOCK_INIT
) {
786 lockdebug_remember_recursive_mutex(this);
789 constexpr recursive_mutex_tt(const fork_unsafe_lock_t unsafe
)
790 : mLock(OS_UNFAIR_RECURSIVE_LOCK_INIT
)
795 lockdebug_recursive_mutex_lock(this);
796 os_unfair_recursive_lock_lock(&mLock
);
801 lockdebug_recursive_mutex_unlock(this);
803 os_unfair_recursive_lock_unlock(&mLock
);
808 lockdebug_recursive_mutex_unlock(this);
810 bzero(&mLock
, sizeof(mLock
));
811 mLock
= os_unfair_recursive_lock OS_UNFAIR_RECURSIVE_LOCK_INIT
;
816 if (os_unfair_recursive_lock_tryunlock4objc(&mLock
)) {
817 lockdebug_recursive_mutex_unlock(this);
823 void assertLocked() {
824 lockdebug_recursive_mutex_assert_locked(this);
827 void assertUnlocked() {
828 lockdebug_recursive_mutex_assert_unlocked(this);
833 template <bool Debug
>
835 pthread_mutex_t mutex
;
839 constexpr monitor_tt()
840 : mutex(PTHREAD_MUTEX_INITIALIZER
), cond(PTHREAD_COND_INITIALIZER
)
842 lockdebug_remember_monitor(this);
845 monitor_tt(const fork_unsafe_lock_t unsafe
)
846 : mutex(PTHREAD_MUTEX_INITIALIZER
), cond(PTHREAD_COND_INITIALIZER
)
851 lockdebug_monitor_enter(this);
853 int err
= pthread_mutex_lock(&mutex
);
854 if (err
) _objc_fatal("pthread_mutex_lock failed (%d)", err
);
859 lockdebug_monitor_leave(this);
861 int err
= pthread_mutex_unlock(&mutex
);
862 if (err
) _objc_fatal("pthread_mutex_unlock failed (%d)", err
);
867 lockdebug_monitor_wait(this);
869 int err
= pthread_cond_wait(&cond
, &mutex
);
870 if (err
) _objc_fatal("pthread_cond_wait failed (%d)", err
);
875 int err
= pthread_cond_signal(&cond
);
876 if (err
) _objc_fatal("pthread_cond_signal failed (%d)", err
);
881 int err
= pthread_cond_broadcast(&cond
);
882 if (err
) _objc_fatal("pthread_cond_broadcast failed (%d)", err
);
887 lockdebug_monitor_leave(this);
889 bzero(&mutex
, sizeof(mutex
));
890 bzero(&cond
, sizeof(cond
));
891 mutex
= pthread_mutex_t PTHREAD_MUTEX_INITIALIZER
;
892 cond
= pthread_cond_t PTHREAD_COND_INITIALIZER
;
897 lockdebug_monitor_assert_locked(this);
900 void assertUnlocked()
902 lockdebug_monitor_assert_unlocked(this);
907 // semaphore_create formatted for INIT_ONCE use
908 static inline semaphore_t
create_semaphore(void)
912 k
= semaphore_create(mach_task_self(), &sem
, SYNC_POLICY_FIFO
, 0);
913 if (k
) _objc_fatal("semaphore_create failed (0x%x)", k
);
919 typedef struct mach_header headerType
;
920 typedef struct segment_command segmentType
;
921 typedef struct section sectionType
;
923 typedef struct mach_header_64 headerType
;
924 typedef struct segment_command_64 segmentType
;
925 typedef struct section_64 sectionType
;
927 #define headerIsBundle(hi) (hi->mhdr()->filetype == MH_BUNDLE)
928 #define libobjc_header ((headerType *)&_mh_dylib_header)
932 /* Secure /tmp usage */
933 extern int secure_open(const char *filename
, int flags
, uid_t euid
);
946 memdup(const void *mem
, size_t len
)
948 void *dup
= malloc(len
);
949 memcpy(dup
, mem
, len
);
953 // strdup that doesn't copy read-only memory
955 strdupIfMutable(const char *str
)
957 size_t size
= strlen(str
) + 1;
958 if (_dyld_is_memory_immutable(str
, size
)) {
961 return (char *)memdup(str
, size
);
965 // free strdupIfMutable() result
967 freeIfMutable(char *str
)
969 size_t size
= strlen(str
) + 1;
970 if (_dyld_is_memory_immutable(str
, size
)) {
977 // nil-checking unsigned strdup
978 static inline uint8_t *
979 ustrdupMaybeNil(const uint8_t *str
)
981 if (!str
) return nil
;
982 return (uint8_t *)strdupIfMutable((char *)str
);
985 // OS version checking:
988 // DYLD_OS_VERSION(mac, ios, tv, watch, bridge)
989 // sdkIsOlderThan(mac, ios, tv, watch, bridge)
990 // sdkIsAtLeast(mac, ios, tv, watch, bridge)
992 // This version order matches OBJC_AVAILABLE.
995 # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_MACOSX_VERSION_##x
996 # define sdkVersion() dyld_get_program_sdk_version()
999 # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##i
1000 # define sdkVersion() dyld_get_program_sdk_version()
1003 // dyld does not currently have distinct constants for tvOS
1004 # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##t
1005 # define sdkVersion() dyld_get_program_sdk_version()
1007 #elif TARGET_OS_BRIDGE
1008 # if TARGET_OS_WATCH
1009 # error bridgeOS 1.0 not supported
1011 // fixme don't need bridgeOS versioning yet
1012 # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##t
1013 # define sdkVersion() dyld_get_program_sdk_bridge_os_version()
1015 #elif TARGET_OS_WATCH
1016 # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_WATCHOS_VERSION_##w
1017 // watchOS has its own API for compatibility reasons
1018 # define sdkVersion() dyld_get_program_sdk_watch_os_version()
1025 #define sdkIsOlderThan(x, i, t, w, b) \
1026 (sdkVersion() < DYLD_OS_VERSION(x, i, t, w, b))
1027 #define sdkIsAtLeast(x, i, t, w, b) \
1028 (sdkVersion() >= DYLD_OS_VERSION(x, i, t, w, b))
1030 // Allow bare 0 to be used in DYLD_OS_VERSION() and sdkIsOlderThan()
1031 #define DYLD_MACOSX_VERSION_0 0
1032 #define DYLD_IOS_VERSION_0 0
1033 #define DYLD_TVOS_VERSION_0 0
1034 #define DYLD_WATCHOS_VERSION_0 0
1035 #define DYLD_BRIDGEOS_VERSION_0 0
1037 // Pretty-print a DYLD_*_VERSION_* constant.
1038 #define SDK_FORMAT "%hu.%hhu.%hhu"
1039 #define FORMAT_SDK(v) \
1040 (unsigned short)(((uint32_t)(v))>>16), \
1041 (unsigned char)(((uint32_t)(v))>>8), \
1042 (unsigned char)(((uint32_t)(v))>>0)
1044 // fork() safety requires careful tracking of all locks.
1045 // Our custom lock types check this in debug builds.
1046 // Disallow direct use of all other lock types.
1047 typedef __darwin_pthread_mutex_t pthread_mutex_t UNAVAILABLE_ATTRIBUTE
;
1048 typedef __darwin_pthread_rwlock_t pthread_rwlock_t UNAVAILABLE_ATTRIBUTE
;
1049 typedef int32_t OSSpinLock UNAVAILABLE_ATTRIBUTE
;
1050 typedef struct os_unfair_lock_s os_unfair_lock UNAVAILABLE_ATTRIBUTE
;