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_MSVC_MANAGER_H
32 #define CDSLIB_THREADING_DETAILS_MSVC_MANAGER_H
34 #if !( CDS_COMPILER == CDS_COMPILER_MSVC || (CDS_COMPILER == CDS_COMPILER_INTEL && CDS_OS_INTERFACE == CDS_OSI_WINDOWS))
35 # error "threading/details/msvc_manager.h may be used only with Microsoft Visual C++ / Intel C++ compiler"
38 #include <cds/threading/details/_common.h>
41 namespace cds { namespace threading {
44 struct msvc_internal {
45 typedef unsigned char ThreadDataPlaceholder[ sizeof(ThreadData) ];
46 __declspec( thread ) static ThreadDataPlaceholder s_threadData;
47 __declspec( thread ) static ThreadData * s_pThreadData;
51 /// cds::threading::Manager implementation based on Microsoft Visual C++ __declspec( thread ) construction
52 CDS_CXX11_INLINE_NAMESPACE namespace msvc {
54 /// Thread-specific data manager based on MSVC __declspec( thread ) feature
58 static ThreadData * _threadData()
60 return msvc_internal::s_pThreadData;
63 static ThreadData * create_thread_data()
65 if ( !msvc_internal::s_pThreadData ) {
66 msvc_internal::s_pThreadData = new (msvc_internal::s_threadData) ThreadData();
68 return msvc_internal::s_pThreadData;
71 static void destroy_thread_data()
73 if ( msvc_internal::s_pThreadData ) {
74 msvc_internal::s_pThreadData->ThreadData::~ThreadData();
75 msvc_internal::s_pThreadData = nullptr;
82 /// Initialize manager (empty function)
84 This function is automatically called by cds::Initialize
89 /// Terminate manager (empty function)
91 This function is automatically called by cds::Terminate
96 /// Checks whether current thread is attached to \p libcds feature or not.
97 static bool isThreadAttached()
99 ThreadData * pData = _threadData();
100 return pData != 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 internal 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();
135 }} // namespace cds::threading
138 #endif // #ifndef CDSLIB_THREADING_DETAILS_MSVC_MANAGER_H