[runtime] Switch getenv to use heap memory
[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 <config.h>
16 #include <glib.h>
17
18 /* TLS entries used by the runtime */
19 typedef enum {
20         /* mono_thread_internal_current () */
21         TLS_KEY_THREAD = 0,
22         TLS_KEY_JIT_TLS = 1,
23         /* mono_domain_get () */
24         TLS_KEY_DOMAIN = 2,
25         TLS_KEY_LMF = 3,
26         TLS_KEY_SGEN_THREAD_INFO = 4,
27         TLS_KEY_LMF_ADDR = 5,
28         TLS_KEY_NUM = 6
29 } MonoTlsKey;
30
31 #ifdef HAVE_KW_THREAD
32 #define USE_KW_THREAD
33 #endif
34
35 #if defined(USE_KW_THREAD)
36 #define HAVE_GET_TLS_ADDR
37 #elif defined(TARGET_MACH) && (defined(TARGET_X86) || defined(TARGET_AMD64))
38 /* mono_mach_get_tls_address_from_thread is untested for arm/arm64 */
39 #define HAVE_GET_TLS_ADDR
40 #endif
41
42 #ifdef HOST_WIN32
43
44 #include <windows.h>
45
46 #define MonoNativeTlsKey DWORD
47 #define mono_native_tls_alloc(key,destructor) ((*(key) = TlsAlloc ()) != TLS_OUT_OF_INDEXES && destructor == NULL)
48 #define mono_native_tls_free TlsFree
49 #define mono_native_tls_set_value TlsSetValue
50 #define mono_native_tls_get_value TlsGetValue
51
52 #else
53
54 #include <pthread.h>
55
56 #define MonoNativeTlsKey pthread_key_t
57 #define mono_native_tls_get_value pthread_getspecific
58
59 static inline int
60 mono_native_tls_alloc (MonoNativeTlsKey *key, void *destructor)
61 {
62         return pthread_key_create (key, (void (*)(void*)) destructor) == 0;
63 }
64
65 static inline void
66 mono_native_tls_free (MonoNativeTlsKey key)
67 {
68         pthread_key_delete (key);
69 }
70
71 static inline int
72 mono_native_tls_set_value (MonoNativeTlsKey key, gpointer value)
73 {
74         return !pthread_setspecific (key, value);
75 }
76
77 #endif /* HOST_WIN32 */
78
79 void mono_tls_init_gc_keys (void);
80 void mono_tls_init_runtime_keys (void);
81 void mono_tls_free_keys (void);
82 gint32 mono_tls_get_tls_offset (MonoTlsKey key);
83 gpointer mono_tls_get_tls_getter (MonoTlsKey key, gboolean name);
84 gpointer mono_tls_get_tls_setter (MonoTlsKey key, gboolean name);
85 gpointer mono_tls_get_tls_addr (MonoTlsKey key);
86
87 gpointer mono_tls_get_thread (void);
88 gpointer mono_tls_get_jit_tls (void);
89 gpointer mono_tls_get_domain (void);
90 gpointer mono_tls_get_lmf (void);
91 gpointer mono_tls_get_sgen_thread_info (void);
92 gpointer mono_tls_get_lmf_addr (void);
93
94 void mono_tls_set_thread (gpointer value);
95 void mono_tls_set_jit_tls (gpointer value);
96 void mono_tls_set_domain (gpointer value);
97 void mono_tls_set_lmf (gpointer value);
98 void mono_tls_set_sgen_thread_info (gpointer value);
99 void mono_tls_set_lmf_addr (gpointer value);
100
101 #endif /* __MONO_TLS_H__ */