Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / utils / mono-tls.h
1 /**
2  * \file
3  * Low-level TLS support
4  *
5  * Author:
6  *      Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * Copyright 2011 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12
13 #ifndef __MONO_TLS_H__
14 #define __MONO_TLS_H__
15
16 #include <config.h>
17 #include <glib.h>
18
19 /* TLS entries used by the runtime */
20 typedef enum {
21         /* mono_thread_internal_current () */
22         TLS_KEY_THREAD = 0,
23         TLS_KEY_JIT_TLS = 1,
24         /* mono_domain_get () */
25         TLS_KEY_DOMAIN = 2,
26         TLS_KEY_SGEN_THREAD_INFO = 3,
27         TLS_KEY_LMF_ADDR = 4,
28         TLS_KEY_NUM = 5
29 } MonoTlsKey;
30
31 #ifdef HAVE_KW_THREAD
32 #define USE_KW_THREAD
33 #endif
34
35 #ifdef HOST_WIN32
36
37 #include <windows.h>
38
39 #define MonoNativeTlsKey DWORD
40 #define mono_native_tls_alloc(key,destructor) ((*(key) = TlsAlloc ()) != TLS_OUT_OF_INDEXES && destructor == NULL)
41 #define mono_native_tls_free TlsFree
42 #define mono_native_tls_set_value TlsSetValue
43 #define mono_native_tls_get_value TlsGetValue
44
45 #else
46
47 #include <pthread.h>
48
49 #define MonoNativeTlsKey pthread_key_t
50 #define mono_native_tls_get_value pthread_getspecific
51
52 static inline int
53 mono_native_tls_alloc (MonoNativeTlsKey *key, void *destructor)
54 {
55         return pthread_key_create (key, (void (*)(void*)) destructor) == 0;
56 }
57
58 static inline void
59 mono_native_tls_free (MonoNativeTlsKey key)
60 {
61         pthread_key_delete (key);
62 }
63
64 static inline int
65 mono_native_tls_set_value (MonoNativeTlsKey key, gpointer value)
66 {
67         return !pthread_setspecific (key, value);
68 }
69
70 #endif /* HOST_WIN32 */
71
72 void mono_tls_init_gc_keys (void);
73 void mono_tls_init_runtime_keys (void);
74 void mono_tls_free_keys (void);
75 gint32 mono_tls_get_tls_offset (MonoTlsKey key);
76 gpointer mono_tls_get_tls_getter (MonoTlsKey key, gboolean name);
77 gpointer mono_tls_get_tls_setter (MonoTlsKey key, gboolean name);
78
79 gpointer mono_tls_get_thread (void);
80 gpointer mono_tls_get_jit_tls (void);
81 gpointer mono_tls_get_domain (void);
82 gpointer mono_tls_get_sgen_thread_info (void);
83 gpointer mono_tls_get_lmf_addr (void);
84
85 void mono_tls_set_thread (gpointer value);
86 void mono_tls_set_jit_tls (gpointer value);
87 void mono_tls_set_domain (gpointer value);
88 void mono_tls_set_sgen_thread_info (gpointer value);
89 void mono_tls_set_lmf_addr (gpointer value);
90
91 #endif /* __MONO_TLS_H__ */