Added new files to VC 2015 projects
[libcds.git] / src / thread_data.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #include <cds/threading/details/_common.h>
32 #include <cds/gc/hp.h>
33 #include <cds/gc/dhp.h>
34
35 namespace cds { namespace threading {
36
37     CDS_EXPORT_API atomics::atomic<size_t> ThreadData::s_nLastUsedProcNo( 0 );
38     CDS_EXPORT_API size_t ThreadData::s_nProcCount = 1;
39
40     CDS_EXPORT_API void ThreadData::init()
41     {
42         if ( m_nAttachCount++ == 0 ) {
43             if ( cds::gc::HP::isUsed() )
44                 cds::gc::hp::smr::attach_thread();
45             if ( cds::gc::DHP::isUsed() )
46                 cds::gc::dhp::smr::attach_thread();
47
48             if ( cds::urcu::details::singleton<cds::urcu::general_instant_tag>::isUsed() )
49                 m_pGPIRCU = cds::urcu::details::singleton<cds::urcu::general_instant_tag>::attach_thread();
50             if ( cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::isUsed() )
51                 m_pGPBRCU = cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::attach_thread();
52             if ( cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::isUsed() )
53                 m_pGPTRCU = cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::attach_thread();
54 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
55             if ( cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::isUsed() )
56                 m_pSHBRCU = cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::attach_thread();
57             if ( cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::isUsed() )
58                 m_pSHTRCU = cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::attach_thread();
59 #endif
60         }
61     }
62
63     CDS_EXPORT_API bool ThreadData::fini()
64     {
65         if ( --m_nAttachCount == 0 ) {
66             if ( cds::gc::DHP::isUsed() )
67                 cds::gc::dhp::smr::detach_thread();
68             if ( cds::gc::HP::isUsed() )
69                 cds::gc::hp::smr::detach_thread();
70
71             if ( cds::urcu::details::singleton<cds::urcu::general_instant_tag>::isUsed() ) {
72                 cds::urcu::details::singleton<cds::urcu::general_instant_tag>::detach_thread( m_pGPIRCU );
73                 m_pGPIRCU = nullptr;
74             }
75             if ( cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::isUsed() ) {
76                 cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::detach_thread( m_pGPBRCU );
77                 m_pGPBRCU = nullptr;
78             }
79             if ( cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::isUsed() ) {
80                 cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::detach_thread( m_pGPTRCU );
81                 m_pGPTRCU = nullptr;
82             }
83 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
84             if ( cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::isUsed() ) {
85                 cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::detach_thread( m_pSHBRCU );
86                 m_pSHBRCU = nullptr;
87             }
88             if ( cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::isUsed() ) {
89                 cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::detach_thread( m_pSHTRCU );
90                 m_pSHTRCU = nullptr;
91             }
92 #endif
93             return true;
94         }
95         return false;
96     }
97
98
99 }} // namespace cds::threading