3 #ifndef CDSLIB_THREADING_DETAILS_CXX11_MANAGER_H
4 #define CDSLIB_THREADING_DETAILS_CXX11_MANAGER_H
6 #include <cds/threading/details/_common.h>
8 #ifndef CDS_CXX11_THREAD_LOCAL_SUPPORT
9 # error "The compiler does not support C++11 thread_local keyword. You cannot use CDS_THREADING_CXX11 threading model."
13 namespace cds { namespace threading {
16 struct cxx11_internal {
17 typedef unsigned char ThreadDataPlaceholder[ sizeof(ThreadData) ];
18 static thread_local ThreadDataPlaceholder CDS_DATA_ALIGNMENT(8) s_threadData;
19 static thread_local ThreadData * s_pThreadData;
23 /// cds::threading::Manager implementation based on c++11 thread_local declaration
24 CDS_CXX11_INLINE_NAMESPACE namespace cxx11 {
26 /// Thread-specific data manager based on c++11 thread_local feature
31 static ThreadData * _threadData()
33 return cxx11_internal::s_pThreadData;
36 static ThreadData * create_thread_data()
38 if ( !cxx11_internal::s_pThreadData ) {
39 cxx11_internal::s_pThreadData = new (cxx11_internal::s_threadData) ThreadData();
41 return cxx11_internal::s_pThreadData;
44 static void destroy_thread_data()
46 if ( cxx11_internal::s_pThreadData ) {
47 ThreadData * p = cxx11_internal::s_pThreadData;
48 cxx11_internal::s_pThreadData = nullptr;
49 p->ThreadData::~ThreadData();
55 /// Initialize manager (empty function)
57 This function is automatically called by cds::Initialize
62 /// Terminate manager (empty function)
64 This function is automatically called by cds::Terminate
69 /// Checks whether current thread is attached to \p libcds feature or not.
70 static bool isThreadAttached()
72 return _threadData() != nullptr;
75 /// This method must be called in beginning of thread execution
76 static void attachThread()
78 create_thread_data()->init();
81 /// This method must be called in end of thread execution
82 static void detachThread()
84 assert( _threadData() );
86 if ( _threadData()->fini() )
87 destroy_thread_data();
90 /// Returns ThreadData pointer for the current thread
91 static ThreadData * thread_data()
93 ThreadData * p = _threadData();
98 /// Get gc::HP thread GC implementation for current thread
100 The object returned may be uninitialized if you did not call attachThread in the beginning of thread execution
101 or if you did not use gc::HP.
102 To initialize gc::HP GC you must constuct cds::gc::HP object in the beginning of your application
104 static gc::HP::thread_gc_impl& getHZPGC()
106 assert( _threadData()->m_hpManager != nullptr );
107 return *(_threadData()->m_hpManager);
110 /// Get gc::DHP thread GC implementation for current thread
112 The object returned may be uninitialized if you did not call attachThread in the beginning of thread execution
113 or if you did not use gc::DHP.
114 To initialize gc::DHP GC you must constuct cds::gc::DHP object in the beginning of your application
116 static gc::DHP::thread_gc_impl& getDHPGC()
118 assert( _threadData()->m_dhpManager != nullptr );
119 return *(_threadData()->m_dhpManager);
123 static size_t fake_current_processor()
125 return _threadData()->fake_current_processor();
132 }} // namespace cds::threading
135 #endif // #ifndef CDSLIB_THREADING_DETAILS_CXX11_MANAGER_H