[runtime] Fix MONO_ARCH_ENABLE_MONO_LMF_VAR
authorVlad Brezae <brezaevlad@gmail.com>
Fri, 16 Dec 2016 01:00:18 +0000 (03:00 +0200)
committerVlad Brezae <brezaevlad@gmail.com>
Fri, 16 Dec 2016 19:28:47 +0000 (21:28 +0200)
Using MONO_ARCH_ENABLE_MONO_LMF_VAR requires that we have means of getting the address of a tls variable (which is always the case when using __thread, but not necessarily when using pthread). Make sure that using it is properly guarded.

In the future we might want to remove MONO_ARCH_ENABLE_LMF_VAR and always try to use a tls variable for the lmf. Additionally we should benchmark what combinations work best and stick to them on all platforms. (ex when using fast tls we might consider using lmf_get + lmf_set if possible, when using fallbacks we might always want to use the lmf_addr to void multiple calls, always use lmf_ir etc).

mono/mini/mini-runtime.c
mono/utils/mono-tls.c
mono/utils/mono-tls.h

index 0c67814f126135e9e89b8a59f6204a26b1539e8e..a7383e612845db83ae03b10d0784c071cc155a4f 100644 (file)
@@ -691,7 +691,7 @@ register_dyn_icall (gpointer func, const char *name, const char *sigstr, gboolea
 MonoLMF *
 mono_get_lmf (void)
 {
-#if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
+#if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR) && defined(HAVE_GET_TLS_ADDR)
        return (MonoLMF *)mono_tls_get_lmf ();
 #else
        MonoJitTlsData *jit_tls;
@@ -716,11 +716,11 @@ mono_get_lmf_addr (void)
 void
 mono_set_lmf (MonoLMF *lmf)
 {
-#if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
+#if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR) && defined(HAVE_GET_TLS_ADDR)
        mono_tls_set_lmf (lmf);
-#endif
-
+#else
        (*mono_get_lmf_addr ()) = lmf;
+#endif
 }
 
 MonoJitTlsData*
@@ -852,7 +852,15 @@ setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
 
        jit_tls->first_lmf = lmf;
 
-#if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
+       /*
+        * We can have 2 configurations for accessing lmf.
+        * We can use only the tls_lmf_addr variable, which will store the address of
+        * jit_tls->lmf, or, if we have MONO_ARCH_ENABLE_MONO_LMF_VAR enabled, we can
+        * use both tls_lmf_addr and tls_lmf variables (in this case we need to have
+        * means of getting the address of a tls variable; this can be done always
+        * when using __thread or, on osx, even when using pthread)
+        */
+#if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR) && defined(HAVE_GET_TLS_ADDR)
        /* jit_tls->lmf is unused */
        mono_tls_set_lmf (lmf);
        mono_set_lmf_addr (mono_tls_get_tls_addr (TLS_KEY_LMF));
index 765011dbfe81aa5551e410a2fb01e9065a2be6a0..186e1d5c551b46a67be4772ce7791665faafdb46 100644 (file)
@@ -7,7 +7,6 @@
  * Copyright 2013 Xamarin, Inc (http://www.xamarin.com)
  */
 
-#include <config.h>
 #include <mono/utils/mach-support.h>
 
 #include "mono-tls.h"
@@ -34,9 +33,6 @@
  * wrappers and managed allocators, both of which are not aot-ed by default.
  * So far, we never supported inlined fast tls on full-aot systems.
  */
-#ifdef HAVE_KW_THREAD
-#define USE_KW_THREAD
-#endif
 
 #ifdef USE_KW_THREAD
 
@@ -270,6 +266,7 @@ mono_tls_get_tls_setter (MonoTlsKey key, gboolean name)
 gpointer
 mono_tls_get_tls_addr (MonoTlsKey key)
 {
+#ifdef HAVE_GET_TLS_ADDR
        if (key == TLS_KEY_LMF) {
 #if defined(USE_KW_THREAD)
                return &mono_tls_lmf;
@@ -277,6 +274,7 @@ mono_tls_get_tls_addr (MonoTlsKey key)
                return mono_mach_get_tls_address_from_thread (pthread_self (), mono_tls_key_lmf);
 #endif
        }
+#endif
        /* Implement if we ever need for other targets/keys */
        g_assert_not_reached ();
        return NULL;
index 8cccc2875599a0a0c5ba0d0e7d64342a2d81eda8..a3751ea6e38b7ee2bbc60aaa1390f35d1eb2b88d 100644 (file)
@@ -12,6 +12,7 @@
 #ifndef __MONO_TLS_H__
 #define __MONO_TLS_H__
 
+#include <config.h>
 #include <glib.h>
 
 /* TLS entries used by the runtime */
@@ -27,6 +28,17 @@ typedef enum {
        TLS_KEY_NUM = 6
 } MonoTlsKey;
 
+#ifdef HAVE_KW_THREAD
+#define USE_KW_THREAD
+#endif
+
+#if defined(USE_KW_THREAD)
+#define HAVE_GET_TLS_ADDR
+#elif defined(TARGET_MACH) && (defined(TARGET_X86) || defined(TARGET_AMD64))
+/* mono_mach_get_tls_address_from_thread is untested for arm/arm64 */
+#define HAVE_GET_TLS_ADDR
+#endif
+
 #ifdef HOST_WIN32
 
 #include <windows.h>