3 #ifndef __CDS_THREADING_DETAILS_GCC_MANAGER_H
4 #define __CDS_THREADING_DETAILS_GCC_MANAGER_H
6 #if !( CDS_COMPILER == CDS_COMPILER_GCC || CDS_COMPILER == CDS_COMPILER_CLANG || CDS_COMPILER == CDS_COMPILER_INTEL)
7 # error "threading/details/gcc_manager.h may be used only with GCC or Clang C++ compiler"
10 #include <cds/threading/details/_common.h>
13 namespace cds { namespace threading {
17 typedef unsigned char ThreadDataPlaceholder[ sizeof(ThreadData) ];
18 static __thread ThreadDataPlaceholder CDS_DATA_ALIGNMENT(8) s_threadData;
19 static __thread ThreadData * s_pThreadData;
23 /// cds::threading::Manager implementation based on GCC __thread declaration
24 CDS_CXX11_INLINE_NAMESPACE namespace gcc {
26 /// Thread-specific data manager based on GCC __thread feature
31 static ThreadData * _threadData()
33 return gcc_internal::s_pThreadData;
36 static ThreadData * create_thread_data()
38 if ( !gcc_internal::s_pThreadData ) {
39 gcc_internal::s_pThreadData = new (gcc_internal::s_threadData) ThreadData();
41 return gcc_internal::s_pThreadData;
44 static void destroy_thread_data()
46 if ( gcc_internal::s_pThreadData ) {
47 ThreadData * p = gcc_internal::s_pThreadData;
48 gcc_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 );
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 );
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 __CDS_THREADING_DETAILS_GCC_MANAGER_H