Merge branch 'dev' of github.com:khizmax/libcds into dev
[libcds.git] / cds / threading / details / _common.h
index be0acb49cfee810186b9db24d4f9f1885cbaa3d2..f8f059964a6c0b9a652c5e4a562cf336dd3f77d0 100644 (file)
@@ -1,11 +1,35 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
 
-#ifndef __CDS_THREADING__COMMON_H
-#define __CDS_THREADING__COMMON_H
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
 
-#include <cds/gc/hp_decl.h>
-#include <cds/gc/hrc_decl.h>
-#include <cds/gc/ptb_decl.h>
+    Source code repo: http://github.com/khizmax/libcds/
+    Download: http://sourceforge.net/projects/libcds/files/
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this
+      list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright notice,
+      this list of conditions and the following disclaimer in the documentation
+      and/or other materials provided with the distribution.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef CDSLIB_THREADING__COMMON_H
+#define CDSLIB_THREADING__COMMON_H
 
 #include <cds/urcu/details/gp_decl.h>
 #include <cds/urcu/details/sh_decl.h>
@@ -19,7 +43,7 @@ namespace cds {
         The library does not dictate any thread model. To embed the library to your application you should choose
         appropriate implementation of \p cds::threading::Manager interface
         or should provide yourself.
-        The \p %cds::threading::Manager interface manages \p ñds::threading::ThreadData structure that contains GC's thread specific data.
+        The \p %cds::threading::Manager interface manages \p cds::threading::ThreadData structure that contains GC's thread specific data.
 
         Any \p cds::threading::Manager implementation is a singleton and it must be accessible from any thread and from any point of
         your application. Note that you should not mix different implementation of the \p cds::threading::Manager in your application.
@@ -55,10 +79,10 @@ namespace cds {
         \code
         class Manager {
         public:
-            // Initialize manager (called by cds::Initialize() )
+            // Initialize manager (called by cds::Initialize())
             static void init();
 
-            // Terminate manager (called by cds::Terminate() )
+            // Terminate manager (called by cds::Terminate())
             static void fini();
 
             // Checks whether current thread is attached to \p libcds feature or not.
@@ -72,14 +96,8 @@ namespace cds {
             // (called by dtor of GC thread object, for example, by dtor of cds::gc::HP::thread_gc)
             static void detachThread();
 
-            // Get cds::gc::HP thread GC implementation for current thread
-            static gc::HP::thread_gc_impl&   getHZPGC();
-
-            // Get cds::gc::HRC thread GC implementation for current thread
-            static gc::HRC::thread_gc_impl&   getHRCGC();
-
-            // Get cds::gc::PTB thread GC implementation for current thread;
-            static gc::PTB::thread_gc_impl&   getPTBGC();
+            // Get cds::gc::DHP thread GC implementation for current thread;
+            static gc::DHP::thread_gc_impl&   getDHPGC();
         };
         \endcode
 
@@ -113,10 +131,6 @@ namespace cds {
         struct ThreadData {
 
             //@cond
-            char CDS_DATA_ALIGNMENT(8) m_hpManagerPlaceholder[sizeof(cds::gc::HP::thread_gc_impl)]   ;   ///< Michael's Hazard Pointer GC placeholder
-            char CDS_DATA_ALIGNMENT(8) m_hrcManagerPlaceholder[sizeof(cds::gc::HRC::thread_gc_impl)]  ;   ///< Gidenstam's GC placeholder
-            char CDS_DATA_ALIGNMENT(8) m_ptbManagerPlaceholder[sizeof(cds::gc::PTB::thread_gc_impl)]  ;   ///< Pass The Buck GC placeholder
-
             cds::urcu::details::thread_data< cds::urcu::general_instant_tag > *     m_pGPIRCU;
             cds::urcu::details::thread_data< cds::urcu::general_buffered_tag > *    m_pGPBRCU;
             cds::urcu::details::thread_data< cds::urcu::general_threaded_tag > *    m_pGPTRCU;
@@ -127,10 +141,6 @@ namespace cds {
 
             //@endcond
 
-            cds::gc::HP::thread_gc_impl  * m_hpManager     ;   ///< Michael's Hazard Pointer GC thread-specific data
-            cds::gc::HRC::thread_gc_impl * m_hrcManager    ;   ///< Gidenstam's GC thread-specific data
-            cds::gc::PTB::thread_gc_impl * m_ptbManager    ;   ///< Pass The Buck GC thread-specific data
-
             size_t  m_nFakeProcessorNumber  ;   ///< fake "current processor" number
 
             //@cond
@@ -141,128 +151,36 @@ namespace cds {
             cds::algo::elimination::record   m_EliminationRec;
 
             //@cond
-            static CDS_EXPORT_API CDS_ATOMIC::atomic<size_t> s_nLastUsedProcNo;
-            static CDS_EXPORT_API size_t                     s_nProcCount;
+            static CDS_EXPORT_API atomics::atomic<size_t> s_nLastUsedProcNo;
+            static CDS_EXPORT_API size_t                  s_nProcCount;
             //@endcond
 
             //@cond
             ThreadData()
-                : m_pGPIRCU( NULL )
-                , m_pGPBRCU( NULL )
-                , m_pGPTRCU( NULL )
+                : m_pGPIRCU( nullptr )
+                , m_pGPBRCU( nullptr )
+                , m_pGPTRCU( nullptr )
 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
-                , m_pSHBRCU( NULL )
-                , m_pSHTRCU( NULL )
+                , m_pSHBRCU( nullptr )
+                , m_pSHTRCU( nullptr )
 #endif
-                , m_nFakeProcessorNumber( s_nLastUsedProcNo.fetch_add(1, CDS_ATOMIC::memory_order_relaxed) % s_nProcCount )
+                , m_nFakeProcessorNumber( s_nLastUsedProcNo.fetch_add(1, atomics::memory_order_relaxed) % s_nProcCount )
                 , m_nAttachCount(0)
-            {
-                if (cds::gc::HP::isUsed() )
-                    m_hpManager = new (m_hpManagerPlaceholder) cds::gc::HP::thread_gc_impl;
-                else
-                    m_hpManager = nullptr;
-
-                if ( cds::gc::HRC::isUsed() )
-                    m_hrcManager = new (m_hrcManagerPlaceholder) cds::gc::HRC::thread_gc_impl;
-                else
-                    m_hrcManager = nullptr;
-
-                if ( cds::gc::PTB::isUsed() )
-                    m_ptbManager = new (m_ptbManagerPlaceholder) cds::gc::PTB::thread_gc_impl;
-                else
-                    m_ptbManager = nullptr;
-            }
+            {}
 
             ~ThreadData()
             {
-                if ( m_hpManager ) {
-                    typedef cds::gc::HP::thread_gc_impl hp_thread_gc_impl;
-                    m_hpManager->~hp_thread_gc_impl();
-                    m_hpManager = nullptr;
-                }
-
-                if ( m_hrcManager ) {
-                    typedef cds::gc::HRC::thread_gc_impl hrc_thread_gc_impl;
-                    m_hrcManager->~hrc_thread_gc_impl();
-                    m_hrcManager = nullptr;
-                }
-
-                if ( m_ptbManager ) {
-                    typedef cds::gc::PTB::thread_gc_impl ptb_thread_gc_impl;
-                    m_ptbManager->~ptb_thread_gc_impl();
-                    m_ptbManager = nullptr;
-                }
-
-                assert( m_pGPIRCU == NULL );
-                assert( m_pGPBRCU == NULL );
-                assert( m_pGPTRCU == NULL );
-#ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
-                assert( m_pSHBRCU == NULL );
-                assert( m_pSHTRCU == NULL );
-#endif
-            }
-
-            void init()
-            {
-                if ( m_nAttachCount++ == 0 ) {
-                    if ( cds::gc::HP::isUsed() )
-                        m_hpManager->init();
-                    if ( cds::gc::HRC::isUsed() )
-                        m_hrcManager->init();
-                    if ( cds::gc::PTB::isUsed() )
-                        m_ptbManager->init();
-
-                    if ( cds::urcu::details::singleton<cds::urcu::general_instant_tag>::isUsed() )
-                        m_pGPIRCU = cds::urcu::details::singleton<cds::urcu::general_instant_tag>::attach_thread();
-                    if ( cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::isUsed() )
-                        m_pGPBRCU = cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::attach_thread();
-                    if ( cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::isUsed() )
-                        m_pGPTRCU = cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::attach_thread();
+                assert( m_pGPIRCU == nullptr );
+                assert( m_pGPBRCU == nullptr );
+                assert( m_pGPTRCU == nullptr );
 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
-                    if ( cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::isUsed() )
-                        m_pSHBRCU = cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::attach_thread();
-                    if ( cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::isUsed() )
-                        m_pSHTRCU = cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::attach_thread();
+                assert( m_pSHBRCU == nullptr );
+                assert( m_pSHTRCU == nullptr );
 #endif
-                }
             }
 
-            bool fini()
-            {
-                if ( --m_nAttachCount == 0 ) {
-                    if ( cds::gc::PTB::isUsed() )
-                        m_ptbManager->fini();
-                    if ( cds::gc::HRC::isUsed() )
-                        m_hrcManager->fini();
-                    if ( cds::gc::HP::isUsed() )
-                        m_hpManager->fini();
-
-                    if ( cds::urcu::details::singleton<cds::urcu::general_instant_tag>::isUsed() ) {
-                        cds::urcu::details::singleton<cds::urcu::general_instant_tag>::detach_thread( m_pGPIRCU );
-                        m_pGPIRCU = NULL;
-                    }
-                    if ( cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::isUsed() ) {
-                        cds::urcu::details::singleton<cds::urcu::general_buffered_tag>::detach_thread( m_pGPBRCU );
-                        m_pGPBRCU = NULL;
-                    }
-                    if ( cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::isUsed() ) {
-                        cds::urcu::details::singleton<cds::urcu::general_threaded_tag>::detach_thread( m_pGPTRCU );
-                        m_pGPTRCU = NULL;
-                    }
-#ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
-                    if ( cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::isUsed() ) {
-                        cds::urcu::details::singleton<cds::urcu::signal_buffered_tag>::detach_thread( m_pSHBRCU );
-                        m_pSHBRCU = NULL;
-                    }
-                    if ( cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::isUsed() ) {
-                        cds::urcu::details::singleton<cds::urcu::signal_threaded_tag>::detach_thread( m_pSHTRCU );
-                        m_pSHTRCU = NULL;
-                    }
-#endif
-                    return true;
-                }
-                return false;
-            }
+            CDS_EXPORT_API void init();
+            CDS_EXPORT_API bool fini();
 
             size_t fake_current_processor()
             {
@@ -273,6 +191,6 @@ namespace cds {
         //@endcond
 
     } // namespace threading
-} // namespace cds::threading
+} // namespace cds
 
-#endif // #ifndef __CDS_THREADING__COMMON_H
+#endif // #ifndef CDSLIB_THREADING__COMMON_H