Merge branch 'ldionne-ldionne-cmake' into dev
[libcds.git] / cds / threading / details / wintls_manager.h
index 5ffdc9817d9e7f0a88caf14b5cf21d974aa7672e..a7d6e9da2c1d31867fe67895b60fa9284c7593e9 100644 (file)
@@ -1,10 +1,40 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
 
-#ifndef __CDS_THREADING_DETAILS_WINTLS_MANAGER_H
-#define __CDS_THREADING_DETAILS_WINTLS_MANAGER_H
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
 
+    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_DETAILS_WINTLS_MANAGER_H
+#define CDSLIB_THREADING_DETAILS_WINTLS_MANAGER_H
+
+#include <system_error>
 #include <stdio.h>
 #include <cds/threading/details/_common.h>
+#include <cds/details/throw_exception.h>
 
 //@cond
 namespace cds { namespace threading {
@@ -22,22 +52,13 @@ namespace cds { namespace threading {
             typedef DWORD api_error_code;
 
             /// TLS API exception
-            class api_exception: public cds::Exception {
-            public:
-                const api_error_code    m_errCode   ;   ///< error code
+            class api_exception : public std::system_error
+            {
             public:
                 /// Exception constructor
                 api_exception( api_error_code nCode, const char * pszFunction )
-                    : m_errCode( nCode )
-                {
-                    char buf[256];
-#           if CDS_OS_TYPE == CDS_OS_MINGW
-                    sprintf( buf, "Win32 TLS API error %lu [function %s]", nCode, pszFunction );
-#           else
-                    sprintf_s( buf, sizeof(buf)/sizeof(buf[0]), "Win32 TLS API error %lu [function %s]", nCode, pszFunction );
-#           endif
-                    m_strMsg = buf;
-                }
+                    : std::system_error( static_cast<int>(nCode), std::system_category(), pszFunction )
+                {}
             };
 
             //@cond
@@ -57,8 +78,8 @@ namespace cds { namespace threading {
                 static void init()
                 {
                     if ( m_key == TLS_OUT_OF_INDEXES ) {
-                        if ( (m_key = ::TlsAlloc()) == TLS_OUT_OF_INDEXES )
-                            throw api_exception( ::GetLastError(), "TlsAlloc" );
+                        if ( ( m_key = ::TlsAlloc()) == TLS_OUT_OF_INDEXES )
+                            CDS_THROW_EXCEPTION( api_exception( ::GetLastError(), "TlsAlloc" ));
                     }
                 }
 
@@ -66,7 +87,7 @@ namespace cds { namespace threading {
                 {
                     if ( m_key != TLS_OUT_OF_INDEXES ) {
                         if ( ::TlsFree( m_key ) == 0 )
-                            throw api_exception( ::GetLastError(), "TlsFree" );
+                            CDS_THROW_EXCEPTION( api_exception( ::GetLastError(), "TlsFree" ));
                         m_key = TLS_OUT_OF_INDEXES;
                     }
                 }
@@ -75,8 +96,8 @@ namespace cds { namespace threading {
                 {
                     api_error_code  nErr;
                     void * pData = ::TlsGetValue( m_key );
-                    if ( pData == nullptr && (nErr = ::GetLastError()) != ERROR_SUCCESS )
-                        throw api_exception( nErr, "TlsGetValue" );
+                    if ( pData == nullptr && ( nErr = ::GetLastError()) != ERROR_SUCCESS )
+                        CDS_THROW_EXCEPTION( api_exception( nErr, "TlsGetValue" ));
                     return reinterpret_cast<ThreadData *>( pData );
                 }
 
@@ -84,7 +105,7 @@ namespace cds { namespace threading {
                 {
                     ThreadData * pData = new ThreadData;
                     if ( !::TlsSetValue( m_key, pData ))
-                        throw api_exception( ::GetLastError(), "TlsSetValue" );
+                        CDS_THROW_EXCEPTION( api_exception( ::GetLastError(), "TlsSetValue" ));
                 }
                 static void free()
                 {
@@ -163,11 +184,10 @@ namespace cds { namespace threading {
                 ThreadData * pData = _threadData( do_attachThread );
                 assert( pData );
 
-                if ( pData ) {
+                if ( pData )
                     pData->init();
-                }
                 else
-                    throw api_exception( api_error_code(-1), "cds::threading::wintls::Manager::attachThread" );
+                    CDS_THROW_EXCEPTION( api_exception( api_error_code(-1), "cds::threading::wintls::Manager::attachThread" ));
             }
 
             /// This method must be called in end of thread execution
@@ -183,11 +203,11 @@ namespace cds { namespace threading {
                 assert( pData );
 
                 if ( pData ) {
-                    if ( pData->fini() )
+                    if ( pData->fini())
                         _threadData( do_detachThread );
                 }
                 else
-                    throw api_exception( api_error_code(-1), "cds::threading::winapi::Manager::detachThread" );
+                    CDS_THROW_EXCEPTION( api_exception( api_error_code(-1), "cds::threading::winapi::Manager::detachThread" ));
             }
 
             /// Returns ThreadData pointer for the current thread
@@ -196,28 +216,6 @@ namespace cds { namespace threading {
                 return _threadData( do_getData );
             }
 
-            /// Get gc::HP thread GC implementation for current thread
-            /**
-                The object returned may be uninitialized if you did not call attachThread in the beginning of thread execution
-                or if you did not use gc::HP.
-                To initialize gc::HP GC you must constuct cds::gc::HP object in the beginning of your application
-            */
-            static gc::HP::thread_gc_impl&   getHZPGC()
-            {
-                return *(_threadData( do_getData )->m_hpManager);
-            }
-
-            /// Get gc::DHP thread GC implementation for current thread
-            /**
-                The object returned may be uninitialized if you did not call attachThread in the beginning of thread execution
-                or if you did not use gc::DHP.
-                To initialize gc::DHP GC you must constuct cds::gc::DHP object in the beginning of your application
-            */
-            static gc::DHP::thread_gc_impl&   getDHPGC()
-            {
-                return *(_threadData( do_getData )->m_dhpManager);
-            }
-
             //@cond
             static size_t fake_current_processor()
             {
@@ -230,4 +228,4 @@ namespace cds { namespace threading {
 }} // namespace cds::threading
 //@endcond
 
-#endif // #ifndef __CDS_THREADING_DETAILS_WINTLS_MANAGER_H
+#endif // #ifndef CDSLIB_THREADING_DETAILS_WINTLS_MANAGER_H