Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r86396 - in trunk: boost/chrono/detail/inlined/win boost/sync/detail libs/sync/src
From: andrey.semashev_at_[hidden]
Date: 2013-10-22 14:23:22


Author: andysem
Date: 2013-10-22 14:23:22 EDT (Tue, 22 Oct 2013)
New Revision: 86396
URL: http://svn.boost.org/trac/boost/changeset/86396

Log:
Added support for compiler-based TLS.

Text files modified:
   trunk/boost/chrono/detail/inlined/win/chrono.hpp | 7 +++--
   trunk/boost/sync/detail/config.hpp | 17 ++++++++++++++
   trunk/libs/sync/src/tss_windows.cpp | 47 ++++++++++++++++++++++++++++++++++-----
   3 files changed, 61 insertions(+), 10 deletions(-)

Modified: trunk/boost/chrono/detail/inlined/win/chrono.hpp
==============================================================================
--- trunk/boost/chrono/detail/inlined/win/chrono.hpp Tue Oct 22 13:59:44 2013 (r86395)
+++ trunk/boost/chrono/detail/inlined/win/chrono.hpp 2013-10-22 14:23:22 EDT (Tue, 22 Oct 2013) (r86396)
@@ -94,8 +94,8 @@
     return system_clock::time_point(
       system_clock::duration(
         ((static_cast<__int64>( ft.dwHighDateTime ) << 32) | ft.dwLowDateTime)
- //- 116444736000000000LL
- - (134775LL*864000000000LL)
+ - 116444736000000000LL
+ //- (134775LL*864000000000LL)
       )
     );
   }
@@ -113,7 +113,8 @@
     return system_clock::time_point(
       system_clock::duration(
        ((static_cast<__int64>( ft.dwHighDateTime ) << 32) | ft.dwLowDateTime)
- - (134775LL*864000000000LL)
+ - 116444736000000000LL
+ //- (134775LL*864000000000LL)
        ));
   }
 #endif

Modified: trunk/boost/sync/detail/config.hpp
==============================================================================
--- trunk/boost/sync/detail/config.hpp Tue Oct 22 13:59:44 2013 (r86395)
+++ trunk/boost/sync/detail/config.hpp 2013-10-22 14:23:22 EDT (Tue, 22 Oct 2013) (r86396)
@@ -47,6 +47,23 @@
 
 #endif
 
+#if !defined(BOOST_SYNC_USE_COMPILER_TLS)
+#if defined(BOOST_SYNC_DETAIL_PLATFORM_WINAPI) && BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
+// Compiler-based TLS are safe to use in DLLs only since Windows Vista
+#define BOOST_SYNC_USE_COMPILER_TLS
+#endif
+#endif // !defined(BOOST_SYNC_USE_COMPILER_TLS)
+
+#if defined(BOOST_SYNC_USE_COMPILER_TLS)
+#if defined(__GNUC__) || defined(__SUNPRO_CC)
+#define BOOST_SYNC_DETAIL_TLS __thread
+#elif defined(BOOST_MSVC)
+#define BOOST_SYNC_DETAIL_TLS __declspec(thread)
+#else
+#undef BOOST_SYNC_USE_COMPILER_TLS
+#endif
+#endif // defined(BOOST_SYNC_USE_COMPILER_TLS)
+
 #if defined(BOOST_SYNC_DETAIL_PLATFORM_POSIX)
 #define BOOST_SYNC_DETAIL_ABI_NAMESPACE posix
 #elif defined(BOOST_SYNC_DETAIL_PLATFORM_LINUX)

Modified: trunk/libs/sync/src/tss_windows.cpp
==============================================================================
--- trunk/libs/sync/src/tss_windows.cpp Tue Oct 22 13:59:44 2013 (r86395)
+++ trunk/libs/sync/src/tss_windows.cpp 2013-10-22 14:23:22 EDT (Tue, 22 Oct 2013) (r86396)
@@ -40,7 +40,11 @@
 static long init_tss_once_flag = 0;
 #endif
 static tss_manager* tss_mgr = NULL;
+#if defined(BOOST_SYNC_DETAIL_TLS)
+static BOOST_SYNC_DETAIL_TLS tss_manager::thread_context* tss_context = NULL;
+#else
 static DWORD tss_key = TLS_OUT_OF_INDEXES;
+#endif
 
 extern "C" {
 
@@ -57,9 +61,11 @@
         std::abort();
     }
 
+#if !defined(BOOST_SYNC_DETAIL_TLS)
     tss_key = TlsAlloc();
     if (tss_key == TLS_OUT_OF_INDEXES)
         std::abort();
+#endif
 
     return TRUE;
 }
@@ -75,7 +81,7 @@
 
 #else // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
 
-BOOST_FORCEINLINE void init_tss_once()
+BOOST_FORCEINLINE void init_tss_once() BOOST_NOEXCEPT
 {
     if (const_cast< long volatile& >(init_tss_once_flag) != 2)
     {
@@ -98,6 +104,30 @@
 
 #endif // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
 
+#if defined(BOOST_SYNC_DETAIL_TLS)
+
+BOOST_FORCEINLINE tss_manager::thread_context* get_thread_context() BOOST_NOEXCEPT
+{
+ return tss_context;
+}
+BOOST_FORCEINLINE void set_thread_context(tss_manager::thread_context* p) BOOST_NOEXCEPT
+{
+ tss_context = p;
+}
+
+#else // defined(BOOST_SYNC_DETAIL_TLS)
+
+BOOST_FORCEINLINE tss_manager::thread_context* get_thread_context() BOOST_NOEXCEPT
+{
+ return static_cast< tss_manager::thread_context* >(TlsGetValue(tss_key));
+}
+BOOST_FORCEINLINE void set_thread_context(tss_manager::thread_context* p) BOOST_NOEXCEPT
+{
+ TlsSetValue(tss_key, p);
+}
+
+#endif // defined(BOOST_SYNC_DETAIL_TLS)
+
 } // namespace
 
 namespace windows {
@@ -112,8 +142,10 @@
     tss_mgr = NULL;
     delete p;
 
+#if !defined(BOOST_SYNC_DETAIL_TLS)
     TlsFree(tss_key);
     tss_key = TLS_OUT_OF_INDEXES;
+#endif
 }
 
 void on_thread_enter()
@@ -124,9 +156,10 @@
 {
     // This callback may be called before tss manager is initialized
     init_tss_once();
- if (tss_manager::thread_context* ctx = static_cast< tss_manager::thread_context* >(TlsGetValue(tss_key)))
+ if (tss_manager::thread_context* ctx = get_thread_context())
     {
         tss_mgr->destroy_thread_context(ctx);
+ set_thread_context(NULL);
     }
 }
 
@@ -135,12 +168,12 @@
 BOOST_SYNC_API void add_thread_exit_callback(at_thread_exit_callback callback, void* context)
 {
     init_tss_once();
- tss_manager::thread_context* ctx = static_cast< tss_manager::thread_context* >(TlsGetValue(tss_key));
+ tss_manager::thread_context* ctx = get_thread_context();
 
     if (!ctx)
     {
         ctx = tss_mgr->create_thread_context();
- TlsSetValue(tss_key, ctx);
+ set_thread_context(ctx);
     }
 
     ctx->add_at_exit_entry(callback, context);
@@ -166,7 +199,7 @@
 
 BOOST_SYNC_API void* get_thread_specific(thread_specific_key key)
 {
- tss_manager::thread_context* ctx = static_cast< tss_manager::thread_context* >(TlsGetValue(tss_key));
+ tss_manager::thread_context* ctx = get_thread_context();
     if (ctx)
         return ctx->get_value(key);
     return NULL;
@@ -174,12 +207,12 @@
 
 BOOST_SYNC_API void set_thread_specific(thread_specific_key key, void* p)
 {
- tss_manager::thread_context* ctx = static_cast< tss_manager::thread_context* >(TlsGetValue(tss_key));
+ tss_manager::thread_context* ctx = get_thread_context();
 
     if (!ctx)
     {
         ctx = tss_mgr->create_thread_context();
- TlsSetValue(tss_key, ctx);
+ set_thread_context(ctx);
     }
 
     ctx->set_value(key, p);


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk