First set of licensing changes
[mono.git] / mono / utils / mono-tls.h
1 /*
2  * mono-tls.h: Low-level TLS support
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * Copyright 2011 Novell, Inc (http://www.novell.com)
8  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11
12 #ifndef __MONO_TLS_H__
13 #define __MONO_TLS_H__
14
15 #include <glib.h>
16
17 /* TLS entries used by the runtime */
18 typedef enum {
19         /* mono_thread_internal_current () */
20         TLS_KEY_THREAD = 0,
21         TLS_KEY_JIT_TLS = 1,
22         /* mono_domain_get () */
23         TLS_KEY_DOMAIN = 2,
24         TLS_KEY_LMF = 3,
25         TLS_KEY_SGEN_THREAD_INFO = 4,
26         TLS_KEY_SGEN_TLAB_NEXT_ADDR = 5,
27         TLS_KEY_SGEN_TLAB_TEMP_END = 6,
28         TLS_KEY_BOEHM_GC_THREAD = 7,
29         TLS_KEY_LMF_ADDR = 8,
30         TLS_KEY_NUM = 9
31 } MonoTlsKey;
32
33 #ifdef HOST_WIN32
34
35 #include <windows.h>
36
37 #define MonoNativeTlsKey DWORD
38 #define mono_native_tls_alloc(key,destructor) ((*(key) = TlsAlloc ()) != TLS_OUT_OF_INDEXES && destructor == NULL)
39 #define mono_native_tls_free TlsFree
40 #define mono_native_tls_set_value TlsSetValue
41 #define mono_native_tls_get_value TlsGetValue
42
43 #else
44
45 #include <pthread.h>
46
47 #define MonoNativeTlsKey pthread_key_t
48 #define mono_native_tls_get_value pthread_getspecific
49
50 static inline int
51 mono_native_tls_alloc (MonoNativeTlsKey *key, void *destructor)
52 {
53         return pthread_key_create (key, (void (*)(void*)) destructor) == 0;
54 }
55
56 static inline void
57 mono_native_tls_free (MonoNativeTlsKey key)
58 {
59         pthread_key_delete (key);
60 }
61
62 static inline int
63 mono_native_tls_set_value (MonoNativeTlsKey key, gpointer value)
64 {
65         return !pthread_setspecific (key, value);
66 }
67
68 #endif /* HOST_WIN32 */
69
70 int mono_tls_key_get_offset (MonoTlsKey key);
71 void mono_tls_key_set_offset (MonoTlsKey key, int offset);
72
73 #endif /* __MONO_TLS_H__ */