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_PTHREAD_MANAGER_H
32 #define CDSLIB_THREADING_DETAILS_PTHREAD_MANAGER_H
34 #include <system_error>
37 #include <cds/threading/details/_common.h>
38 #include <cds/details/throw_exception.h>
41 namespace cds { namespace threading {
43 /// cds::threading::Manager implementation based on pthread thread-specific data functions
44 CDS_CXX11_INLINE_NAMESPACE namespace pthread {
46 /// Thread-specific data manager based on pthread thread-specific data functions
48 Manager throws an exception of Manager::pthread_exception class if an error occurs
52 /// pthread error code type
53 typedef int pthread_error_code;
56 class pthread_exception: public std::system_error
59 /// Exception constructor
60 pthread_exception( int nCode, const char * pszFunction )
61 : std::system_error( nCode, std::system_category(), pszFunction )
65 /// pthread TLS key holder
68 static pthread_key_t m_key;
70 static void key_destructor(void * p)
73 reinterpret_cast<ThreadData *>(p)->fini();
74 delete reinterpret_cast<ThreadData *>(p);
80 pthread_error_code nErr;
81 if ( ( nErr = pthread_key_create( &m_key, key_destructor )) != 0 )
82 CDS_THROW_EXCEPTION( pthread_exception( nErr, "pthread_key_create" ));
87 pthread_error_code nErr;
88 if ( ( nErr = pthread_key_delete( m_key )) != 0 )
89 CDS_THROW_EXCEPTION( pthread_exception( nErr, "pthread_key_delete" ));
92 static ThreadData * get()
94 return reinterpret_cast<ThreadData *>( pthread_getspecific( m_key ));
99 pthread_error_code nErr;
100 ThreadData * pData = new ThreadData;
101 if ( ( nErr = pthread_setspecific( m_key, pData )) != 0 )
102 CDS_THROW_EXCEPTION( pthread_exception( nErr, "pthread_setspecific" ));
106 ThreadData * p = get();
107 pthread_setspecific( m_key, nullptr );
126 static ThreadData * _threadData( EThreadAction nAction )
130 return Holder::get();
132 return Holder::get();
133 case do_attachThread:
134 if ( Holder::get() == nullptr )
136 return Holder::get();
137 case do_detachThread:
144 assert( false ) ; // anything forgotten?..
146 assert(false) ; // how did we get here?
152 /// Initialize manager
154 This function is automatically called by cds::Initialize
161 /// Terminate manager
163 This function is automatically called by cds::Terminate
170 /// Checks whether current thread is attached to \p libcds feature or not.
171 static bool isThreadAttached()
173 return _threadData( do_checkData ) != nullptr;
176 /// This method must be called in beginning of thread execution
178 If TLS pointer to manager's data is \p nullptr, pthread_exception is thrown
180 If an error occurs in call of pthread API function, pthread_exception is thrown
181 with pthread error code.
183 static void attachThread()
185 ThreadData * pData = _threadData( do_attachThread );
192 CDS_THROW_EXCEPTION( pthread_exception( -1, "cds::threading::pthread::Manager::attachThread" ));
195 /// This method must be called in end of thread execution
197 If TLS pointer to manager's data is \p nullptr, pthread_exception is thrown
199 If an error occurs in call of pthread API function, pthread_exception is thrown
200 with pthread error code.
202 static void detachThread()
204 ThreadData * pData = _threadData( do_getData );
209 _threadData( do_detachThread );
212 CDS_THROW_EXCEPTION( pthread_exception( -1, "cds::threading::pthread::Manager::detachThread" ));
215 /// Returns ThreadData pointer for the current thread
216 static ThreadData * thread_data()
218 return _threadData( do_getData );
222 static size_t fake_current_processor()
224 return _threadData( do_getData )->fake_current_processor();
230 } // namespace pthread
231 }} // namespace cds::threading
234 #endif // #ifndef CDSLIB_THREADING_DETAILS_PTHREAD_MANAGER_H