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
;
54 # ifndef __STDC_LIMIT_MACROS
55 # define __STDC_LIMIT_MACROS
72 # include <crt_externs.h>
73 # include <AssertMacros.h>
75 # include <Availability.h>
76 # include <TargetConditionals.h>
77 # include <sys/mman.h>
78 # include <sys/time.h>
79 # include <sys/stat.h>
80 # include <sys/param.h>
81 # include <mach/mach.h>
82 # include <mach/vm_param.h>
83 # include <mach-o/dyld.h>
84 # include <mach-o/ldsyms.h>
85 # include <mach-o/loader.h>
86 # include <mach-o/getsect.h>
87 # include <mach-o/dyld_priv.h>
88 # include <malloc/malloc.h>
89 # include <os/lock_private.h>
90 # include <libkern/OSAtomic.h>
91 # include <libkern/OSCacheControl.h>
92 # include <System/pthread_machdep.h>
93 # include "objc-probes.h" // generated dtrace probe definitions.
95 // Some libc functions call objc_msgSend()
96 // so we can't use them without deadlocks.
97 void syslog(int, const char *, ...) UNAVAILABLE_ATTRIBUTE
;
98 void vsyslog(int, const char *, va_list) UNAVAILABLE_ATTRIBUTE
;
101 #define ALWAYS_INLINE inline __attribute__((always_inline))
102 #define NEVER_INLINE inline __attribute__((noinline))
106 static ALWAYS_INLINE
uintptr_t
107 addc(uintptr_t lhs
, uintptr_t rhs
, uintptr_t carryin
, uintptr_t *carryout
)
109 return __builtin_addcl(lhs
, rhs
, carryin
, carryout
);
112 static ALWAYS_INLINE
uintptr_t
113 subc(uintptr_t lhs
, uintptr_t rhs
, uintptr_t carryin
, uintptr_t *carryout
)
115 return __builtin_subcl(lhs
, rhs
, carryin
, carryout
);
123 LoadExclusive(uintptr_t *src
)
126 asm("ldxr %x0, [%x1]"
128 : "r" (src
), "m" (*src
));
134 StoreExclusive(uintptr_t *dst
, uintptr_t oldvalue __unused
, uintptr_t value
)
137 asm("stxr %w0, %x2, [%x3]"
138 : "=r" (result
), "=m" (*dst
)
139 : "r" (value
), "r" (dst
));
146 StoreReleaseExclusive(uintptr_t *dst
, uintptr_t oldvalue __unused
, uintptr_t value
)
149 asm("stlxr %w0, %x2, [%x3]"
150 : "=r" (result
), "=m" (*dst
)
151 : "r" (value
), "r" (dst
));
160 LoadExclusive(uintptr_t *src
)
167 StoreExclusive(uintptr_t *dst
, uintptr_t oldvalue
, uintptr_t value
)
169 return OSAtomicCompareAndSwapPtr((void *)oldvalue
, (void *)value
,
175 StoreReleaseExclusive(uintptr_t *dst
, uintptr_t oldvalue
, uintptr_t value
)
177 return OSAtomicCompareAndSwapPtrBarrier((void *)oldvalue
, (void *)value
,
182 #elif __x86_64__ || __i386__
186 LoadExclusive(uintptr_t *src
)
193 StoreExclusive(uintptr_t *dst
, uintptr_t oldvalue
, uintptr_t value
)
196 return __sync_bool_compare_and_swap((void **)dst
, (void *)oldvalue
, (void *)value
);
201 StoreReleaseExclusive(uintptr_t *dst
, uintptr_t oldvalue
, uintptr_t value
)
203 return StoreExclusive(dst
, oldvalue
, value
);
207 # error unknown architecture
211 #define spinlock_t os_lock_handoff_s
212 #define spinlock_trylock(l) os_lock_trylock(l)
213 #define spinlock_lock(l) os_lock_lock(l)
214 #define spinlock_unlock(l) os_lock_unlock(l)
215 #define SPINLOCK_INITIALIZER OS_LOCK_HANDOFF_INIT
218 #if !TARGET_OS_IPHONE
219 # include <CrashReporterClient.h>
221 // CrashReporterClient not yet available on iOS
223 extern const char *CRSetCrashLogMessage(const char *msg
);
224 extern const char *CRGetCrashLogMessage(void);
225 extern const char *CRSetCrashLogMessage2(const char *msg
);
231 # include <algorithm>
232 # include <functional>
236 # define PRIVATE_EXTERN __attribute__((visibility("hidden")))
237 # undef __private_extern__
238 # define __private_extern__ use_PRIVATE_EXTERN_instead
239 # undef private_extern
240 # define private_extern use_PRIVATE_EXTERN_instead
242 /* Use this for functions that are intended to be breakpoint hooks.
243 If you do not, the compiler may optimize them away.
244 BREAKPOINT_FUNCTION( void stop_on_error(void) ); */
245 # define BREAKPOINT_FUNCTION(prototype) \
246 OBJC_EXTERN __attribute__((noinline, used, visibility("hidden"))) \
247 prototype { asm(""); }
249 #elif TARGET_OS_WIN32
251 # define WINVER 0x0501 // target Windows XP and later
252 # define _WIN32_WINNT 0x0501 // target Windows XP and later
253 # define WIN32_LEAN_AND_MEAN
254 // hack: windef.h typedefs BOOL as int
255 # define BOOL WINBOOL
256 # include <windows.h>
266 # include <Availability.h>
270 # include <algorithm>
271 # include <functional>
273 # define __BEGIN_DECLS extern "C" {
274 # define __END_DECLS }
276 # define __BEGIN_DECLS /*empty*/
277 # define __END_DECLS /*empty*/
280 # define PRIVATE_EXTERN
281 # define __attribute__(x)
282 # define inline __inline
284 /* Use this for functions that are intended to be breakpoint hooks.
285 If you do not, the compiler may optimize them away.
286 BREAKPOINT_FUNCTION( void MyBreakpointFunction(void) ); */
287 # define BREAKPOINT_FUNCTION(prototype) \
288 __declspec(noinline) prototype { __asm { } }
290 /* stub out dtrace probes */
291 # define OBJC_RUNTIME_OBJC_EXCEPTION_RETHROW() do {} while(0)
292 # define OBJC_RUNTIME_OBJC_EXCEPTION_THROW(arg0) do {} while(0)
299 #include <objc/objc.h>
300 #include <objc/objc-api.h>
304 extern void _objc_fatal(const char *fmt
, ...) __attribute__((noreturn
, format (printf
, 1, 2)));
306 #define INIT_ONCE_PTR(var, create, delete) \
309 typeof(var) v = create; \
311 if (OSAtomicCompareAndSwapPtrBarrier(0, (void*)v, (void**)&var)){ \
319 #define INIT_ONCE_32(var, create, delete) \
322 typeof(var) v = create; \
324 if (OSAtomicCompareAndSwap32Barrier(0, v, (volatile int32_t *)&var)) { \
333 // Thread keys reserved by libc for our use.
334 #if defined(__PTK_FRAMEWORK_OBJC_KEY0)
335 # define SUPPORT_DIRECT_THREAD_KEYS 1
336 # define TLS_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY0)
337 # define SYNC_DATA_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY1)
338 # define SYNC_COUNT_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY2)
339 # define AUTORELEASE_POOL_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY3)
340 # if SUPPORT_RETURN_AUTORELEASE
341 # define AUTORELEASE_POOL_RECLAIM_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY4)
343 # if SUPPORT_QOS_HACK
344 # define QOS_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY5)
347 # define SUPPORT_DIRECT_THREAD_KEYS 0
353 // Compiler compatibility
357 #define strdup _strdup
359 #define issetugid() 0
361 #define MIN(x, y) ((x) < (y) ? (x) : (y))
363 static __inline
void bcopy(const void *src
, void *dst
, size_t size
) { memcpy(dst
, src
, size
); }
364 static __inline
void bzero(void *dst
, size_t size
) { memset(dst
, 0, size
); }
366 int asprintf(char **dstp
, const char *format
, ...);
368 typedef void * malloc_zone_t
;
370 static __inline malloc_zone_t
malloc_default_zone(void) { return (malloc_zone_t
)-1; }
371 static __inline
void *malloc_zone_malloc(malloc_zone_t z
, size_t size
) { return malloc(size
); }
372 static __inline
void *malloc_zone_calloc(malloc_zone_t z
, size_t size
, size_t count
) { return calloc(size
, count
); }
373 static __inline
void *malloc_zone_realloc(malloc_zone_t z
, void *p
, size_t size
) { return realloc(p
, size
); }
374 static __inline
void malloc_zone_free(malloc_zone_t z
, void *p
) { free(p
); }
375 static __inline malloc_zone_t
malloc_zone_from_ptr(const void *p
) { return (malloc_zone_t
)-1; }
376 static __inline
size_t malloc_size(const void *p
) { return _msize((void*)p
); /* fixme invalid pointer check? */ }
381 #define require_action_string(cond, dest, act, msg) do { if (!(cond)) { { act; } goto dest; } } while (0)
382 #define require_noerr_string(err, dest, msg) do { if (err) goto dest; } while (0)
383 #define require_string(cond, dest, msg) do { if (!(cond)) goto dest; } while (0)
388 static __inline BOOL
OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
)
390 // fixme barrier is overkill
391 long original
= InterlockedCompareExchange(dst
, newl
, oldl
);
392 return (original
== oldl
);
395 static __inline BOOL
OSAtomicCompareAndSwapPtrBarrier(void *oldp
, void *newp
, void * volatile *dst
)
397 void *original
= InterlockedCompareExchangePointer(dst
, newp
, oldp
);
398 return (original
== oldp
);
401 static __inline BOOL
OSAtomicCompareAndSwap32Barrier(int32_t oldl
, int32_t newl
, int32_t volatile *dst
)
403 long original
= InterlockedCompareExchange((volatile long *)dst
, newl
, oldl
);
404 return (original
== oldl
);
407 static __inline
int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst
)
409 return InterlockedDecrement((volatile long *)dst
);
412 static __inline
int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst
)
414 return InterlockedIncrement((volatile long *)dst
);
418 // Internal data types
420 typedef DWORD objc_thread_t
; // thread ID
421 static __inline
int thread_equal(objc_thread_t t1
, objc_thread_t t2
) {
424 static __inline objc_thread_t
thread_self(void) {
425 return GetCurrentThreadId();
430 void (*dtor
)(void *);
432 static __inline tls_key_t
tls_create(void (*dtor
)(void*)) {
433 // fixme need dtor registry for DllMain to call on thread detach
439 static __inline
void *tls_get(tls_key_t k
) {
440 return TlsGetValue(k
.key
);
442 static __inline
void tls_set(tls_key_t k
, void *value
) {
443 TlsSetValue(k
.key
, value
);
447 CRITICAL_SECTION
*lock
;
449 #define MUTEX_INITIALIZER {0};
450 extern void mutex_init(mutex_t
*m
);
451 static __inline
int _mutex_lock_nodebug(mutex_t
*m
) {
456 EnterCriticalSection(m
->lock
);
459 static __inline
bool _mutex_try_lock_nodebug(mutex_t
*m
) {
464 return TryEnterCriticalSection(m
->lock
);
466 static __inline
int _mutex_unlock_nodebug(mutex_t
*m
) {
468 LeaveCriticalSection(m
->lock
);
473 typedef mutex_t spinlock_t
;
474 #define spinlock_lock(l) mutex_lock(l)
475 #define spinlock_unlock(l) mutex_unlock(l)
476 #define SPINLOCK_INITIALIZER MUTEX_INITIALIZER
482 #define RECURSIVE_MUTEX_INITIALIZER {0};
483 #define RECURSIVE_MUTEX_NOT_LOCKED 1
484 extern void recursive_mutex_init(recursive_mutex_t
*m
);
485 static __inline
int _recursive_mutex_lock_nodebug(recursive_mutex_t
*m
) {
487 return WaitForSingleObject(m
->mutex
, INFINITE
);
489 static __inline
bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t
*m
) {
491 return (WAIT_OBJECT_0
== WaitForSingleObject(m
->mutex
, 0));
493 static __inline
int _recursive_mutex_unlock_nodebug(recursive_mutex_t
*m
) {
495 return ReleaseMutex(m
->mutex
) ? 0 : RECURSIVE_MUTEX_NOT_LOCKED
;
500 typedef HANDLE mutex_t;
501 static inline void mutex_init(HANDLE *m) { *m = CreateMutex(NULL, FALSE, NULL); }
502 static inline void _mutex_lock(mutex_t *m) { WaitForSingleObject(*m, INFINITE); }
503 static inline bool mutex_try_lock(mutex_t *m) { return WaitForSingleObject(*m, 0) == WAIT_OBJECT_0; }
504 static inline void _mutex_unlock(mutex_t *m) { ReleaseMutex(*m); }
507 // based on http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
508 // Vista-only CONDITION_VARIABLE would be better
511 HANDLE waiters
; // semaphore for those in cond_wait()
512 HANDLE waitersDone
; // auto-reset event after everyone gets a broadcast
513 CRITICAL_SECTION waitCountLock
; // guards waitCount and didBroadcast
514 unsigned int waitCount
;
517 #define MONITOR_INITIALIZER { 0 }
518 #define MONITOR_NOT_ENTERED 1
519 extern int monitor_init(monitor_t
*c
);
521 static inline int _monitor_enter_nodebug(monitor_t
*c
) {
523 int err
= monitor_init(c
);
526 return WaitForSingleObject(c
->mutex
, INFINITE
);
528 static inline int _monitor_exit_nodebug(monitor_t
*c
) {
529 if (!ReleaseMutex(c
->mutex
)) return MONITOR_NOT_ENTERED
;
532 static inline int _monitor_wait_nodebug(monitor_t
*c
) {
534 EnterCriticalSection(&c
->waitCountLock
);
536 LeaveCriticalSection(&c
->waitCountLock
);
538 SignalObjectAndWait(c
->mutex
, c
->waiters
, INFINITE
, FALSE
);
540 EnterCriticalSection(&c
->waitCountLock
);
542 last
= c
->didBroadcast
&& c
->waitCount
== 0;
543 LeaveCriticalSection(&c
->waitCountLock
);
546 // tell broadcaster that all waiters have awoken
547 SignalObjectAndWait(c
->waitersDone
, c
->mutex
, INFINITE
, FALSE
);
549 WaitForSingleObject(c
->mutex
, INFINITE
);
552 // fixme error checking
555 static inline int monitor_notify(monitor_t
*c
) {
558 EnterCriticalSection(&c
->waitCountLock
);
559 haveWaiters
= c
->waitCount
> 0;
560 LeaveCriticalSection(&c
->waitCountLock
);
563 ReleaseSemaphore(c
->waiters
, 1, 0);
566 // fixme error checking
569 static inline int monitor_notifyAll(monitor_t
*c
) {
570 EnterCriticalSection(&c
->waitCountLock
);
571 if (c
->waitCount
== 0) {
572 LeaveCriticalSection(&c
->waitCountLock
);
576 ReleaseSemaphore(c
->waiters
, c
->waitCount
, 0);
577 LeaveCriticalSection(&c
->waitCountLock
);
579 // fairness: wait for everyone to move from waiters to mutex
580 WaitForSingleObject(c
->waitersDone
, INFINITE
);
581 // not under waitCountLock, but still under mutex
584 // fixme error checking
589 // fixme no rwlock yet
591 #define rwlock_t mutex_t
592 #define rwlock_init(r) mutex_init(r)
593 #define _rwlock_read_nodebug(m) _mutex_lock_nodebug(m)
594 #define _rwlock_write_nodebug(m) _mutex_lock_nodebug(m)
595 #define _rwlock_try_read_nodebug(m) _mutex_try_lock_nodebug(m)
596 #define _rwlock_try_write_nodebug(m) _mutex_try_lock_nodebug(m)
597 #define _rwlock_unlock_read_nodebug(m) _mutex_unlock_nodebug(m)
598 #define _rwlock_unlock_write_nodebug(m) _mutex_unlock_nodebug(m)
601 typedef IMAGE_DOS_HEADER headerType
;
602 // fixme YES bundle? NO bundle? sometimes?
603 #define headerIsBundle(hi) YES
604 OBJC_EXTERN IMAGE_DOS_HEADER __ImageBase
;
605 #define libobjc_header ((headerType *)&__ImageBase)
614 #include <mach-o/loader.h>
616 # define SEGMENT_CMD LC_SEGMENT
618 # define SEGMENT_CMD LC_SEGMENT_64
621 #ifndef VM_MEMORY_OBJC_DISPATCHERS
622 # define VM_MEMORY_OBJC_DISPATCHERS 0
626 // Compiler compatibility
630 // Internal data types
632 typedef pthread_t objc_thread_t
;
634 static __inline
int thread_equal(objc_thread_t t1
, objc_thread_t t2
) {
635 return pthread_equal(t1
, t2
);
637 static __inline objc_thread_t
thread_self(void) {
638 return pthread_self();
642 typedef pthread_key_t tls_key_t
;
644 static inline tls_key_t
tls_create(void (*dtor
)(void*)) {
646 pthread_key_create(&k
, dtor
);
649 static inline void *tls_get(tls_key_t k
) {
650 return pthread_getspecific(k
);
652 static inline void tls_set(tls_key_t k
, void *value
) {
653 pthread_setspecific(k
, value
);
656 #if SUPPORT_DIRECT_THREAD_KEYS
659 static bool is_valid_direct_key(tls_key_t k
) {
660 return ( k
== SYNC_DATA_DIRECT_KEY
661 || k
== SYNC_COUNT_DIRECT_KEY
662 || k
== AUTORELEASE_POOL_KEY
663 # if SUPPORT_RETURN_AUTORELEASE
664 || k
== AUTORELEASE_POOL_RECLAIM_KEY
666 # if SUPPORT_QOS_HACK
675 // rdar://9162780 _pthread_get/setspecific_direct are inefficient
676 // copied from libdispatch
678 __attribute__((const))
679 static ALWAYS_INLINE
void**
683 #if defined(__arm__) && defined(_ARM_ARCH_6)
684 __asm__("mrc p15, 0, %[p], c13, c0, 3" : [p
] "=&r" (p
));
685 return (void**)(p
& ~0x3ul
);
687 #error tls_base not implemented
692 static ALWAYS_INLINE
void
693 tls_set_direct(void **tsdb
, tls_key_t k
, void *v
)
695 assert(is_valid_direct_key(k
));
699 #define tls_set_direct(k, v) \
700 tls_set_direct(tls_base(), (k), (v))
703 static ALWAYS_INLINE
void *
704 tls_get_direct(void **tsdb
, tls_key_t k
)
706 assert(is_valid_direct_key(k
));
710 #define tls_get_direct(k) \
711 tls_get_direct(tls_base(), (k))
717 static inline void *tls_get_direct(tls_key_t k
)
719 assert(is_valid_direct_key(k
));
721 if (_pthread_has_direct_tsd()) {
722 return _pthread_getspecific_direct(k
);
724 return pthread_getspecific(k
);
727 static inline void tls_set_direct(tls_key_t k
, void *value
)
729 assert(is_valid_direct_key(k
));
731 if (_pthread_has_direct_tsd()) {
732 _pthread_setspecific_direct(k
, value
);
734 pthread_setspecific(k
, value
);
741 // SUPPORT_DIRECT_THREAD_KEYS
745 static inline pthread_t
pthread_self_direct()
748 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF
);
751 static inline mach_port_t
mach_thread_self_direct()
753 return (mach_port_t
)(uintptr_t)
754 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
);
758 static inline pthread_priority_t
pthread_self_priority_direct()
760 pthread_priority_t pri
= (pthread_priority_t
)
761 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
);
762 return pri
& ~_PTHREAD_PRIORITY_FLAGS_MASK
;
767 typedef pthread_mutex_t mutex_t
;
768 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER;
770 static inline int _mutex_lock_nodebug(mutex_t
*m
) {
771 return pthread_mutex_lock(m
);
773 static inline bool _mutex_try_lock_nodebug(mutex_t
*m
) {
774 return !pthread_mutex_trylock(m
);
776 static inline int _mutex_unlock_nodebug(mutex_t
*m
) {
777 return pthread_mutex_unlock(m
);
782 pthread_mutex_t
*mutex
;
784 #define RECURSIVE_MUTEX_INITIALIZER {0};
785 #define RECURSIVE_MUTEX_NOT_LOCKED EPERM
786 extern void recursive_mutex_init(recursive_mutex_t
*m
);
788 static inline int _recursive_mutex_lock_nodebug(recursive_mutex_t
*m
) {
790 return pthread_mutex_lock(m
->mutex
);
792 static inline bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t
*m
) {
794 return !pthread_mutex_trylock(m
->mutex
);
796 static inline int _recursive_mutex_unlock_nodebug(recursive_mutex_t
*m
) {
798 return pthread_mutex_unlock(m
->mutex
);
803 pthread_mutex_t mutex
;
806 #define MONITOR_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER }
807 #define MONITOR_NOT_ENTERED EPERM
809 static inline int monitor_init(monitor_t
*c
) {
810 int err
= pthread_mutex_init(&c
->mutex
, NULL
);
812 err
= pthread_cond_init(&c
->cond
, NULL
);
814 pthread_mutex_destroy(&c
->mutex
);
819 static inline int _monitor_enter_nodebug(monitor_t
*c
) {
820 return pthread_mutex_lock(&c
->mutex
);
822 static inline int _monitor_exit_nodebug(monitor_t
*c
) {
823 return pthread_mutex_unlock(&c
->mutex
);
825 static inline int _monitor_wait_nodebug(monitor_t
*c
) {
826 return pthread_cond_wait(&c
->cond
, &c
->mutex
);
828 static inline int monitor_notify(monitor_t
*c
) {
829 return pthread_cond_signal(&c
->cond
);
831 static inline int monitor_notifyAll(monitor_t
*c
) {
832 return pthread_cond_broadcast(&c
->cond
);
836 // semaphore_create formatted for INIT_ONCE use
837 static inline semaphore_t
create_semaphore(void)
841 k
= semaphore_create(mach_task_self(), &sem
, SYNC_POLICY_FIFO
, 0);
842 if (k
) _objc_fatal("semaphore_create failed (0x%x)", k
);
848 // Override QOS class to avoid priority inversion in rwlocks
849 // <rdar://17697862> do a qos override before taking rw lock in objc
851 #include <pthread/workqueue_private.h>
852 extern pthread_priority_t BackgroundPriority
;
853 extern pthread_priority_t MainPriority
;
855 static inline void qosStartOverride()
857 uintptr_t overrideRefCount
= (uintptr_t)tls_get_direct(QOS_KEY
);
858 if (overrideRefCount
> 0) {
859 // If there is a qos override, increment the refcount and continue
860 tls_set_direct(QOS_KEY
, (void *)(overrideRefCount
+ 1));
863 pthread_priority_t currentPriority
= pthread_self_priority_direct();
864 // Check if override is needed. Only override if we are background qos
865 if (currentPriority
<= BackgroundPriority
) {
866 int res __unused
= _pthread_override_qos_class_start_direct(mach_thread_self_direct(), MainPriority
);
868 // Once we override, we set the reference count in the tsd
869 // to know when to end the override
870 tls_set_direct(QOS_KEY
, (void *)1);
875 static inline void qosEndOverride()
877 uintptr_t overrideRefCount
= (uintptr_t)tls_get_direct(QOS_KEY
);
878 if (overrideRefCount
== 0) return;
880 if (overrideRefCount
== 1) {
882 int res __unused
= _pthread_override_qos_class_end_direct(mach_thread_self_direct());
886 // decrement refcount
887 tls_set_direct(QOS_KEY
, (void *)(overrideRefCount
- 1));
892 // not SUPPORT_QOS_HACK
894 static inline void qosStartOverride() { }
895 static inline void qosEndOverride() { }
897 // not SUPPORT_QOS_HACK
900 /* Custom read-write lock
901 - reader is atomic add/subtract
902 - writer is pthread mutex plus atomic add/subtract
903 - fairness: new readers wait if a writer wants in
904 - fairness: when writer completes, readers (probably) precede new writer
906 state: xxxxxxxx xxxxxxxx yyyyyyyy yyyyyyyz
907 x: blocked reader count
908 y: active reader count
909 z: readers allowed flag
912 pthread_rwlock_t rwl
;
915 static inline void rwlock_init(rwlock_t
*l
)
917 int err __unused
= pthread_rwlock_init(&l
->rwl
, NULL
);
921 static inline void _rwlock_read_nodebug(rwlock_t
*l
)
924 int err __unused
= pthread_rwlock_rdlock(&l
->rwl
);
928 static inline void _rwlock_unlock_read_nodebug(rwlock_t
*l
)
930 int err __unused
= pthread_rwlock_unlock(&l
->rwl
);
936 static inline bool _rwlock_try_read_nodebug(rwlock_t
*l
)
939 int err
= pthread_rwlock_tryrdlock(&l
->rwl
);
940 assert(err
== 0 || err
== EBUSY
|| err
== EAGAIN
);
950 static inline void _rwlock_write_nodebug(rwlock_t
*l
)
953 int err __unused
= pthread_rwlock_wrlock(&l
->rwl
);
957 static inline void _rwlock_unlock_write_nodebug(rwlock_t
*l
)
959 int err __unused
= pthread_rwlock_unlock(&l
->rwl
);
964 static inline bool _rwlock_try_write_nodebug(rwlock_t
*l
)
967 int err
= pthread_rwlock_trywrlock(&l
->rwl
);
968 assert(err
== 0 || err
== EBUSY
);
979 typedef struct mach_header headerType
;
980 typedef struct segment_command segmentType
;
981 typedef struct section sectionType
;
983 typedef struct mach_header_64 headerType
;
984 typedef struct segment_command_64 segmentType
;
985 typedef struct section_64 sectionType
;
987 #define headerIsBundle(hi) (hi->mhdr->filetype == MH_BUNDLE)
988 #define libobjc_header ((headerType *)&_mh_dylib_header)
992 /* Secure /tmp usage */
993 extern int secure_open(const char *filename
, int flags
, uid_t euid
);