1 krisbash 1.1 #ifndef _pal_tls_h
2 #define _pal_tls_h
3
4 #include <stddef.h>
5 #include <nits/base/nits.h>
6
7 #if defined(PAL_HAVE_PTHREADS)
8 # include <pthread.h>
9 #endif
10
11 PAL_BEGIN_EXTERNC
12
13 typedef struct _TLS
14 {
15 #if defined(_MSC_VER)
16 DWORD index;
17 #else
18 pthread_key_t key;
19 #endif
20 }
21 TLS;
22 krisbash 1.1
23 _Return_type_success_(return == 0) int TLS_Init_Injected(
24 _Out_ TLS* self,
25 NitsCallSite cs);
26
27 #define TLS_Init(self) TLS_Init_Injected(self, NitsHere())
28
29 PAL_INLINE int TLS_Destroy(
30 _Inout_ TLS* self)
31 {
32 #if defined(_MSC_VER)
33 return TlsFree(self->index) ? 0 : -1;
34 #else
35 return pthread_key_delete(self->key) == 0 ? 0 : -1;
36 #endif
37 }
38
39 PAL_INLINE int TLS_Set(
40 _Inout_ TLS* self,
41 _In_ ptrdiff_t data)
42 {
43 krisbash 1.1 #if defined(_MSC_VER)
44 return TlsSetValue(self->index, (void*)data) ? 0 : -1;
45 #else
46 return pthread_setspecific(self->key, (void*)data) == 0 ? 0 : -1;
47 #endif
48 }
49
50 PAL_INLINE ptrdiff_t TLS_Get(
51 _In_ TLS* self)
52 {
53 #if defined(_MSC_VER)
54 return (ptrdiff_t)TlsGetValue(self->index);
55 #else
56 return (ptrdiff_t)pthread_getspecific(self->key);
57 #endif
58 }
59
60 PAL_END_EXTERNC
61
62 #endif /* _pal_tls_h */
|