-
Notifications
You must be signed in to change notification settings - Fork 469
TLS: Thread Local Storage #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,10 +92,76 @@ _dispatch_thread_key_create(const unsigned long *k, void (*d)(void *)) | |
if (!*k || !d) return; | ||
dispatch_assert_zero(pthread_key_init_np((int)*k, d)); | ||
} | ||
#elif DISPATCH_USE_THREAD_LOCAL_STORAGE | ||
|
||
DISPATCH_TSD_INLINE | ||
static inline void | ||
_dispatch_thread_key_create(pthread_key_t *k, void (*d)(void *)) | ||
{ | ||
dispatch_assert_zero(pthread_key_create(k, d)); | ||
} | ||
|
||
struct dispatch_tsd { | ||
pid_t tid; | ||
void *dispatch_queue_key; | ||
void *dispatch_frame_key; | ||
void *dispatch_cache_key; | ||
void *dispatch_context_key; | ||
void *dispatch_pthread_root_queue_observer_hooks_key; | ||
void *dispatch_defaultpriority_key; | ||
#if DISPATCH_INTROSPECTION | ||
void *dispatch_introspection_key; | ||
#elif DISPATCH_PERF_MON | ||
void *dispatch_bcounter_key; | ||
#endif | ||
#if DISPATCH_LOCK_USE_SEMAPHORE_FALLBACK | ||
void *dispatch_sema4_key; | ||
#endif | ||
void *dispatch_priority_key; | ||
void *dispatch_voucher_key; | ||
void *dispatch_deferred_items_key; | ||
}; | ||
|
||
extern __thread struct dispatch_tsd __dispatch_tsd; | ||
extern pthread_key_t __dispatch_tsd_key; | ||
extern void libdispatch_tsd_init(void); | ||
extern void _libdispatch_tsd_cleanup(void *ctx); | ||
|
||
DISPATCH_ALWAYS_INLINE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should also add |
||
static inline struct dispatch_tsd * | ||
_dispatch_get_tsd_base(void) | ||
{ | ||
if (unlikely(__dispatch_tsd.tid == 0)) { | ||
libdispatch_tsd_init(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a |
||
} | ||
OS_COMPILER_CAN_ASSUME(__dispatch_tsd.tid != 0); | ||
return &__dispatch_tsd; | ||
} | ||
|
||
#define _dispatch_thread_getspecific(key) \ | ||
(_dispatch_get_tsd_base()->key) | ||
#define _dispatch_thread_setspecific(key, value) \ | ||
(void)(_dispatch_get_tsd_base()->key = (value)) | ||
|
||
#define _dispatch_thread_getspecific_pair(k1, p1, k2, p2) \ | ||
( *(p1) = _dispatch_thread_getspecific(k1), \ | ||
*(p2) = _dispatch_thread_getspecific(k2) ) | ||
|
||
#define _dispatch_thread_getspecific_packed_pair(k1, k2, p) \ | ||
( (p)[0] = _dispatch_thread_getspecific(k1), \ | ||
(p)[1] = _dispatch_thread_getspecific(k2) ) | ||
|
||
#define _dispatch_thread_setspecific_pair(k1, p1, k2, p2) \ | ||
( _dispatch_thread_setspecific(k1,p1), \ | ||
_dispatch_thread_setspecific(k2,p2) ) | ||
|
||
#define _dispatch_thread_setspecific_packed_pair(k1, k2, p) \ | ||
( _dispatch_thread_setspecific(k1,(p)[0]), \ | ||
_dispatch_thread_setspecific(k2,(p)[1]) ) | ||
|
||
#else | ||
extern pthread_key_t dispatch_queue_key; | ||
extern pthread_key_t dispatch_frame_key; | ||
extern pthread_key_t dispatch_sema4_key; | ||
extern pthread_key_t dispatch_cache_key; | ||
extern pthread_key_t dispatch_context_key; | ||
extern pthread_key_t dispatch_pthread_root_queue_observer_hooks_key; | ||
|
@@ -105,7 +171,8 @@ extern pthread_key_t dispatch_introspection_key; | |
#elif DISPATCH_PERF_MON | ||
extern pthread_key_t dispatch_bcounter_key; | ||
#endif | ||
extern pthread_key_t dispatch_cache_key; | ||
extern pthread_key_t dispatch_sema4_key; | ||
extern pthread_key_t dispatch_priority_key; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eeew thanks |
||
extern pthread_key_t dispatch_voucher_key; | ||
extern pthread_key_t dispatch_deferred_items_key; | ||
|
||
|
@@ -117,6 +184,7 @@ _dispatch_thread_key_create(pthread_key_t *k, void (*d)(void *)) | |
} | ||
#endif | ||
|
||
#ifndef DISPATCH_USE_THREAD_LOCAL_STORAGE | ||
DISPATCH_TSD_INLINE | ||
static inline void | ||
_dispatch_thread_setspecific(pthread_key_t k, void *v) | ||
|
@@ -210,6 +278,7 @@ _dispatch_thread_setspecific_packed_pair(pthread_key_t k1, pthread_key_t k2, | |
_dispatch_thread_setspecific(k1, p[0]); | ||
_dispatch_thread_setspecific(k2, p[1]); | ||
} | ||
#endif | ||
|
||
#if TARGET_OS_WIN32 | ||
#define _dispatch_thread_self() ((uintptr_t)GetCurrentThreadId()) | ||
|
@@ -228,6 +297,8 @@ _dispatch_thread_setspecific_packed_pair(pthread_key_t k1, pthread_key_t k2, | |
#if DISPATCH_USE_DIRECT_TSD | ||
#define _dispatch_thread_port() ((mach_port_t)(uintptr_t)\ | ||
_dispatch_thread_getspecific(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF)) | ||
#elif DISPATCH_USE_THREAD_LOCAL_STORAGE | ||
#define _dispatch_thread_port() ((mach_port_t)(_dispatch_get_tsd_base()->tid)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't like this name, and if there are calls to _dispatch_thread_port() in non mach code, we should most likely port them to IOW that thing here should be in a #if TARGET_OS_WIN32
#define _dispatch_thread_port() ((mach_port_t)0)
#elif HAVE_MACH
#if DISPATCH_USE_DIRECT_TSD
#define _dispatch_thread_port() ((mach_port_t)(uintptr_t)\
_dispatch_thread_getspecific(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF))
#else
#define _dispatch_thread_port() pthread_mach_thread_np(_dispatch_thread_self())
#endif
#endif // HAVE_MACH There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically definining it for Windows is not the right way, but let's leave that part alone. it should also define |
||
#else | ||
#define _dispatch_thread_port() pthread_mach_thread_np(_dispatch_thread_self()) | ||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make that
instead of linux_sbuts.c