From nobody Mon Sep 17 00:00:00 2001 From: Pavel Afremov Date: Wed, 4 Apr 2007 11:36:37 +0400 Subject: [PATCH] Fast TLS access via intrinsics on Windows platforms. --- vm/include/open/hythread.h | 33 +++++++++++++++++++++++++++------ vm/include/open/hythread_ext.h | 31 +++---------------------------- vm/thread/src/thread_native_basic.c | 18 +++++++++++++----- 3 files changed, 43 insertions(+), 39 deletions(-) af29183b11cd08c7ddabcd8c7c7310d0c9607290 diff --git a/vm/include/open/hythread.h b/vm/include/open/hythread.h index 837cbea..5e0e586 100644 --- a/vm/include/open/hythread.h +++ b/vm/include/open/hythread.h @@ -27,6 +27,14 @@ #include #include #include "hycomp.h" + +#ifdef WIN32 +# if (_MSC_VER >= 1400) +# include +# endif +#endif + + /* * Idea behind these declarations is to make some functions static inlined for * all files that include hythread_ext.h/hythread.h except one, that @@ -400,7 +408,8 @@ #endif -#if (defined (WIN32) && !defined (_WIN64)) +#if defined (WIN32) + //use optimized asm monitor enter and exit helpers #define ASM_MONITOR_HELPER @@ -443,11 +452,23 @@ extern "C" { #endif /* __cplusplus */ hy_inline hythread_t VMCALL hythread_self() { - register hythread_t t; - _asm { mov eax, fs:[0x14] - mov t, eax; - } - return t; +#ifndef _WIN64 +# if (_MSC_VER >= 1400) + // file winnt.h can't be included here + // 0x14 = offsetof(NT_TIB, ArbitraryUserPointer) + return (hythread_t) __readfsdword(0x14); +# else + register hythread_t t; + _asm { mov eax, fs:[0x14] + mov t, eax; + } + return t; +# endif +#else + // file winnt.h can't be included here + // 0x28 = offsetof(NT_TIB, ArbitraryUserPointer) + return (hythread_t) __readgsqword(0x28); +#endif } #define tm_self_tls (hythread_self()) diff --git a/vm/include/open/hythread_ext.h b/vm/include/open/hythread_ext.h index 45c61e3..818a8cc 100644 --- a/vm/include/open/hythread_ext.h +++ b/vm/include/open/hythread_ext.h @@ -351,20 +351,11 @@ hy_inline IDATA VMCALL hythread_is_suspe * point where safe suspension is possible. */ hy_inline void VMCALL hythread_suspend_enable() { + register hythread_t thread; assert(!hythread_is_suspend_enabled()); -#ifdef FS14_TLS_USE - // the macros could work for WIN32 - __asm { - mov eax, fs:[0x14] - dec[eax] HyThread_public.disable_count - } -#else - { - register hythread_t thread = tm_self_tls; - ((HyThread_public *)thread)->disable_count--; - } -#endif + thread = tm_self_tls; + ((HyThread_public *)thread)->disable_count--; } /** @@ -389,24 +380,8 @@ hy_inline void VMCALL hythread_suspend_d // default group only. assert(((HyThread_public *)hythread_self())->group == get_java_thread_group()); -#ifdef FS14_TLS_USE - // the macros could work for WIN32 - __asm { - mov eax, fs:[0x14] - inc[eax] HyThread_public.disable_count - mov eax,[eax] HyThread_public.request - test eax, eax - jnz suspended - } - return; - - suspended: - thread = tm_self_tls; - -#else thread = tm_self_tls; ((HyThread_public *)thread)->disable_count++; -#endif if (((HyThread_public *)thread)->request && ((HyThread_public *)thread)->disable_count == 1) { diff --git a/vm/thread/src/thread_native_basic.c b/vm/thread/src/thread_native_basic.c index 52e4122..87b88ae 100644 --- a/vm/thread/src/thread_native_basic.c +++ b/vm/thread/src/thread_native_basic.c @@ -381,11 +381,19 @@ hythread_t hythread_self_slow() { } static void thread_set_self(hythread_t thread) { - //tm_self_tls = thread; - _asm{ - mov eax, thread - mov fs:[0x14], eax - } + // tm_self_tls = thread; +#ifndef _WIN64 +# if (_MSC_VER >= 1400) + __writefsdword(offsetof(NT_TIB, ArbitraryUserPointer), thread); +# else + _asm{ + mov eax, thread + mov fs:[0x14], eax + } +# endif +#else + __writegsqword(offsetof(NT_TIB, ArbitraryUserPointer), thread); +#endif } #else /** -- 1.3.3