Skip to content

Commit

Permalink
[runtime] Fix MONO_ARCH_ENABLE_MONO_LMF_VAR
Browse files Browse the repository at this point in the history
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).


Commit migrated from mono/mono@da1e7bb
  • Loading branch information
BrzVlad committed Dec 16, 2016
1 parent caf3b83 commit 5bbc3c4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/mono/mono/mini/mini-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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*
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 2 additions & 4 deletions src/mono/mono/utils/mono-tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -270,13 +266,15 @@ 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;
#elif defined(TARGET_MACH)
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;
Expand Down
12 changes: 12 additions & 0 deletions src/mono/mono/utils/mono-tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef __MONO_TLS_H__
#define __MONO_TLS_H__

#include <config.h>
#include <glib.h>

/* TLS entries used by the runtime */
Expand All @@ -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>
Expand Down

0 comments on commit 5bbc3c4

Please sign in to comment.