Remove io-layer Windows API TLS emulation code.
authorMark Probst <mark.probst@gmail.com>
Fri, 22 Jul 2011 09:48:36 +0000 (11:48 +0200)
committerMark Probst <mark.probst@gmail.com>
Fri, 22 Jul 2011 09:48:36 +0000 (11:48 +0200)
We should always use native TLS.  The Windows TLS emulation adds
an unnecessary layer and even slows us down considerably on some
platforms: The TlsGetValue() code not only does a TLS get, but,
since it calls SetLastError(), also a TLS set, so on platforms
with slow TLS set, TLS get will also be slow.

This will almost certainly break something.  I beg forgiveness.

27 files changed:
mono/io-layer/threads.h
mono/io-layer/wthreads.c
mono/metadata/domain.c
mono/metadata/loader.c
mono/metadata/marshal.c
mono/metadata/mono-wsq.c
mono/metadata/object-internals.h
mono/metadata/threads.c
mono/mini/debugger-agent.c
mono/mini/exceptions-amd64.c
mono/mini/exceptions-arm.c
mono/mini/exceptions-ppc.c
mono/mini/exceptions-x86.c
mono/mini/jit-icalls.c
mono/mini/mini-amd64.c
mono/mini/mini-exceptions.c
mono/mini/mini-gc.c
mono/mini/mini-llvm.c
mono/mini/mini-posix.c
mono/mini/mini-trampolines.c
mono/mini/mini.c
mono/mini/mini.h
mono/mini/tasklets.c
mono/mini/tramp-amd64.c
mono/mini/tramp-x86.c
mono/profiler/mono-profiler-logging.c
mono/utils/mono-tls.h

index b61f26113517d6e76d9675d434545e44bafede6d..3fd68b87574abed518726c1bee645057457d5d8c 100644 (file)
@@ -55,11 +55,6 @@ extern gsize GetCurrentThreadId(void); /* NB return is 32bit in MS API */
 extern gpointer GetCurrentThread(void);
 extern guint32 ResumeThread(gpointer handle);
 extern guint32 SuspendThread(gpointer handle);
-extern guint32 mono_pthread_key_for_tls (guint32 idx);
-extern guint32 TlsAlloc(void);
-extern gboolean TlsFree(guint32 idx);
-extern gpointer TlsGetValue(guint32 idx);
-extern gboolean TlsSetValue(guint32 idx, gpointer value);
 extern void Sleep(guint32 ms);
 extern guint32 SleepEx(guint32 ms, gboolean alertable);
 extern guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer thread_handle, 
index da33b517e8c580ee0358cb2e82760549c4daaf8f..bb37149d76b948c930017b314ea6630c02f2455e 100644 (file)
@@ -36,7 +36,6 @@
 #endif
 
 #undef DEBUG
-#undef TLS_DEBUG
 
 #if 0
 #define WAIT_DEBUG(code) do { code } while (0)
@@ -814,172 +813,6 @@ guint32 SuspendThread(gpointer handle)
        return(0xFFFFFFFF);
 }
 
-/*
- * We assume here that TLS_MINIMUM_AVAILABLE is less than
- * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
- * library usage.
- *
- * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
- * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
- * fine.
- */
-
-static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
-static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
-static pthread_mutex_t TLS_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-guint32
-mono_pthread_key_for_tls (guint32 idx)
-{
-       return (guint32)TLS_keys [idx];
-}
-
-/**
- * TlsAlloc:
- *
- * Allocates a Thread Local Storage (TLS) index.  Any thread in the
- * same process can use this index to store and retrieve values that
- * are local to that thread.
- *
- * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
- * is available.
- */
-guint32 TlsAlloc(void)
-{
-       guint32 i;
-       int thr_ret;
-       
-       pthread_mutex_lock (&TLS_mutex);
-       
-       for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
-               if(TLS_used[i]==FALSE) {
-                       TLS_used[i]=TRUE;
-                       thr_ret = pthread_key_create(&TLS_keys[i], NULL);
-                       g_assert (thr_ret == 0);
-
-                       pthread_mutex_unlock (&TLS_mutex);
-       
-#ifdef TLS_DEBUG
-                       g_message ("%s: returning key %d", __func__, i);
-#endif
-                       
-                       return(i);
-               }
-       }
-
-       pthread_mutex_unlock (&TLS_mutex);
-       
-#ifdef TLS_DEBUG
-       g_message ("%s: out of indices", __func__);
-#endif
-                       
-       
-       return(TLS_OUT_OF_INDEXES);
-}
-
-#define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
-
-/**
- * TlsFree:
- * @idx: The TLS index to free
- *
- * Releases a TLS index, making it available for reuse.  This call
- * will delete any TLS data stored under index @idx in all threads.
- *
- * Return value: %TRUE on success, %FALSE otherwise.
- */
-gboolean TlsFree(guint32 idx)
-{
-       int thr_ret;
-       
-#ifdef TLS_DEBUG
-       g_message ("%s: freeing key %d", __func__, idx);
-#endif
-
-       if (idx >= TLS_MINIMUM_AVAILABLE) {
-               SetLastError (ERROR_INVALID_PARAMETER);
-               return FALSE;
-       }
-
-       pthread_mutex_lock (&TLS_mutex);
-       
-       if(TLS_used[idx]==FALSE) {
-               pthread_mutex_unlock (&TLS_mutex);
-               SetLastError (ERROR_INVALID_PARAMETER);
-               return(FALSE);
-       }
-       
-       TLS_used[idx]=FALSE;
-       thr_ret = pthread_key_delete(TLS_keys[idx]);
-       g_assert (thr_ret == 0);
-       
-       pthread_mutex_unlock (&TLS_mutex);
-       
-       return(TRUE);
-}
-
-/**
- * TlsGetValue:
- * @idx: The TLS index to retrieve
- *
- * Retrieves the TLS data stored under index @idx.
- *
- * Return value: The value stored in the TLS index @idx in the current
- * thread, or %NULL on error.  As %NULL can be a valid return value,
- * in this case GetLastError() returns %ERROR_SUCCESS.
- */
-gpointer TlsGetValue(guint32 idx)
-{
-       gpointer ret;
-       
-#ifdef TLS_DEBUG
-       g_message ("%s: looking up key %d", __func__, idx);
-#endif
-       if (idx >= TLS_MINIMUM_AVAILABLE) {
-               SetLastError (ERROR_INVALID_PARAMETER);
-               return NULL;
-       }
-       ret=pthread_getspecific(TLS_keys[idx]);
-
-#ifdef TLS_DEBUG
-       g_message ("%s: returning %p", __func__, ret);
-#endif
-
-       SetLastError (ERROR_SUCCESS);
-       return(ret);
-}
-
-/**
- * TlsSetValue:
- * @idx: The TLS index to store
- * @value: The value to store under index @idx
- *
- * Stores @value at TLS index @idx.
- *
- * Return value: %TRUE on success, %FALSE otherwise.
- */
-gboolean TlsSetValue(guint32 idx, gpointer value)
-{
-       int ret;
-
-#ifdef TLS_DEBUG
-       g_message ("%s: setting key %d to %p", __func__, idx, value);
-#endif
-       if (idx >= TLS_MINIMUM_AVAILABLE) {
-               SetLastError (ERROR_INVALID_PARAMETER);
-               return FALSE;
-       }
-       
-       ret=pthread_setspecific(TLS_keys[idx], value);
-#ifdef TLS_DEBUG
-       if(ret!=0)
-               g_message ("%s: pthread_setspecific error: %s", __func__,
-                          strerror (ret));
-#endif
-       
-       return(ret == 0);
-}
-
 /**
  * SleepEx:
  * @ms: The time in milliseconds to suspend for
index 5fbd6fb8b5364f4036d46f1570de15e9238d1ae5..220715f7aeb23da14e561f6b4ece167c94362294 100644 (file)
@@ -21,6 +21,7 @@
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-counters.h>
 #include <mono/utils/hazard-pointer.h>
+#include <mono/utils/mono-tls.h>
 #include <mono/metadata/object.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/domain-internals.h>
@@ -44,7 +45,7 @@
  * or the other (we used to do it because tls slots were GC-tracked,
  * but we can't depend on this).
  */
-static guint32 appdomain_thread_id = -1;
+static MonoNativeTlsKey appdomain_thread_id;
 
 /* 
  * Avoid calling TlsSetValue () if possible, since in the io-layer, it acquires
@@ -67,14 +68,14 @@ MONO_FAST_TLS_DECLARE(tls_appdomain);
 #else
 #define SET_APPDOMAIN(x) do { \
        MONO_FAST_TLS_SET (tls_appdomain,x); \
-       TlsSetValue (appdomain_thread_id, x); \
+       mono_native_tls_set_value (appdomain_thread_id, x); \
 } while (FALSE)
 #endif
 
 #else /* !MONO_HAVE_FAST_TLS */
 
-#define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
-#define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
+#define GET_APPDOMAIN() ((MonoDomain *)mono_native_tls_get_value (appdomain_thread_id))
+#define SET_APPDOMAIN(x) mono_native_tls_set_value (appdomain_thread_id, x);
 
 #endif
 
@@ -157,7 +158,7 @@ get_runtime_by_version (const char *version);
 static MonoImage*
 mono_jit_info_find_aot_module (guint8* addr);
 
-guint32
+MonoNativeTlsKey
 mono_domain_get_tls_key (void)
 {
        return appdomain_thread_id;
@@ -1254,7 +1255,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        mono_gc_base_init ();
 
        MONO_FAST_TLS_INIT (tls_appdomain);
-       appdomain_thread_id = TlsAlloc ();
+       mono_native_tls_alloc (appdomain_thread_id, NULL);
 
        InitializeCriticalSection (&appdomains_mutex);
 
@@ -1719,13 +1720,9 @@ mono_cleanup (void)
        mono_debug_cleanup ();
        mono_metadata_cleanup ();
 
-       TlsFree (appdomain_thread_id);
+       mono_native_tls_free (appdomain_thread_id);
        DeleteCriticalSection (&appdomains_mutex);
 
-       /*
-        * This should be called last as TlsGetValue ()/TlsSetValue () can be called during
-        * shutdown.
-        */
 #ifndef HOST_WIN32
        _wapi_cleanup ();
 #endif
index ac20805850f37d698f4107f67874b2a5e35e4636..c2263d80dc786f217b76a0be925d727744292bc1 100644 (file)
@@ -43,6 +43,7 @@
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-counters.h>
 #include <mono/utils/mono-error-internals.h>
+#include <mono/utils/mono-tls.h>
 
 MonoDefaults mono_defaults;
 
@@ -65,13 +66,13 @@ static guint32 signatures_size;
 /*
  * This TLS variable contains the last type load error encountered by the loader.
  */
-guint32 loader_error_thread_id;
+MonoNativeTlsKey loader_error_thread_id;
 
 /*
  * This TLS variable holds how many times the current thread has acquired the loader 
  * lock.
  */
-guint32 loader_lock_nest_id;
+MonoNativeTlsKey loader_lock_nest_id;
 
 static void dllmap_cleanup (void);
 
@@ -84,8 +85,8 @@ mono_loader_init ()
                InitializeCriticalSection (&loader_mutex);
                loader_lock_inited = TRUE;
 
-               loader_error_thread_id = TlsAlloc ();
-               loader_lock_nest_id = TlsAlloc ();
+               mono_native_tls_alloc (loader_error_thread_id, NULL);
+               mono_native_tls_alloc (loader_lock_nest_id, NULL);
 
                mono_counters_register ("Inflated signatures size",
                                                                MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
@@ -105,8 +106,8 @@ mono_loader_cleanup (void)
 {
        dllmap_cleanup ();
 
-       TlsFree (loader_error_thread_id);
-       TlsFree (loader_lock_nest_id);
+       mono_native_tls_free (loader_error_thread_id);
+       mono_native_tls_free (loader_lock_nest_id);
 
        DeleteCriticalSection (&loader_mutex);
        loader_lock_inited = FALSE;     
@@ -127,7 +128,7 @@ mono_loader_cleanup (void)
 static void
 set_loader_error (MonoLoaderError *error)
 {
-       TlsSetValue (loader_error_thread_id, error);
+       mono_native_tls_set_value (loader_error_thread_id, error);
 }
 
 /**
@@ -265,7 +266,7 @@ mono_loader_set_error_bad_image (char *msg)
 MonoLoaderError*
 mono_loader_get_last_error (void)
 {
-       return (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
+       return (MonoLoaderError*)mono_native_tls_get_value (loader_error_thread_id);
 }
 
 /**
@@ -276,15 +277,15 @@ mono_loader_get_last_error (void)
 void
 mono_loader_clear_error (void)
 {
-       MonoLoaderError *ex = (MonoLoaderError*)TlsGetValue (loader_error_thread_id);
+       MonoLoaderError *ex = (MonoLoaderError*)mono_native_tls_get_value (loader_error_thread_id);
 
        if (ex) {
                g_free (ex->class_name);
                g_free (ex->assembly_name);
                g_free (ex->msg);
                g_free (ex);
-       
-               TlsSetValue (loader_error_thread_id, NULL);
+
+               mono_native_tls_set_value (loader_error_thread_id, NULL);
        }
 }
 
@@ -2174,7 +2175,7 @@ mono_loader_lock (void)
 {
        mono_locks_acquire (&loader_mutex, LoaderLock);
        if (G_UNLIKELY (loader_lock_track_ownership)) {
-               TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) + 1));
+               mono_native_tls_set_value (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id)) + 1));
        }
 }
 
@@ -2183,7 +2184,7 @@ mono_loader_unlock (void)
 {
        mono_locks_release (&loader_mutex, LoaderLock);
        if (G_UNLIKELY (loader_lock_track_ownership)) {
-               TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) - 1));
+               mono_native_tls_set_value (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id)) - 1));
        }
 }
 
@@ -2211,7 +2212,7 @@ mono_loader_lock_is_owned_by_self (void)
 {
        g_assert (loader_lock_track_ownership);
 
-       return GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) > 0;
+       return GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id)) > 0;
 }
 
 /*
index 7e2636516e2c476dd32a9fad98030526a9985e73..5ade3bb294adf8e8e169e00bedef1e13c8538fed 100644 (file)
@@ -35,6 +35,7 @@
 #include "mono/metadata/gc-internal.h"
 #include "mono/metadata/cominterop.h"
 #include "mono/utils/mono-counters.h"
+#include "mono/utils/mono-tls.h"
 #include <string.h>
 #include <errno.h>
 
@@ -76,9 +77,9 @@ typedef struct _MonoRemotingMethods MonoRemotingMethods;
 static CRITICAL_SECTION marshal_mutex;
 static gboolean marshal_mutex_initialized;
 
-static guint32 last_error_tls_id;
+static MonoNativeTlsKey last_error_tls_id;
 
-static guint32 load_type_info_tls_id;
+static MonoNativeTlsKey load_type_info_tls_id;
 
 static void
 delegate_hash_table_add (MonoDelegate *d);
@@ -201,8 +202,8 @@ mono_marshal_init (void)
                module_initialized = TRUE;
                InitializeCriticalSection (&marshal_mutex);
                marshal_mutex_initialized = TRUE;
-               last_error_tls_id = TlsAlloc ();
-               load_type_info_tls_id = TlsAlloc ();
+               mono_native_tls_alloc (last_error_tls_id, NULL);
+               mono_native_tls_alloc (load_type_info_tls_id, NULL);
 
                register_icall (ves_icall_System_Threading_Thread_ResetAbort, "ves_icall_System_Threading_Thread_ResetAbort", "void", TRUE);
                register_icall (mono_marshal_string_to_utf16, "mono_marshal_string_to_utf16", "ptr obj", FALSE);
@@ -263,8 +264,8 @@ mono_marshal_cleanup (void)
 {
        mono_cominterop_cleanup ();
 
-       TlsFree (load_type_info_tls_id);
-       TlsFree (last_error_tls_id);
+       mono_native_tls_free (load_type_info_tls_id);
+       mono_native_tls_free (last_error_tls_id);
        DeleteCriticalSection (&marshal_mutex);
        marshal_mutex_initialized = FALSE;
 }
@@ -10557,9 +10558,9 @@ void
 mono_marshal_set_last_error (void)
 {
 #ifdef WIN32
-       TlsSetValue (last_error_tls_id, GINT_TO_POINTER (GetLastError ()));
+       mono_native_tls_set_value (last_error_tls_id, GINT_TO_POINTER (GetLastError ()));
 #else
-       TlsSetValue (last_error_tls_id, GINT_TO_POINTER (errno));
+       mono_native_tls_set_value (last_error_tls_id, GINT_TO_POINTER (errno));
 #endif
 }
 
@@ -10567,7 +10568,7 @@ static void
 mono_marshal_set_last_error_windows (int error)
 {
 #ifdef WIN32
-       TlsSetValue (last_error_tls_id, GINT_TO_POINTER (error));
+       mono_native_tls_set_value (last_error_tls_id, GINT_TO_POINTER (error));
 #endif
 }
 
@@ -10809,7 +10810,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
 {
        MONO_ARCH_SAVE_REGS;
 
-       return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id)));
+       return (GPOINTER_TO_INT (mono_native_tls_get_value (last_error_tls_id)));
 }
 
 guint32 
@@ -11209,7 +11210,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerIn
 static gboolean
 mono_marshal_is_loading_type_info (MonoClass *klass)
 {
-       GSList *loads_list = TlsGetValue (load_type_info_tls_id);
+       GSList *loads_list = mono_native_tls_get_value (load_type_info_tls_id);
 
        return g_slist_find (loads_list, klass) != NULL;
 }
@@ -11254,9 +11255,9 @@ mono_marshal_load_type_info (MonoClass* klass)
         * under initialization in a TLS list.
         */
        g_assert (!mono_marshal_is_loading_type_info (klass));
-       loads_list = TlsGetValue (load_type_info_tls_id);
+       loads_list = mono_native_tls_get_value (load_type_info_tls_id);
        loads_list = g_slist_prepend (loads_list, klass);
-       TlsSetValue (load_type_info_tls_id, loads_list);
+       mono_native_tls_set_value (load_type_info_tls_id, loads_list);
        
        iter = NULL;
        while ((field = mono_class_get_fields (klass, &iter))) {
@@ -11358,9 +11359,9 @@ mono_marshal_load_type_info (MonoClass* klass)
                mono_marshal_load_type_info (klass->element_class);
        }
 
-       loads_list = TlsGetValue (load_type_info_tls_id);
+       loads_list = mono_native_tls_get_value (load_type_info_tls_id);
        loads_list = g_slist_remove (loads_list, klass);
-       TlsSetValue (load_type_info_tls_id, loads_list);
+       mono_native_tls_set_value (load_type_info_tls_id, loads_list);
 
        /*We do double-checking locking on marshal_info */
        mono_memory_barrier ();
index 4b13fde7b0a6740a515758ca913254cca4e98bd7..ebb15517391bbd602d78cebe63fd55eae25277a5 100644 (file)
@@ -11,6 +11,7 @@
 #include <mono/metadata/object.h>
 #include <mono/metadata/mono-wsq.h>
 #include <mono/utils/mono-semaphore.h>
+#include <mono/utils/mono-tls.h>
 
 #define INITIAL_LENGTH 32
 #define WSQ_DEBUG(...)
@@ -25,21 +26,22 @@ struct _MonoWSQ {
 };
 
 #define NO_KEY ((guint32) -1)
-static guint32 wsq_tlskey = NO_KEY;
+static MonoNativeTlsKey wsq_tlskey;
+static gboolean wsq_tlskey_inited = FALSE;
 
 void
 mono_wsq_init ()
 {
-       wsq_tlskey = TlsAlloc ();
+       mono_native_tls_alloc (wsq_tlskey, NULL);
 }
 
 void
 mono_wsq_cleanup ()
 {
-       if (wsq_tlskey == NO_KEY)
+       if (!wsq_tlskey_inited)
                return;
-       TlsFree (wsq_tlskey);
-       wsq_tlskey = NO_KEY;
+       mono_native_tls_free (wsq_tlskey);
+       wsq_tlskey_inited = FALSE;
 }
 
 MonoWSQ *
@@ -48,7 +50,7 @@ mono_wsq_create ()
        MonoWSQ *wsq;
        MonoDomain *root;
 
-       if (wsq_tlskey == NO_KEY)
+       if (!wsq_tlskey_inited)
                return NULL;
 
        wsq = g_new0 (MonoWSQ, 1);
@@ -57,7 +59,7 @@ mono_wsq_create ()
        root = mono_get_root_domain ();
        wsq->queue = mono_array_new_cached (root, mono_defaults.object_class, INITIAL_LENGTH);
        MONO_SEM_INIT (&wsq->lock, 1);
-       if (!TlsSetValue (wsq_tlskey, wsq)) {
+       if (!mono_native_tls_set_value (wsq_tlskey, wsq)) {
                mono_wsq_destroy (wsq);
                wsq = NULL;
        }
@@ -74,8 +76,8 @@ mono_wsq_destroy (MonoWSQ *wsq)
        MONO_GC_UNREGISTER_ROOT (wsq->queue);
        MONO_SEM_DESTROY (&wsq->lock);
        memset (wsq, 0, sizeof (MonoWSQ));
-       if (wsq_tlskey != NO_KEY && TlsGetValue (wsq_tlskey) == wsq)
-               TlsSetValue (wsq_tlskey, NULL);
+       if (wsq_tlskey_inited && mono_native_tls_get_value (wsq_tlskey) == wsq)
+               mono_native_tls_set_value (wsq_tlskey, NULL);
        g_free (wsq);
 }
 
@@ -95,10 +97,10 @@ mono_wsq_local_push (void *obj)
        int count;
        MonoWSQ *wsq;
 
-       if (obj == NULL || wsq_tlskey == NO_KEY)
+       if (obj == NULL || !wsq_tlskey_inited)
                return FALSE;
 
-       wsq = (MonoWSQ *) TlsGetValue (wsq_tlskey);
+       wsq = (MonoWSQ *) mono_native_tls_get_value (wsq_tlskey);
        if (wsq == NULL) {
                WSQ_DEBUG ("local_push: no wsq\n");
                return FALSE;
@@ -145,10 +147,10 @@ mono_wsq_local_pop (void **ptr)
        gboolean res;
        MonoWSQ *wsq;
 
-       if (ptr == NULL || wsq_tlskey == NO_KEY)
+       if (ptr == NULL || !wsq_tlskey_inited)
                return FALSE;
 
-       wsq = (MonoWSQ *) TlsGetValue (wsq_tlskey);
+       wsq = (MonoWSQ *) mono_native_tls_get_value (wsq_tlskey);
        if (wsq == NULL) {
                WSQ_DEBUG ("local_pop: no wsq\n");
                return FALSE;
@@ -185,10 +187,10 @@ mono_wsq_local_pop (void **ptr)
 void
 mono_wsq_try_steal (MonoWSQ *wsq, void **ptr, guint32 ms_timeout)
 {
-       if (wsq == NULL || ptr == NULL || *ptr != NULL || wsq_tlskey == NO_KEY)
+       if (wsq == NULL || ptr == NULL || *ptr != NULL || !wsq_tlskey_inited)
                return;
 
-       if (TlsGetValue (wsq_tlskey) == wsq)
+       if (mono_native_tls_get_value (wsq_tlskey) == wsq)
                return;
 
        if (mono_sem_timedwait (&wsq->lock, ms_timeout, FALSE) == 0) {
index bb3d8503e8984f5ebb1cd7d0cd7d546196b99c79..069ce9a8c2bbf60fb70d799fa4c17c89348a2d74 100644 (file)
@@ -11,6 +11,7 @@
 #include "mono/utils/mono-compiler.h"
 #include "mono/utils/mono-error.h"
 #include "mono/utils/mono-stack-unwinding.h"
+#include "mono/utils/mono-tls.h"
 
 /* 
  * We should find a better place for this stuff. We can't put it in mono-compiler.h,
@@ -669,13 +670,13 @@ mono_type_initialization_cleanup (void) MONO_INTERNAL;
 int
 mono_thread_kill           (MonoInternalThread *thread, int signal) MONO_INTERNAL;
 
-guint32
+MonoNativeTlsKey
 mono_thread_get_tls_key    (void) MONO_INTERNAL;
 
 gint32
 mono_thread_get_tls_offset (void) MONO_INTERNAL;
 
-guint32
+MonoNativeTlsKey
 mono_domain_get_tls_key    (void) MONO_INTERNAL;
 
 gint32
index 809ac2199e53f916ef5d759fab8249964fd310ec..c7c6a78f5cb0a98273e706039e93b2607ccfd47f 100644 (file)
@@ -44,6 +44,7 @@
 #include <mono/utils/mono-time.h>
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/hazard-pointer.h>
+#include <mono/utils/mono-tls.h>
 
 #include <mono/metadata/gc-internal.h>
 
@@ -147,7 +148,7 @@ static MonoGHashTable *threads_starting_up = NULL;
 static MonoGHashTable *thread_start_args = NULL;
 
 /* The TLS key that holds the MonoObject assigned to each thread */
-static guint32 current_object_key = -1;
+static MonoNativeTlsKey current_object_key;
 
 #ifdef MONO_HAVE_FAST_TLS
 /* we need to use both the Tls* functions and __thread because
@@ -156,12 +157,12 @@ static guint32 current_object_key = -1;
 MONO_FAST_TLS_DECLARE(tls_current_object);
 #define SET_CURRENT_OBJECT(x) do { \
        MONO_FAST_TLS_SET (tls_current_object, x); \
-       TlsSetValue (current_object_key, x); \
+       mono_native_tls_set_value (current_object_key, x); \
 } while (FALSE)
 #define GET_CURRENT_OBJECT() ((MonoInternalThread*) MONO_FAST_TLS_GET (tls_current_object))
 #else
-#define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x)
-#define GET_CURRENT_OBJECT() (MonoInternalThread*) TlsGetValue (current_object_key)
+#define SET_CURRENT_OBJECT(x) mono_native_tls_set_value (current_object_key, x)
+#define GET_CURRENT_OBJECT() (MonoInternalThread*) mono_native_tls_get_value (current_object_key)
 #endif
 
 /* function called at thread start */
@@ -216,7 +217,7 @@ get_next_managed_thread_id (void)
        return InterlockedIncrement (&managed_thread_id_counter);
 }
 
-guint32
+MonoNativeTlsKey
 mono_thread_get_tls_key (void)
 {
        return current_object_key;
@@ -2431,7 +2432,7 @@ void mono_thread_init (MonoThreadStartCB start_cb,
        mono_init_static_data_info (&context_static_info);
 
        MONO_FAST_TLS_INIT (tls_current_object);
-       current_object_key=TlsAlloc();
+       mono_native_tls_alloc (current_object_key, NULL);
        THREAD_DEBUG (g_message ("%s: Allocated current_object_key %d", __func__, current_object_key));
 
        mono_thread_start_cb = start_cb;
@@ -2474,7 +2475,7 @@ void mono_thread_cleanup (void)
        CloseHandle (background_change_event);
 #endif
 
-       TlsFree (current_object_key);
+       mono_native_tls_free (current_object_key);
 }
 
 void
index 3e3c98575ddcd09e16fbde8115d9948fe3bd50d4..433b1061ef26db121a63ff17646e799dd5423725 100644 (file)
@@ -564,7 +564,7 @@ static int frame_id = 0;
 
 static GPtrArray *event_requests;
 
-static guint32 debugger_tls_id;
+static MonoNativeTlsKey debugger_tls_id;
 
 static gboolean vm_start_event_sent, vm_death_event_sent, disconnected;
 
@@ -850,7 +850,7 @@ mono_debugger_agent_init (void)
        mono_profiler_install_jit_end (jit_end);
        mono_profiler_install_method_invoke (start_runtime_invoke, end_runtime_invoke);
 
-       debugger_tls_id = TlsAlloc ();
+       mono_native_tls_alloc (debugger_tls_id, NULL);
 
        thread_to_tls = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_GC);
        MONO_GC_REGISTER_ROOT_FIXED (thread_to_tls);
@@ -2005,7 +2005,7 @@ save_thread_context (MonoContext *ctx)
 {
        DebuggerTlsData *tls;
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (tls);
 
        if (ctx)
@@ -2084,7 +2084,7 @@ mono_debugger_agent_thread_interrupt (void *sigctx, MonoJitInfo *ji)
        if (!inited)
                return FALSE;
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        if (!tls)
                return FALSE;
 
@@ -2392,7 +2392,7 @@ invalidate_frames (DebuggerTlsData *tls)
        int i;
 
        if (!tls)
-               tls = TlsGetValue (debugger_tls_id);
+               tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (tls);
 
        for (i = 0; i < tls->frame_count; ++i) {
@@ -2427,7 +2427,7 @@ suspend_current (void)
                return;
        }
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (tls);
 
        mono_mutex_lock (&suspend_mutex);
@@ -3259,14 +3259,14 @@ thread_startup (MonoProfiler *prof, uintptr_t tid)
                }
        }
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (!tls);
        // FIXME: Free this somewhere
        tls = g_new0 (DebuggerTlsData, 1);
        tls->resume_event = CreateEvent (NULL, FALSE, FALSE, NULL);
        MONO_GC_REGISTER_ROOT_SINGLE (tls->thread);
        tls->thread = thread;
-       TlsSetValue (debugger_tls_id, tls);
+       mono_native_tls_set_value (debugger_tls_id, tls);
 
        DEBUG (1, fprintf (log_file, "[%p] Thread started, obj=%p, tls=%p.\n", (gpointer)tid, thread, tls));
 
@@ -4031,7 +4031,7 @@ process_signal_event (void (*func) (DebuggerTlsData*))
        if (!restore_context)
                restore_context = mono_get_restore_context ();
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        /* Have to save/restore the restore_ctx as we can be called recursively during invokes etc. */
        memcpy (&orig_restore_ctx, &tls->restore_ctx, sizeof (MonoContext));
        memcpy (&tls->restore_ctx, &tls->handler_ctx, sizeof (MonoContext));
@@ -4059,7 +4059,7 @@ resume_from_signal_handler (void *sigctx, void *func)
 
        /* Save the original context in TLS */
        // FIXME: This might not work on an altstack ?
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (tls);
 
        // FIXME: MonoContext usually doesn't include the fp registers, so these are 
@@ -4323,7 +4323,7 @@ debugger_agent_single_step_from_context (MonoContext *ctx)
 {
        DebuggerTlsData *tls;
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (tls);
        memcpy (&tls->restore_ctx, ctx, sizeof (MonoContext));
 
@@ -4335,7 +4335,7 @@ debugger_agent_breakpoint_from_context (MonoContext *ctx)
 {
        DebuggerTlsData *tls;
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (tls);
        memcpy (&tls->restore_ctx, ctx, sizeof (MonoContext));
 
@@ -4723,7 +4723,7 @@ mono_debugger_agent_begin_exception_filter (MonoException *exc, MonoContext *ctx
        if (!inited)
                return;
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        if (!tls)
                return;
 
@@ -4762,7 +4762,7 @@ mono_debugger_agent_end_exception_filter (MonoException *exc, MonoContext *ctx,
        if (!inited)
                return;
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        if (!tls)
                return;
 
@@ -5503,7 +5503,7 @@ invoke_method (void)
        if (!restore_context)
                restore_context = mono_get_restore_context ();
 
-       tls = TlsGetValue (debugger_tls_id);
+       tls = mono_native_tls_get_value (debugger_tls_id);
        g_assert (tls);
 
        /*
index a8eb9328a4de6adaf9f2f6748aeaac4ff221841e..aab64af8498359bcbb463181f8da21aa5d0d3d1d 100644 (file)
@@ -722,7 +722,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
 static void
 handle_signal_exception (gpointer obj, gboolean test_only)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoContext ctx;
        static void (*restore_context) (MonoContext *);
 
@@ -756,7 +756,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
         * signal is disabled, and we could run arbitrary code though the debugger. So
         * resume into the normal stack and do most work there if possible.
         */
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        guint64 sp = UCONTEXT_REG_RSP (ctx);
 
        /* Pass the ctx parameter in TLS */
@@ -891,7 +891,7 @@ mono_arch_ip_from_context (void *sigctx)
 static void
 restore_soft_guard_pages (void)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        if (jit_tls->stack_ovf_guard_base)
                mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
 }
index 68b5a16be4054be1e01f089c01b5f63609f9b152..240161876288b59faab415430fc006c72097a38d 100644 (file)
@@ -516,7 +516,7 @@ mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
 static void
 handle_signal_exception (gpointer obj, gboolean test_only)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoContext ctx;
        static void (*restore_context) (MonoContext *);
 
@@ -558,7 +558,7 @@ mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
         * signal is disabled, and we could run arbitrary code though the debugger. So
         * resume into the normal stack and do most work there if possible.
         */
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        guint64 sp = UCONTEXT_REG_SP (sigctx);
 
        /* Pass the ctx parameter in TLS */
index 6f860587da1958613e5222819f3ef8315159eb29..f9b47214069bd8e8d1b0ef721c7ced3f195fd7b7 100644 (file)
@@ -752,7 +752,7 @@ mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean
 static void
 handle_signal_exception (gpointer obj, gboolean test_only)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoContext ctx;
        static void (*restore_context) (MonoContext *);
 
@@ -793,7 +793,7 @@ mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
         * signal is disabled, and we could run arbitrary code though the debugger. So
         * resume into the normal stack and do most work there if possible.
         */
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        mgreg_t sp;
        void *sigctx = ctx;
        int frame_size;
index 6745bcdeffa1e501935974a61287f76ed0dc502a..dea90055f41d6daab30905665b2280a3d8d130e2 100644 (file)
@@ -118,7 +118,7 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
        DWORD page_size;
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo rji;
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = jit_tls->lmf;            
        MonoContext initial_ctx;
        MonoContext ctx;
@@ -932,7 +932,7 @@ mono_arch_ip_from_context (void *sigctx)
 static void
 handle_signal_exception (gpointer obj)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoContext ctx;
        static void (*restore_context) (MonoContext *);
 
@@ -1027,7 +1027,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
         * signal is disabled, and we could run arbitrary code though the debugger. So
         * resume into the normal stack and do most work there if possible.
         */
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
 
        /* Pass the ctx parameter in TLS */
        mono_arch_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
@@ -1040,7 +1040,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
        return TRUE;
 #elif defined (TARGET_WIN32)
        MonoContext mctx;
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        struct sigcontext *ctx = (struct sigcontext *)sigctx;
 
        mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
@@ -1070,7 +1070,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 static void
 restore_soft_guard_pages (void)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        if (jit_tls->stack_ovf_guard_base)
                mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
 }
index 17d39b1c21fc27503b2987453e99086c13afa98f..cd9839fb64d9f39ee869d73ba522cf3a82bf63e0 100644 (file)
@@ -1024,7 +1024,7 @@ mono_object_castclass (MonoObject *obj, MonoClass *klass)
        MonoJitTlsData *jit_tls = NULL;
 
        if (mini_get_debug_options ()->better_cast_details) {
-               jit_tls = TlsGetValue (mono_jit_tls_id);
+               jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
                jit_tls->class_cast_from = NULL;
        }
 
@@ -1052,7 +1052,7 @@ mono_object_castclass_with_cache (MonoObject *obj, MonoClass *klass, gpointer *c
        gpointer cached_vtable, obj_vtable;
 
        if (mini_get_debug_options ()->better_cast_details) {
-               jit_tls = TlsGetValue (mono_jit_tls_id);
+               jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
                jit_tls->class_cast_from = NULL;
        }
 
index 8a5a09d1b94bfb01d0c762c5732d2383512a8307..c25817307f07b2e8c1343b8de1a7da8b5b7e22ac 100644 (file)
@@ -27,6 +27,7 @@
 #include <mono/utils/mono-math.h>
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/mono-memory-model.h>
+#include <mono/utils/mono-tls.h>
 
 #include "trace.h"
 #include "ir-emit.h"
@@ -213,49 +214,49 @@ amd64_is_near_call (guint8 *code)
 /* amd64_call_reg_internal, which uses amd64_alu_* macros, etc.             */
 /* We only want to force bundle alignment for the top level instruction,    */
 /* so NaCl pseudo-instructions can be implemented with sub instructions.    */
-static guint32 nacl_instruction_depth;
+static MonoNativeTlsKey nacl_instruction_depth;
 
-static guint32 nacl_rex_tag;
-static guint32 nacl_legacy_prefix_tag;
+static MonoNativeTlsKey nacl_rex_tag;
+static MonoNativeTlsKey nacl_legacy_prefix_tag;
 
 void
 amd64_nacl_clear_legacy_prefix_tag ()
 {
-       TlsSetValue (nacl_legacy_prefix_tag, NULL);
+       mono_native_tls_set_value (nacl_legacy_prefix_tag, NULL);
 }
 
 void
 amd64_nacl_tag_legacy_prefix (guint8* code)
 {
-       if (TlsGetValue (nacl_legacy_prefix_tag) == NULL)
-               TlsSetValue (nacl_legacy_prefix_tag, code);
+       if (mono_native_tls_get_value (nacl_legacy_prefix_tag) == NULL)
+               mono_native_tls_set_value (nacl_legacy_prefix_tag, code);
 }
 
 void
 amd64_nacl_tag_rex (guint8* code)
 {
-       TlsSetValue (nacl_rex_tag, code);
+       mono_native_tls_set_value (nacl_rex_tag, code);
 }
 
 guint8*
 amd64_nacl_get_legacy_prefix_tag ()
 {
-       return (guint8*)TlsGetValue (nacl_legacy_prefix_tag);
+       return (guint8*)mono_native_tls_get_value (nacl_legacy_prefix_tag);
 }
 
 guint8*
 amd64_nacl_get_rex_tag ()
 {
-       return (guint8*)TlsGetValue (nacl_rex_tag);
+       return (guint8*)mono_native_tls_get_value (nacl_rex_tag);
 }
 
 /* Increment the instruction "depth" described above */
 void
 amd64_nacl_instruction_pre ()
 {
-       intptr_t depth = (intptr_t) TlsGetValue (nacl_instruction_depth);
+       intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
        depth++;
-       TlsSetValue (nacl_instruction_depth, (gpointer)depth);
+       mono_native_tls_set_value (nacl_instruction_depth, (gpointer)depth);
 }
 
 /* amd64_nacl_instruction_post: Decrement instruction "depth", force bundle */
@@ -266,9 +267,9 @@ amd64_nacl_instruction_pre ()
 void
 amd64_nacl_instruction_post (guint8 **start, guint8 **end)
 {
-       intptr_t depth = (intptr_t) TlsGetValue(nacl_instruction_depth);
+       intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
        depth--;
-       TlsSetValue (nacl_instruction_depth, (void*)depth);
+       mono_native_tls_set_value (nacl_instruction_depth, (void*)depth);
 
        g_assert ( depth >= 0 );
        if (depth == 0) {
@@ -1287,10 +1288,10 @@ mono_arch_init (void)
 
        InitializeCriticalSection (&mini_arch_mutex);
 #if defined(__native_client_codegen__)
-       nacl_instruction_depth = TlsAlloc ();
-       TlsSetValue (nacl_instruction_depth, (gpointer)0);
-       nacl_rex_tag = TlsAlloc ();
-       nacl_legacy_prefix_tag = TlsAlloc ();
+       mono_native_tls_alloc (nacl_instruction_depth, NULL);
+       mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
+       mono_native_tls_alloc (nacl_rex_tag, NULL);
+       mono_native_tls_alloc (nacl_legacy_prefix_tag, NULL);
 #endif
 
 #ifdef MONO_ARCH_NOMAP32BIT
@@ -1325,9 +1326,9 @@ mono_arch_cleanup (void)
 {
        DeleteCriticalSection (&mini_arch_mutex);
 #if defined(__native_client_codegen__)
-       TlsFree (nacl_instruction_depth);
-       TlsFree (nacl_rex_tag);
-       TlsFree (nacl_legacy_prefix_tag);
+       mono_native_tls_free (nacl_instruction_depth);
+       mono_native_tls_free (nacl_rex_tag);
+       mono_native_tls_free (nacl_legacy_prefix_tag);
 #endif
 }
 
index 583b50519b95be946547231be4395492ed8adcde..e1f97f2507436f6abeeb6a08bfc8344d25051bd5 100644 (file)
@@ -635,7 +635,7 @@ static void
 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
 {
        if (!start_ctx) {
-               MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+               MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
                if (jit_tls && jit_tls->orig_ex_ctx_set)
                        start_ctx = &jit_tls->orig_ex_ctx;
        }
@@ -793,7 +793,7 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
                          MonoString **file, gint32 *line, gint32 *column)
 {
        MonoDomain *domain = mono_domain_get ();
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoJitInfo *ji = NULL;
        MonoContext ctx, new_ctx;
@@ -1195,7 +1195,7 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpoin
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo *ji;
        static int (*call_filter) (MonoContext *, gpointer) = NULL;
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoArray *initial_trace_ips = NULL;
        GList *trace_ips = NULL;
@@ -1392,7 +1392,7 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
        MonoJitInfo *ji;
        static int (*call_filter) (MonoContext *, gpointer) = NULL;
        static void (*restore_context) (void *);
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoException *mono_ex;
        gboolean stack_overflow = FALSE;
@@ -1820,7 +1820,7 @@ mono_debugger_run_finally (MonoContext *start_ctx)
 {
        static int (*call_filter) (MonoContext *, gpointer) = NULL;
        MonoDomain *domain = mono_domain_get ();
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoContext ctx, new_ctx;
        MonoJitInfo *ji, rji;
@@ -1969,7 +1969,7 @@ try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
 static void
 try_more_restore (void)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        if (try_restore_stack_protection (jit_tls, 500))
                jit_tls->restore_stack_prot = NULL;
 }
@@ -1977,7 +1977,7 @@ try_more_restore (void)
 static void
 restore_stack_protection (void)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoException *ex = mono_domain_get ()->stack_overflow_ex;
        /* if we can't restore the stack protection, keep a callback installed so
         * we'll try to restore as much stack as we can at each return from unmanaged
@@ -2175,7 +2175,7 @@ mono_handle_native_sigsegv (int signal, void *ctx)
 #ifdef MONO_ARCH_USE_SIGACTION
        struct sigaction sa;
 #endif
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
 
        if (handling_sigsegv)
                return;
@@ -2374,7 +2374,7 @@ mono_print_thread_dump_from_ctx (MonoContext *ctx)
 void
 mono_resume_unwind (MonoContext *ctx)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        static void (*restore_context) (MonoContext *);
        MonoContext new_ctx;
 
@@ -2512,7 +2512,7 @@ mono_set_cast_details (MonoClass *from, MonoClass *to)
        MonoJitTlsData *jit_tls = NULL;
 
        if (mini_get_debug_options ()->better_cast_details) {
-               jit_tls = TlsGetValue (mono_jit_tls_id);
+               jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
                jit_tls->class_cast_from = from;
                jit_tls->class_cast_to = to;
        }
@@ -2608,7 +2608,7 @@ void
 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
 {
 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        jit_tls->ex_ctx = *ctx;
 
        mono_arch_setup_async_callback (ctx, async_cb, user_data);
index cc5a4a2f39fce9ee88e454f4d345881e1c7e2d4c..ae45c11726fdf75a55fe9a6a1d19e83dec96a3fa 100644 (file)
@@ -565,7 +565,7 @@ thread_suspend_func (gpointer user_data, void *sigctx)
        } else {
                tls->has_context = FALSE;
        }
-       tls->jit_tls = TlsGetValue (mono_jit_tls_id);
+       tls->jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
 }
 
 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
index 3398b96d612391fa700679649ee8241f2e67b738..8e9a758a77b75c0b63586e801b6d85520bbe6b26 100644 (file)
@@ -7,6 +7,7 @@
 #include "mini.h"
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/mempool-internals.h>
+#include <mono/utils/mono-tls.h>
 
 #ifndef __STDC_LIMIT_MACROS
 #define __STDC_LIMIT_MACROS
@@ -181,7 +182,7 @@ static LLVMRealPredicate fpcond_to_llvm_cond [] = {
 };
 
 static LLVMExecutionEngineRef ee;
-static guint32 current_cfg_tls_id;
+static MonoNativeTlsKey current_cfg_tls_id;
 
 static MonoLLVMModule jit_module, aot_module;
 static gboolean jit_module_inited;
@@ -4226,7 +4227,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
        mono_loader_lock ();
 
        /* Used to communicate with the callbacks */
-       TlsSetValue (current_cfg_tls_id, cfg);
+       mono_native_tls_set_value (current_cfg_tls_id, cfg);
 
        ctx = g_new0 (EmitContext, 1);
        ctx->cfg = cfg;
@@ -4608,7 +4609,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
        g_free (ctx);
 
-       TlsSetValue (current_cfg_tls_id, NULL);
+       mono_native_tls_set_value (current_cfg_tls_id, NULL);
 
        mono_loader_unlock ();
 }
@@ -4686,7 +4687,7 @@ alloc_cb (LLVMValueRef function, int size)
 {
        MonoCompile *cfg;
 
-       cfg = TlsGetValue (current_cfg_tls_id);
+       cfg = mono_native_tls_get_value (current_cfg_tls_id);
 
        if (cfg) {
                // FIXME: dynamic
@@ -4701,7 +4702,7 @@ emitted_cb (LLVMValueRef function, void *start, void *end)
 {
        MonoCompile *cfg;
 
-       cfg = TlsGetValue (current_cfg_tls_id);
+       cfg = mono_native_tls_get_value (current_cfg_tls_id);
        g_assert (cfg);
        cfg->code_len = (guint8*)end - (guint8*)start;
 }
@@ -4715,7 +4716,7 @@ exception_cb (void *data)
        gpointer *type_info;
        int this_reg, this_offset;
 
-       cfg = TlsGetValue (current_cfg_tls_id);
+       cfg = mono_native_tls_get_value (current_cfg_tls_id);
        g_assert (cfg);
 
        /*
@@ -5090,7 +5091,7 @@ add_intrinsics (LLVMModuleRef module)
 void
 mono_llvm_init (void)
 {
-       current_cfg_tls_id = TlsAlloc ();
+       mono_native_tls_alloc (current_cfg_tls_id, NULL);
 }
 
 static void
index eeffb4e72f35e291c93084f809426d42a5ca016e..88ef8b02ac1aed640b6458eb4e02fb906399b1a6 100644 (file)
@@ -301,7 +301,7 @@ SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
        if (call_chain_depth == 0) {
                mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
        } else {
-               MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+               MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
                int current_frame_index = 1;
                MonoContext mono_context;
                guchar *ips [call_chain_depth + 1];
index 98b20ce50e16cb64347670583dac722f97451b5c..ba2061b4b5c69ccbf27f82afe49d90845028f567 100644 (file)
@@ -990,7 +990,7 @@ mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tram
 {
        MonoContext ctx;
        MonoException *exc;
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        gpointer resume_ip = jit_tls->handler_block_return_address;
 
        memcpy (&ctx, &jit_tls->handler_block_context, sizeof (MonoContext));
index c4d9398ea9471bea18ff7aa2038f7a2cfbf239bf..9e6b383b1d25e3eaf7c74fead3fc2732512b6377 100644 (file)
@@ -53,6 +53,7 @@
 #include <mono/utils/mono-counters.h>
 #include <mono/utils/mono-logger-internal.h>
 #include <mono/utils/mono-mmap.h>
+#include <mono/utils/mono-tls.h>
 #include <mono/utils/dtrace.h>
 
 #include "mini.h"
@@ -75,7 +76,7 @@ static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 op
 static guint32 default_opt = 0;
 static gboolean default_opt_set = FALSE;
 
-guint32 mono_jit_tls_id = -1;
+MonoNativeTlsKey mono_jit_tls_id;
 
 #ifdef MONO_HAVE_FAST_TLS
 MONO_FAST_TLS_DECLARE(mono_jit_tls);
@@ -2460,7 +2461,7 @@ MONO_FAST_TLS_DECLARE(mono_lmf);
 #endif
 #endif
 
-guint32
+MonoNativeTlsKey
 mono_get_jit_tls_key (void)
 {
        return mono_jit_tls_id;
@@ -2502,7 +2503,7 @@ mono_get_lmf (void)
 #else
        MonoJitTlsData *jit_tls;
 
-       if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
+       if ((jit_tls = mono_native_tls_get_value (mono_jit_tls_id)))
                return jit_tls->lmf;
        /*
         * We do not assert here because this function can be called from
@@ -2521,7 +2522,7 @@ mono_get_lmf_addr (void)
 #else
        MonoJitTlsData *jit_tls;
 
-       if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
+       if ((jit_tls = mono_native_tls_get_value (mono_jit_tls_id)))
                return &jit_tls->lmf;
 
        /*
@@ -2534,7 +2535,7 @@ mono_get_lmf_addr (void)
 
        mono_jit_thread_attach (NULL);
        
-       if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
+       if ((jit_tls = mono_native_tls_get_value (mono_jit_tls_id)))
                return &jit_tls->lmf;
 
        g_assert_not_reached ();
@@ -2555,7 +2556,7 @@ mono_set_lmf (MonoLMF *lmf)
 static void
 mono_set_jit_tls (MonoJitTlsData *jit_tls)
 {
-       TlsSetValue (mono_jit_tls_id, jit_tls);
+       mono_native_tls_set_value (mono_jit_tls_id, jit_tls);
 
 #ifdef MONO_HAVE_FAST_TLS
        MONO_FAST_TLS_SET (mono_jit_tls, jit_tls);
@@ -2588,7 +2589,7 @@ mono_jit_thread_attach (MonoDomain *domain)
                mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background);
        }
 #else
-       if (!TlsGetValue (mono_jit_tls_id)) {
+       if (!mono_native_tls_get_value (mono_jit_tls_id)) {
                mono_thread_attach (domain);
                mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background);
        }
@@ -2606,7 +2607,7 @@ mono_jit_thread_attach (MonoDomain *domain)
 static void
 mono_thread_abort (MonoObject *obj)
 {
-       /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
+       /* MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id); */
        
        /* handle_remove should be eventually called for this thread, too
        g_free (jit_tls);*/
@@ -2634,7 +2635,7 @@ setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
        MonoJitTlsData *jit_tls;
        MonoLMF *lmf;
 
-       jit_tls = TlsGetValue (mono_jit_tls_id);
+       jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        if (jit_tls)
                return jit_tls;
 
@@ -5817,7 +5818,7 @@ void
 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler)
 {
        MonoJitInfo *ji;
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        gpointer fault_addr = NULL;
 
        GET_CONTEXT;
@@ -6287,7 +6288,7 @@ mini_init (const char *filename, const char *runtime_version)
        if (!g_thread_supported ())
                g_thread_init (NULL);
 
-       mono_jit_tls_id = TlsAlloc ();
+       mono_native_tls_alloc (mono_jit_tls_id, NULL);
        setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
 
        if (default_opt & MONO_OPT_AOT)
@@ -6696,7 +6697,7 @@ mini_cleanup (MonoDomain *domain)
        mono_runtime_cleanup (domain);
 #endif
 
-       free_jit_tls_data (TlsGetValue (mono_jit_tls_id));
+       free_jit_tls_data (mono_native_tls_get_value (mono_jit_tls_id));
 
        mono_icall_cleanup ();
 
@@ -6737,7 +6738,7 @@ mini_cleanup (MonoDomain *domain)
        if (mono_inject_async_exc_method)
                mono_method_desc_free (mono_inject_async_exc_method);
 
-       TlsFree(mono_jit_tls_id);
+       mono_native_tls_free (mono_jit_tls_id);
 
        DeleteCriticalSection (&jit_mutex);
 
index 078ab23148640bfc575edb54912eed1405f1f01f..184e9fc0f57a0d741487a73d064bd576f364e404 100644 (file)
@@ -23,6 +23,7 @@
 #include <mono/utils/mono-machine.h>
 #include <mono/utils/mono-stack-unwinding.h>
 #include <mono/utils/mono-threads.h>
+#include <mono/utils/mono-tls.h>
 
 #define MONO_BREAKPOINT_ARRAY_SIZE 64
 
@@ -440,7 +441,7 @@ typedef struct MonoLMF MonoLMF;
 typedef struct MonoSpillInfo MonoSpillInfo;
 typedef struct MonoTraceSpec MonoTraceSpec;
 
-extern guint32 mono_jit_tls_id;
+extern MonoNativeTlsKey mono_jit_tls_id;
 extern MonoTraceSpec *mono_jit_trace_calls;
 extern gboolean mono_break_on_exc;
 extern int mono_exc_esp_offset;
@@ -1737,7 +1738,7 @@ MonoLMF * mono_get_lmf                      (void) MONO_INTERNAL;
 MonoLMF** mono_get_lmf_addr                 (void) MONO_INTERNAL;
 void      mono_set_lmf                      (MonoLMF *lmf) MONO_INTERNAL;
 void      mono_jit_thread_attach            (MonoDomain *domain);
-guint32   mono_get_jit_tls_key              (void) MONO_INTERNAL;
+MonoNativeTlsKey mono_get_jit_tls_key       (void) MONO_INTERNAL;
 gint32    mono_get_jit_tls_offset           (void) MONO_INTERNAL;
 gint32    mono_get_lmf_tls_offset           (void) MONO_INTERNAL;
 gint32    mono_get_lmf_addr_tls_offset      (void) MONO_INTERNAL;
index 73d71fe074a8f9db5793922f31147d2369dc96dc..a49be599e512a0f04c789c940411dd692e8e2843 100644 (file)
@@ -54,7 +54,7 @@ continuation_mark_frame (MonoContinuation *cont)
        if (cont->domain)
                return mono_get_exception_argument ("cont", "Already marked");
 
-       jit_tls = TlsGetValue (mono_jit_tls_id);
+       jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        lmf = mono_get_lmf();
        cont->domain = mono_domain_get ();
        cont->thread_id = GetCurrentThreadId ();
index 39f2ba1c509dc570957e1edba21127a971ecf2ed..c8bdfeaf077c4531834564ad5dd6e4e5d514115e 100644 (file)
@@ -1179,7 +1179,7 @@ mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
 static void
 handler_block_trampoline_helper (gpointer *ptr)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        *ptr = jit_tls->handler_block_return_address;
 }
 
index 1bbfc97d4c00b0d98c915011c565ca5e929eb7d3..54079cb200365c6b0c91954bbe2d4d8768fe7128 100644 (file)
@@ -1040,7 +1040,7 @@ mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
 static void
 handler_block_trampoline_helper (gpointer *ptr)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        *ptr = jit_tls->handler_block_return_address;
 }
 
index 7ad574b82f449b5df390133858b53a87b79ff53c..194bc9368d82f68b72d712f58c5e05da5f305060 100644 (file)
@@ -16,6 +16,7 @@
 #include <mono/metadata/threads.h>
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/mono-gc.h>
+#include <mono/utils/mono-tls.h>
 #include <mono/io-layer/atomic.h>
 #include <string.h>
 #include <stdio.h>
@@ -777,11 +778,11 @@ make_pthread_profiler_key (void) {
 #define CURRENT_THREAD_ID() (gsize) GetCurrentThreadId ()
 
 #ifndef HAVE_KW_THREAD
-static guint32 profiler_thread_id = -1;
-#define LOOKUP_PROFILER_THREAD_DATA() ((ProfilerPerThreadData*)TlsGetValue (profiler_thread_id))
-#define SET_PROFILER_THREAD_DATA(x) TlsSetValue (profiler_thread_id, (x));
-#define ALLOCATE_PROFILER_THREAD_DATA() profiler_thread_id = TlsAlloc ()
-#define FREE_PROFILER_THREAD_DATA() TlsFree (profiler_thread_id)
+static MonoNativeTlsKey profiler_thread_id;
+#define LOOKUP_PROFILER_THREAD_DATA() ((ProfilerPerThreadData*)mono_native_tls_get_value (profiler_thread_id))
+#define SET_PROFILER_THREAD_DATA(x) mono_native_tls_set_value (profiler_thread_id, (x));
+#define ALLOCATE_PROFILER_THREAD_DATA() mono_native_tls_alloc (profiler_thread_id, NULL)
+#define FREE_PROFILER_THREAD_DATA() mono_native_tls_free (profiler_thread_id)
 #endif
 
 #define EVENT_TYPE HANDLE
index 2d2a0b2dee3154f91f97d555c75fcde6963af587..52ce6f049e790ba6bd1795969f5433792b0686c9 100644 (file)
@@ -17,6 +17,7 @@
 
 #define MonoNativeTlsKey DWORD
 #define mono_native_tls_alloc(key,destructor) ((key = TlsAlloc ()) != TLS_OUT_OF_INDEXES && destructor == NULL)
+#define mono_native_tls_free TlsFree
 #define mono_native_tls_set_value TlsSetValue
 #define mono_native_tls_get_value TlsGetValue
 
@@ -26,7 +27,8 @@
 
 #define MonoNativeTlsKey pthread_key_t
 #define mono_native_tls_alloc(key,destructor) (pthread_key_create (&key, destructor) == 0) 
-#define mono_native_tls_set_value pthread_setspecific
+#define mono_native_tls_free pthread_key_delete
+#define mono_native_tls_set_value(k,v) (!pthread_setspecific ((k), (v)))
 #define mono_native_tls_get_value pthread_getspecific
 
 #endif /* HOST_WIN32 */