2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_THREADING_DETAILS_GCC_MANAGER_H
32 #define CDSLIB_THREADING_DETAILS_GCC_MANAGER_H
34 #if !( CDS_COMPILER == CDS_COMPILER_GCC || CDS_COMPILER == CDS_COMPILER_CLANG || CDS_COMPILER == CDS_COMPILER_INTEL)
35 # error "threading/details/gcc_manager.h may be used only with GCC or Clang C++ compiler"
38 #include <cds/threading/details/_common.h>
41 namespace cds { namespace threading {
45 typedef unsigned char ThreadDataPlaceholder[ sizeof(ThreadData) ];
46 static __thread ThreadDataPlaceholder CDS_DATA_ALIGNMENT(8) s_threadData;
47 static __thread ThreadData * s_pThreadData;
51 /// cds::threading::Manager implementation based on GCC __thread declaration
52 inline namespace gcc {
54 /// Thread-specific data manager based on GCC __thread feature
59 static ThreadData * _threadData()
61 return gcc_internal::s_pThreadData;
64 static ThreadData * create_thread_data()
66 if ( !gcc_internal::s_pThreadData ) {
67 gcc_internal::s_pThreadData = new (gcc_internal::s_threadData) ThreadData();
69 return gcc_internal::s_pThreadData;
72 static void destroy_thread_data()
74 if ( gcc_internal::s_pThreadData ) {
75 ThreadData * p = gcc_internal::s_pThreadData;
76 gcc_internal::s_pThreadData = nullptr;
77 p->ThreadData::~ThreadData();
83 /// Initialize manager (empty function)
85 This function is automatically called by cds::Initialize
90 /// Terminate manager (empty function)
92 This function is automatically called by cds::Terminate
97 /// Checks whether current thread is attached to \p libcds feature or not.
98 static bool isThreadAttached()
100 return _threadData() != nullptr;
103 /// This method must be called in beginning of thread execution
104 static void attachThread()
106 create_thread_data()->init();
109 /// This method must be called in end of thread execution
110 static void detachThread()
112 assert( _threadData());
114 if ( _threadData()->fini())
115 destroy_thread_data();
118 /// Returns ThreadData pointer for the current thread
119 static ThreadData * thread_data()
121 ThreadData * p = _threadData();
127 static size_t fake_current_processor()
129 return _threadData()->fake_current_processor();
136 }} // namespace cds::threading
139 #endif // #ifndef CDSLIB_THREADING_DETAILS_GCC_MANAGER_H