1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111 |
- /*
- * Copyright (c) 2007 Apple Inc. All Rights Reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
- *
- * @APPLE_LICENSE_HEADER_END@
- */
- /***********************************************************************
- * objc-os.h
- * OS portability layer.
- **********************************************************************/
- #ifndef _OBJC_OS_H
- #define _OBJC_OS_H
- #include <TargetConditionals.h>
- #include "objc-config.h"
- #ifdef __LP64__
- # define WORD_SHIFT 3UL
- # define WORD_MASK 7UL
- # define WORD_BITS 64
- #else
- # define WORD_SHIFT 2UL
- # define WORD_MASK 3UL
- # define WORD_BITS 32
- #endif
- static inline uint32_t word_align(uint32_t x) {
- return (x + WORD_MASK) & ~WORD_MASK;
- }
- static inline size_t word_align(size_t x) {
- return (x + WORD_MASK) & ~WORD_MASK;
- }
- // Mix-in for classes that must not be copied.
- class nocopy_t {
- private:
- nocopy_t(const nocopy_t&) = delete;
- const nocopy_t& operator=(const nocopy_t&) = delete;
- protected:
- constexpr nocopy_t() = default;
- ~nocopy_t() = default;
- };
- #if TARGET_OS_MAC
- # define OS_UNFAIR_LOCK_INLINE 1
- # ifndef __STDC_LIMIT_MACROS
- # define __STDC_LIMIT_MACROS
- # endif
- # include <stdio.h>
- # include <stdlib.h>
- # include <stdint.h>
- # include <stdarg.h>
- # include <string.h>
- # include <ctype.h>
- # include <errno.h>
- # include <dlfcn.h>
- # include <fcntl.h>
- # include <assert.h>
- # include <limits.h>
- # include <syslog.h>
- # include <unistd.h>
- # include <pthread.h>
- # include <crt_externs.h>
- # undef check
- # include <Availability.h>
- # include <TargetConditionals.h>
- # include <sys/mman.h>
- # include <sys/time.h>
- # include <sys/stat.h>
- # include <sys/param.h>
- # include <sys/reason.h>
- # include <mach/mach.h>
- # include <mach/vm_param.h>
- # include <mach/mach_time.h>
- # include <mach-o/dyld.h>
- # include <mach-o/ldsyms.h>
- # include <mach-o/loader.h>
- # include <mach-o/getsect.h>
- # include <mach-o/dyld_priv.h>
- # include <malloc/malloc.h>
- # include <os/lock_private.h>
- # include <libkern/OSAtomic.h>
- # include <libkern/OSCacheControl.h>
- # include <System/pthread_machdep.h>
- # include "objc-probes.h" // generated dtrace probe definitions.
- // Some libc functions call objc_msgSend()
- // so we can't use them without deadlocks.
- void syslog(int, const char *, ...) UNAVAILABLE_ATTRIBUTE;
- void vsyslog(int, const char *, va_list) UNAVAILABLE_ATTRIBUTE;
- #define ALWAYS_INLINE inline __attribute__((always_inline))
- #define NEVER_INLINE inline __attribute__((noinline))
- #define fastpath(x) (__builtin_expect(bool(x), 1))
- #define slowpath(x) (__builtin_expect(bool(x), 0))
- static ALWAYS_INLINE uintptr_t
- addc(uintptr_t lhs, uintptr_t rhs, uintptr_t carryin, uintptr_t *carryout)
- {
- return __builtin_addcl(lhs, rhs, carryin, carryout);
- }
- static ALWAYS_INLINE uintptr_t
- subc(uintptr_t lhs, uintptr_t rhs, uintptr_t carryin, uintptr_t *carryout)
- {
- return __builtin_subcl(lhs, rhs, carryin, carryout);
- }
- #if __arm64__
- // Pointer-size register prefix for inline asm
- # if __LP64__
- # define p "x" // true arm64
- # else
- # define p "w" // arm64_32
- # endif
- static ALWAYS_INLINE
- uintptr_t
- LoadExclusive(uintptr_t *src)
- {
- uintptr_t result;
- asm("ldxr %" p "0, [%x1]"
- : "=r" (result)
- : "r" (src), "m" (*src));
- return result;
- }
- static ALWAYS_INLINE
- bool
- StoreExclusive(uintptr_t *dst, uintptr_t oldvalue __unused, uintptr_t value)
- {
- uint32_t result;
- asm("stxr %w0, %" p "2, [%x3]"
- : "=&r" (result), "=m" (*dst)
- : "r" (value), "r" (dst));
- return !result;
- }
- static ALWAYS_INLINE
- bool
- StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue __unused, uintptr_t value)
- {
- uint32_t result;
- asm("stlxr %w0, %" p "2, [%x3]"
- : "=&r" (result), "=m" (*dst)
- : "r" (value), "r" (dst));
- return !result;
- }
- static ALWAYS_INLINE
- void
- ClearExclusive(uintptr_t *dst)
- {
- // pretend it writes to *dst for instruction ordering purposes
- asm("clrex" : "=m" (*dst));
- }
- #undef p
- #elif __arm__
- static ALWAYS_INLINE
- uintptr_t
- LoadExclusive(uintptr_t *src)
- {
- return *src;
- }
- static ALWAYS_INLINE
- bool
- StoreExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
- {
- return OSAtomicCompareAndSwapPtr((void *)oldvalue, (void *)value,
- (void **)dst);
- }
- static ALWAYS_INLINE
- bool
- StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
- {
- return OSAtomicCompareAndSwapPtrBarrier((void *)oldvalue, (void *)value,
- (void **)dst);
- }
- static ALWAYS_INLINE
- void
- ClearExclusive(uintptr_t *dst __unused)
- {
- }
- #elif __x86_64__ || __i386__
- static ALWAYS_INLINE
- uintptr_t
- LoadExclusive(uintptr_t *src)
- {
- return *src;
- }
- static ALWAYS_INLINE
- bool
- StoreExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
- {
-
- return __sync_bool_compare_and_swap((void **)dst, (void *)oldvalue, (void *)value);
- }
- static ALWAYS_INLINE
- bool
- StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
- {
- return StoreExclusive(dst, oldvalue, value);
- }
- static ALWAYS_INLINE
- void
- ClearExclusive(uintptr_t *dst __unused)
- {
- }
- #else
- # error unknown architecture
- #endif
- #if !TARGET_OS_IPHONE
- # include <CrashReporterClient.h>
- #else
- // CrashReporterClient not yet available on iOS
- __BEGIN_DECLS
- extern const char *CRSetCrashLogMessage(const char *msg);
- extern const char *CRGetCrashLogMessage(void);
- __END_DECLS
- #endif
- # if __cplusplus
- # include <vector>
- # include <algorithm>
- # include <functional>
- using namespace std;
- # endif
- # define PRIVATE_EXTERN __attribute__((visibility("hidden")))
- # undef __private_extern__
- # define __private_extern__ use_PRIVATE_EXTERN_instead
- # undef private_extern
- # define private_extern use_PRIVATE_EXTERN_instead
- /* Use this for functions that are intended to be breakpoint hooks.
- If you do not, the compiler may optimize them away.
- BREAKPOINT_FUNCTION( void stop_on_error(void) ); */
- # define BREAKPOINT_FUNCTION(prototype) \
- OBJC_EXTERN __attribute__((noinline, used, visibility("hidden"))) \
- prototype { asm(""); }
- #elif TARGET_OS_WIN32
- # define WINVER 0x0501 // target Windows XP and later
- # define _WIN32_WINNT 0x0501 // target Windows XP and later
- # define WIN32_LEAN_AND_MEAN
- // hack: windef.h typedefs BOOL as int
- # define BOOL WINBOOL
- # include <windows.h>
- # undef BOOL
- # include <stdio.h>
- # include <stdlib.h>
- # include <stdint.h>
- # include <stdarg.h>
- # include <string.h>
- # include <assert.h>
- # include <malloc.h>
- # include <Availability.h>
- # if __cplusplus
- # include <vector>
- # include <algorithm>
- # include <functional>
- using namespace std;
- # define __BEGIN_DECLS extern "C" {
- # define __END_DECLS }
- # else
- # define __BEGIN_DECLS /*empty*/
- # define __END_DECLS /*empty*/
- # endif
- # define PRIVATE_EXTERN
- # define __attribute__(x)
- # define inline __inline
- /* Use this for functions that are intended to be breakpoint hooks.
- If you do not, the compiler may optimize them away.
- BREAKPOINT_FUNCTION( void MyBreakpointFunction(void) ); */
- # define BREAKPOINT_FUNCTION(prototype) \
- __declspec(noinline) prototype { __asm { } }
- /* stub out dtrace probes */
- # define OBJC_RUNTIME_OBJC_EXCEPTION_RETHROW() do {} while(0)
- # define OBJC_RUNTIME_OBJC_EXCEPTION_THROW(arg0) do {} while(0)
- #else
- # error unknown OS
- #endif
- #include <objc/objc.h>
- #include <objc/objc-api.h>
- extern void _objc_fatal(const char *fmt, ...)
- __attribute__((noreturn, format (printf, 1, 2)));
- extern void _objc_fatal_with_reason(uint64_t reason, uint64_t flags,
- const char *fmt, ...)
- __attribute__((noreturn, format (printf, 3, 4)));
- #define INIT_ONCE_PTR(var, create, delete) \
- do { \
- if (var) break; \
- typeof(var) v = create; \
- while (!var) { \
- if (OSAtomicCompareAndSwapPtrBarrier(0, (void*)v, (void**)&var)){ \
- goto done; \
- } \
- } \
- delete; \
- done:; \
- } while (0)
- #define INIT_ONCE_32(var, create, delete) \
- do { \
- if (var) break; \
- typeof(var) v = create; \
- while (!var) { \
- if (OSAtomicCompareAndSwap32Barrier(0, v, (volatile int32_t *)&var)) { \
- goto done; \
- } \
- } \
- delete; \
- done:; \
- } while (0)
- // Thread keys reserved by libc for our use.
- #if defined(__PTK_FRAMEWORK_OBJC_KEY0)
- # define SUPPORT_DIRECT_THREAD_KEYS 1
- # define TLS_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY0)
- # define SYNC_DATA_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY1)
- # define SYNC_COUNT_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY2)
- # define AUTORELEASE_POOL_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY3)
- # if SUPPORT_RETURN_AUTORELEASE
- # define RETURN_DISPOSITION_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY4)
- # endif
- #else
- # define SUPPORT_DIRECT_THREAD_KEYS 0
- #endif
- #if TARGET_OS_WIN32
- // Compiler compatibility
- // OS compatibility
- #define strdup _strdup
- #define issetugid() 0
- #define MIN(x, y) ((x) < (y) ? (x) : (y))
- static __inline void bcopy(const void *src, void *dst, size_t size) { memcpy(dst, src, size); }
- static __inline void bzero(void *dst, size_t size) { memset(dst, 0, size); }
- int asprintf(char **dstp, const char *format, ...);
- typedef void * malloc_zone_t;
- static __inline malloc_zone_t malloc_default_zone(void) { return (malloc_zone_t)-1; }
- static __inline void *malloc_zone_malloc(malloc_zone_t z, size_t size) { return malloc(size); }
- static __inline void *malloc_zone_calloc(malloc_zone_t z, size_t size, size_t count) { return calloc(size, count); }
- static __inline void *malloc_zone_realloc(malloc_zone_t z, void *p, size_t size) { return realloc(p, size); }
- static __inline void malloc_zone_free(malloc_zone_t z, void *p) { free(p); }
- static __inline malloc_zone_t malloc_zone_from_ptr(const void *p) { return (malloc_zone_t)-1; }
- static __inline size_t malloc_size(const void *p) { return _msize((void*)p); /* fixme invalid pointer check? */ }
- // OSAtomic
- static __inline BOOL OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst)
- {
- // fixme barrier is overkill
- long original = InterlockedCompareExchange(dst, newl, oldl);
- return (original == oldl);
- }
- static __inline BOOL OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void * volatile *dst)
- {
- void *original = InterlockedCompareExchangePointer(dst, newp, oldp);
- return (original == oldp);
- }
- static __inline BOOL OSAtomicCompareAndSwap32Barrier(int32_t oldl, int32_t newl, int32_t volatile *dst)
- {
- long original = InterlockedCompareExchange((volatile long *)dst, newl, oldl);
- return (original == oldl);
- }
- static __inline int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst)
- {
- return InterlockedDecrement((volatile long *)dst);
- }
- static __inline int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst)
- {
- return InterlockedIncrement((volatile long *)dst);
- }
- // Internal data types
- typedef DWORD objc_thread_t; // thread ID
- static __inline int thread_equal(objc_thread_t t1, objc_thread_t t2) {
- return t1 == t2;
- }
- static __inline objc_thread_t thread_self(void) {
- return GetCurrentThreadId();
- }
- typedef struct {
- DWORD key;
- void (*dtor)(void *);
- } tls_key_t;
- static __inline tls_key_t tls_create(void (*dtor)(void*)) {
- // fixme need dtor registry for DllMain to call on thread detach
- tls_key_t k;
- k.key = TlsAlloc();
- k.dtor = dtor;
- return k;
- }
- static __inline void *tls_get(tls_key_t k) {
- return TlsGetValue(k.key);
- }
- static __inline void tls_set(tls_key_t k, void *value) {
- TlsSetValue(k.key, value);
- }
- typedef struct {
- CRITICAL_SECTION *lock;
- } mutex_t;
- #define MUTEX_INITIALIZER {0};
- extern void mutex_init(mutex_t *m);
- static __inline int _mutex_lock_nodebug(mutex_t *m) {
- // fixme error check
- if (!m->lock) {
- mutex_init(m);
- }
- EnterCriticalSection(m->lock);
- return 0;
- }
- static __inline bool _mutex_try_lock_nodebug(mutex_t *m) {
- // fixme error check
- if (!m->lock) {
- mutex_init(m);
- }
- return TryEnterCriticalSection(m->lock);
- }
- static __inline int _mutex_unlock_nodebug(mutex_t *m) {
- // fixme error check
- LeaveCriticalSection(m->lock);
- return 0;
- }
- typedef mutex_t spinlock_t;
- #define spinlock_lock(l) mutex_lock(l)
- #define spinlock_unlock(l) mutex_unlock(l)
- #define SPINLOCK_INITIALIZER MUTEX_INITIALIZER
- typedef struct {
- HANDLE mutex;
- } recursive_mutex_t;
- #define RECURSIVE_MUTEX_INITIALIZER {0};
- #define RECURSIVE_MUTEX_NOT_LOCKED 1
- extern void recursive_mutex_init(recursive_mutex_t *m);
- static __inline int _recursive_mutex_lock_nodebug(recursive_mutex_t *m) {
- assert(m->mutex);
- return WaitForSingleObject(m->mutex, INFINITE);
- }
- static __inline bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t *m) {
- assert(m->mutex);
- return (WAIT_OBJECT_0 == WaitForSingleObject(m->mutex, 0));
- }
- static __inline int _recursive_mutex_unlock_nodebug(recursive_mutex_t *m) {
- assert(m->mutex);
- return ReleaseMutex(m->mutex) ? 0 : RECURSIVE_MUTEX_NOT_LOCKED;
- }
- /*
- typedef HANDLE mutex_t;
- static inline void mutex_init(HANDLE *m) { *m = CreateMutex(NULL, FALSE, NULL); }
- static inline void _mutex_lock(mutex_t *m) { WaitForSingleObject(*m, INFINITE); }
- static inline bool mutex_try_lock(mutex_t *m) { return WaitForSingleObject(*m, 0) == WAIT_OBJECT_0; }
- static inline void _mutex_unlock(mutex_t *m) { ReleaseMutex(*m); }
- */
- // based on http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
- // Vista-only CONDITION_VARIABLE would be better
- typedef struct {
- HANDLE mutex;
- HANDLE waiters; // semaphore for those in cond_wait()
- HANDLE waitersDone; // auto-reset event after everyone gets a broadcast
- CRITICAL_SECTION waitCountLock; // guards waitCount and didBroadcast
- unsigned int waitCount;
- int didBroadcast;
- } monitor_t;
- #define MONITOR_INITIALIZER { 0 }
- #define MONITOR_NOT_ENTERED 1
- extern int monitor_init(monitor_t *c);
- static inline int _monitor_enter_nodebug(monitor_t *c) {
- if (!c->mutex) {
- int err = monitor_init(c);
- if (err) return err;
- }
- return WaitForSingleObject(c->mutex, INFINITE);
- }
- static inline int _monitor_leave_nodebug(monitor_t *c) {
- if (!ReleaseMutex(c->mutex)) return MONITOR_NOT_ENTERED;
- else return 0;
- }
- static inline int _monitor_wait_nodebug(monitor_t *c) {
- int last;
- EnterCriticalSection(&c->waitCountLock);
- c->waitCount++;
- LeaveCriticalSection(&c->waitCountLock);
- SignalObjectAndWait(c->mutex, c->waiters, INFINITE, FALSE);
- EnterCriticalSection(&c->waitCountLock);
- c->waitCount--;
- last = c->didBroadcast && c->waitCount == 0;
- LeaveCriticalSection(&c->waitCountLock);
- if (last) {
- // tell broadcaster that all waiters have awoken
- SignalObjectAndWait(c->waitersDone, c->mutex, INFINITE, FALSE);
- } else {
- WaitForSingleObject(c->mutex, INFINITE);
- }
- // fixme error checking
- return 0;
- }
- static inline int monitor_notify(monitor_t *c) {
- int haveWaiters;
- EnterCriticalSection(&c->waitCountLock);
- haveWaiters = c->waitCount > 0;
- LeaveCriticalSection(&c->waitCountLock);
- if (haveWaiters) {
- ReleaseSemaphore(c->waiters, 1, 0);
- }
- // fixme error checking
- return 0;
- }
- static inline int monitor_notifyAll(monitor_t *c) {
- EnterCriticalSection(&c->waitCountLock);
- if (c->waitCount == 0) {
- LeaveCriticalSection(&c->waitCountLock);
- return 0;
- }
- c->didBroadcast = 1;
- ReleaseSemaphore(c->waiters, c->waitCount, 0);
- LeaveCriticalSection(&c->waitCountLock);
- // fairness: wait for everyone to move from waiters to mutex
- WaitForSingleObject(c->waitersDone, INFINITE);
- // not under waitCountLock, but still under mutex
- c->didBroadcast = 0;
- // fixme error checking
- return 0;
- }
- typedef IMAGE_DOS_HEADER headerType;
- // fixme YES bundle? NO bundle? sometimes?
- #define headerIsBundle(hi) YES
- OBJC_EXTERN IMAGE_DOS_HEADER __ImageBase;
- #define libobjc_header ((headerType *)&__ImageBase)
- // Prototypes
- #elif TARGET_OS_MAC
- // OS headers
- #include <mach-o/loader.h>
- #ifndef __LP64__
- # define SEGMENT_CMD LC_SEGMENT
- #else
- # define SEGMENT_CMD LC_SEGMENT_64
- #endif
- #ifndef VM_MEMORY_OBJC_DISPATCHERS
- # define VM_MEMORY_OBJC_DISPATCHERS 0
- #endif
- // Compiler compatibility
- // OS compatibility
- static inline uint64_t nanoseconds() {
- return mach_absolute_time();
- }
- // Internal data types
- typedef pthread_t objc_thread_t;
- static __inline int thread_equal(objc_thread_t t1, objc_thread_t t2) {
- return pthread_equal(t1, t2);
- }
- static __inline objc_thread_t thread_self(void) {
- return pthread_self();
- }
- typedef pthread_key_t tls_key_t;
- static inline tls_key_t tls_create(void (*dtor)(void*)) {
- tls_key_t k;
- pthread_key_create(&k, dtor);
- return k;
- }
- static inline void *tls_get(tls_key_t k) {
- return pthread_getspecific(k);
- }
- static inline void tls_set(tls_key_t k, void *value) {
- pthread_setspecific(k, value);
- }
- #if SUPPORT_DIRECT_THREAD_KEYS
- #if DEBUG
- static bool is_valid_direct_key(tls_key_t k) {
- return ( k == SYNC_DATA_DIRECT_KEY
- || k == SYNC_COUNT_DIRECT_KEY
- || k == AUTORELEASE_POOL_KEY
- # if SUPPORT_RETURN_AUTORELEASE
- || k == RETURN_DISPOSITION_KEY
- # endif
- );
- }
- #endif
- static inline void *tls_get_direct(tls_key_t k)
- {
- assert(is_valid_direct_key(k));
- if (_pthread_has_direct_tsd()) {
- return _pthread_getspecific_direct(k);
- } else {
- return pthread_getspecific(k);
- }
- }
- static inline void tls_set_direct(tls_key_t k, void *value)
- {
- assert(is_valid_direct_key(k));
- if (_pthread_has_direct_tsd()) {
- _pthread_setspecific_direct(k, value);
- } else {
- pthread_setspecific(k, value);
- }
- }
- // SUPPORT_DIRECT_THREAD_KEYS
- #endif
- static inline pthread_t pthread_self_direct()
- {
- return (pthread_t)
- _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF);
- }
- static inline mach_port_t mach_thread_self_direct()
- {
- return (mach_port_t)(uintptr_t)
- _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF);
- }
- template <bool Debug> class mutex_tt;
- template <bool Debug> class monitor_tt;
- template <bool Debug> class recursive_mutex_tt;
- #if DEBUG
- # define LOCKDEBUG 1
- #else
- # define LOCKDEBUG 0
- #endif
- using spinlock_t = mutex_tt<LOCKDEBUG>;
- using mutex_t = mutex_tt<LOCKDEBUG>;
- using monitor_t = monitor_tt<LOCKDEBUG>;
- using recursive_mutex_t = recursive_mutex_tt<LOCKDEBUG>;
- // Use fork_unsafe_lock to get a lock that isn't
- // acquired and released around fork().
- // All fork-safe locks are checked in debug builds.
- struct fork_unsafe_lock_t {
- constexpr fork_unsafe_lock_t() = default;
- };
- extern const fork_unsafe_lock_t fork_unsafe_lock;
- #include "objc-lockdebug.h"
- template <bool Debug>
- class mutex_tt : nocopy_t {
- os_unfair_lock mLock;
- public:
- constexpr mutex_tt() : mLock(OS_UNFAIR_LOCK_INIT) {
- lockdebug_remember_mutex(this);
- }
- constexpr mutex_tt(const fork_unsafe_lock_t unsafe) : mLock(OS_UNFAIR_LOCK_INIT) { }
- void lock() {
- lockdebug_mutex_lock(this);
- os_unfair_lock_lock_with_options_inline
- (&mLock, OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION);
- }
- void unlock() {
- lockdebug_mutex_unlock(this);
- os_unfair_lock_unlock_inline(&mLock);
- }
- void forceReset() {
- lockdebug_mutex_unlock(this);
- bzero(&mLock, sizeof(mLock));
- mLock = os_unfair_lock OS_UNFAIR_LOCK_INIT;
- }
- void assertLocked() {
- lockdebug_mutex_assert_locked(this);
- }
- void assertUnlocked() {
- lockdebug_mutex_assert_unlocked(this);
- }
- // Address-ordered lock discipline for a pair of locks.
- static void lockTwo(mutex_tt *lock1, mutex_tt *lock2) {
- if (lock1 < lock2) {
- lock1->lock();
- lock2->lock();
- } else {
- lock2->lock();
- if (lock2 != lock1) lock1->lock();
- }
- }
- static void unlockTwo(mutex_tt *lock1, mutex_tt *lock2) {
- lock1->unlock();
- if (lock2 != lock1) lock2->unlock();
- }
- // Scoped lock and unlock
- class locker : nocopy_t {
- mutex_tt& lock;
- public:
- locker(mutex_tt& newLock)
- : lock(newLock) { lock.lock(); }
- ~locker() { lock.unlock(); }
- };
- // Either scoped lock and unlock, or NOP.
- class conditional_locker : nocopy_t {
- mutex_tt& lock;
- bool didLock;
- public:
- conditional_locker(mutex_tt& newLock, bool shouldLock)
- : lock(newLock), didLock(shouldLock)
- {
- if (shouldLock) lock.lock();
- }
- ~conditional_locker() { if (didLock) lock.unlock(); }
- };
- };
- using mutex_locker_t = mutex_tt<LOCKDEBUG>::locker;
- using conditional_mutex_locker_t = mutex_tt<LOCKDEBUG>::conditional_locker;
- template <bool Debug>
- class recursive_mutex_tt : nocopy_t {
- os_unfair_recursive_lock mLock;
- public:
- constexpr recursive_mutex_tt() : mLock(OS_UNFAIR_RECURSIVE_LOCK_INIT) {
- lockdebug_remember_recursive_mutex(this);
- }
- constexpr recursive_mutex_tt(const fork_unsafe_lock_t unsafe)
- : mLock(OS_UNFAIR_RECURSIVE_LOCK_INIT)
- { }
- void lock()
- {
- lockdebug_recursive_mutex_lock(this);
- os_unfair_recursive_lock_lock(&mLock);
- }
- void unlock()
- {
- lockdebug_recursive_mutex_unlock(this);
- os_unfair_recursive_lock_unlock(&mLock);
- }
- void forceReset()
- {
- lockdebug_recursive_mutex_unlock(this);
- bzero(&mLock, sizeof(mLock));
- mLock = os_unfair_recursive_lock OS_UNFAIR_RECURSIVE_LOCK_INIT;
- }
- bool tryUnlock()
- {
- if (os_unfair_recursive_lock_tryunlock4objc(&mLock)) {
- lockdebug_recursive_mutex_unlock(this);
- return true;
- }
- return false;
- }
- void assertLocked() {
- lockdebug_recursive_mutex_assert_locked(this);
- }
- void assertUnlocked() {
- lockdebug_recursive_mutex_assert_unlocked(this);
- }
- };
- template <bool Debug>
- class monitor_tt {
- pthread_mutex_t mutex;
- pthread_cond_t cond;
- public:
- constexpr monitor_tt()
- : mutex(PTHREAD_MUTEX_INITIALIZER), cond(PTHREAD_COND_INITIALIZER)
- {
- lockdebug_remember_monitor(this);
- }
- monitor_tt(const fork_unsafe_lock_t unsafe)
- : mutex(PTHREAD_MUTEX_INITIALIZER), cond(PTHREAD_COND_INITIALIZER)
- { }
- void enter()
- {
- lockdebug_monitor_enter(this);
- int err = pthread_mutex_lock(&mutex);
- if (err) _objc_fatal("pthread_mutex_lock failed (%d)", err);
- }
- void leave()
- {
- lockdebug_monitor_leave(this);
- int err = pthread_mutex_unlock(&mutex);
- if (err) _objc_fatal("pthread_mutex_unlock failed (%d)", err);
- }
- void wait()
- {
- lockdebug_monitor_wait(this);
- int err = pthread_cond_wait(&cond, &mutex);
- if (err) _objc_fatal("pthread_cond_wait failed (%d)", err);
- }
- void notify()
- {
- int err = pthread_cond_signal(&cond);
- if (err) _objc_fatal("pthread_cond_signal failed (%d)", err);
- }
- void notifyAll()
- {
- int err = pthread_cond_broadcast(&cond);
- if (err) _objc_fatal("pthread_cond_broadcast failed (%d)", err);
- }
- void forceReset()
- {
- lockdebug_monitor_leave(this);
-
- bzero(&mutex, sizeof(mutex));
- bzero(&cond, sizeof(cond));
- mutex = pthread_mutex_t PTHREAD_MUTEX_INITIALIZER;
- cond = pthread_cond_t PTHREAD_COND_INITIALIZER;
- }
- void assertLocked()
- {
- lockdebug_monitor_assert_locked(this);
- }
- void assertUnlocked()
- {
- lockdebug_monitor_assert_unlocked(this);
- }
- };
- // semaphore_create formatted for INIT_ONCE use
- static inline semaphore_t create_semaphore(void)
- {
- semaphore_t sem;
- kern_return_t k;
- k = semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, 0);
- if (k) _objc_fatal("semaphore_create failed (0x%x)", k);
- return sem;
- }
- #ifndef __LP64__
- typedef struct mach_header headerType;
- typedef struct segment_command segmentType;
- typedef struct section sectionType;
- #else
- typedef struct mach_header_64 headerType;
- typedef struct segment_command_64 segmentType;
- typedef struct section_64 sectionType;
- #endif
- #define headerIsBundle(hi) (hi->mhdr()->filetype == MH_BUNDLE)
- #define libobjc_header ((headerType *)&_mh_dylib_header)
- // Prototypes
- /* Secure /tmp usage */
- extern int secure_open(const char *filename, int flags, uid_t euid);
- #else
- #error unknown OS
- #endif
- static inline void *
- memdup(const void *mem, size_t len)
- {
- void *dup = malloc(len);
- memcpy(dup, mem, len);
- return dup;
- }
- // strdup that doesn't copy read-only memory
- static inline char *
- strdupIfMutable(const char *str)
- {
- size_t size = strlen(str) + 1;
- if (_dyld_is_memory_immutable(str, size)) {
- return (char *)str;
- } else {
- return (char *)memdup(str, size);
- }
- }
- // free strdupIfMutable() result
- static inline void
- freeIfMutable(char *str)
- {
- size_t size = strlen(str) + 1;
- if (_dyld_is_memory_immutable(str, size)) {
- // nothing
- } else {
- free(str);
- }
- }
- // nil-checking unsigned strdup
- static inline uint8_t *
- ustrdupMaybeNil(const uint8_t *str)
- {
- if (!str) return nil;
- return (uint8_t *)strdupIfMutable((char *)str);
- }
- // OS version checking:
- //
- // sdkVersion()
- // DYLD_OS_VERSION(mac, ios, tv, watch, bridge)
- // sdkIsOlderThan(mac, ios, tv, watch, bridge)
- // sdkIsAtLeast(mac, ios, tv, watch, bridge)
- //
- // This version order matches OBJC_AVAILABLE.
- #if TARGET_OS_OSX
- # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_MACOSX_VERSION_##x
- # define sdkVersion() dyld_get_program_sdk_version()
- #elif TARGET_OS_IOS
- # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##i
- # define sdkVersion() dyld_get_program_sdk_version()
- #elif TARGET_OS_TV
- // dyld does not currently have distinct constants for tvOS
- # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##t
- # define sdkVersion() dyld_get_program_sdk_version()
- #elif TARGET_OS_BRIDGE
- # if TARGET_OS_WATCH
- # error bridgeOS 1.0 not supported
- # endif
- // fixme don't need bridgeOS versioning yet
- # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##t
- # define sdkVersion() dyld_get_program_sdk_bridge_os_version()
- #elif TARGET_OS_WATCH
- # define DYLD_OS_VERSION(x, i, t, w, b) DYLD_WATCHOS_VERSION_##w
- // watchOS has its own API for compatibility reasons
- # define sdkVersion() dyld_get_program_sdk_watch_os_version()
- #else
- # error unknown OS
- #endif
- #define sdkIsOlderThan(x, i, t, w, b) \
- (sdkVersion() < DYLD_OS_VERSION(x, i, t, w, b))
- #define sdkIsAtLeast(x, i, t, w, b) \
- (sdkVersion() >= DYLD_OS_VERSION(x, i, t, w, b))
- // Allow bare 0 to be used in DYLD_OS_VERSION() and sdkIsOlderThan()
- #define DYLD_MACOSX_VERSION_0 0
- #define DYLD_IOS_VERSION_0 0
- #define DYLD_TVOS_VERSION_0 0
- #define DYLD_WATCHOS_VERSION_0 0
- #define DYLD_BRIDGEOS_VERSION_0 0
- // Pretty-print a DYLD_*_VERSION_* constant.
- #define SDK_FORMAT "%hu.%hhu.%hhu"
- #define FORMAT_SDK(v) \
- (unsigned short)(((uint32_t)(v))>>16), \
- (unsigned char)(((uint32_t)(v))>>8), \
- (unsigned char)(((uint32_t)(v))>>0)
- // fork() safety requires careful tracking of all locks.
- // Our custom lock types check this in debug builds.
- // Disallow direct use of all other lock types.
- typedef __darwin_pthread_mutex_t pthread_mutex_t UNAVAILABLE_ATTRIBUTE;
- typedef __darwin_pthread_rwlock_t pthread_rwlock_t UNAVAILABLE_ATTRIBUTE;
- typedef int32_t OSSpinLock UNAVAILABLE_ATTRIBUTE;
- typedef struct os_unfair_lock_s os_unfair_lock UNAVAILABLE_ATTRIBUTE;
- #endif
|