From 8d16489fcd6f3dc1cc76cf089182673400f6267d Mon Sep 17 00:00:00 2001 From: chris Date: Sun, 28 Sep 2003 07:58:09 +0000 Subject: [PATCH] Refactored tss_bool into separate classes for each platform. --- asio/src/Makefile.am | 2 + asio/src/asio/detail/pthread_tss_bool.hpp | 77 ++++++++++++++++++++++ asio/src/asio/detail/tss_bool.hpp | 74 ++------------------- asio/src/asio/detail/win_tss_bool.hpp | 79 +++++++++++++++++++++++ 4 files changed, 162 insertions(+), 70 deletions(-) create mode 100644 asio/src/asio/detail/pthread_tss_bool.hpp create mode 100644 asio/src/asio/detail/win_tss_bool.hpp diff --git a/asio/src/Makefile.am b/asio/src/Makefile.am index 13e95f2b..9589dd3e 100644 --- a/asio/src/Makefile.am +++ b/asio/src/Makefile.am @@ -29,6 +29,7 @@ nobase_include_HEADERS = \ asio/detail/pop_options.hpp \ asio/detail/pthread_mutex.hpp \ asio/detail/pthread_thread.hpp \ + asio/detail/pthread_tss_bool.hpp \ asio/detail/push_options.hpp \ asio/detail/reactive_dgram_socket_service.hpp \ asio/detail/reactive_socket_acceptor_service.hpp \ @@ -52,6 +53,7 @@ nobase_include_HEADERS = \ asio/detail/win_iocp_dgram_socket_service.hpp \ asio/detail/win_mutex.hpp \ asio/detail/win_thread.hpp \ + asio/detail/win_tss_bool.hpp \ asio/detail/winsock_init.hpp \ asio/dgram_socket.hpp \ asio/inet_address_v4.hpp \ diff --git a/asio/src/asio/detail/pthread_tss_bool.hpp b/asio/src/asio/detail/pthread_tss_bool.hpp new file mode 100644 index 00000000..68ea01b5 --- /dev/null +++ b/asio/src/asio/detail/pthread_tss_bool.hpp @@ -0,0 +1,77 @@ +// +// pthread_tss_bool.hpp +// ~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003 Christopher M. Kohlhoff (chris@kohlhoff.com) +// +// Permission to use, copy, modify, distribute and sell this software and its +// documentation for any purpose is hereby granted without fee, provided that +// the above copyright notice appears in all copies and that both the copyright +// notice and this permission notice appear in supporting documentation. This +// software is provided "as is" without express or implied warranty, and with +// no claim as to its suitability for any purpose. +// + +#ifndef ASIO_DETAIL_PTHREAD_TSS_BOOL_HPP +#define ASIO_DETAIL_PTHREAD_TSS_BOOL_HPP + +#include "asio/detail/push_options.hpp" + +#if !defined(_WIN32) + +#include "asio/detail/push_options.hpp" +#include +#include +#include "asio/detail/pop_options.hpp" + +namespace asio { +namespace detail { + +class pthread_tss_bool +{ +public: + // Constructor. + pthread_tss_bool() + { + if (::pthread_key_create(&tss_key_, 0) != 0) + throw std::runtime_error("Cannot create thread-local storage"); + } + + // Destructor. + ~pthread_tss_bool() + { + ::pthread_key_delete(tss_key_); + } + + // Test the value of the flag. + operator bool() const + { + return ::pthread_getspecific(tss_key_) != 0; + } + + // Test for the value of the flag being false. + bool operator!() const + { + return ::pthread_getspecific(tss_key_) == 0; + } + + // Set the value of the flag. + void operator=(bool value) + { + ::pthread_setspecific(tss_key_, value ? this : 0); + } + +private: + // Thread-specific storage to allow unlocked access to determine whether a + // thread is a member of the pool. + mutable pthread_key_t tss_key_; +}; + +} // namespace detail +} // namespace asio + +#endif // !defined(_WIN32) + +#include "asio/detail/pop_options.hpp" + +#endif // ASIO_DETAIL_PTHREAD_TSS_BOOL_HPP diff --git a/asio/src/asio/detail/tss_bool.hpp b/asio/src/asio/detail/tss_bool.hpp index 8cd949ef..57e0a46a 100644 --- a/asio/src/asio/detail/tss_bool.hpp +++ b/asio/src/asio/detail/tss_bool.hpp @@ -17,83 +17,17 @@ #include "asio/detail/push_options.hpp" -#include "asio/detail/push_options.hpp" -#include -#if defined(_WIN32) -#include "asio/detail/socket_types.hpp" -#else -#include -#endif // defined(_WIN32) -#include "asio/detail/pop_options.hpp" +#include "asio/detail/pthread_tss_bool.hpp" +#include "asio/detail/win_tss_bool.hpp" namespace asio { namespace detail { -class tss_bool -{ -public: - // Constructor. - tss_bool() - { #if defined(_WIN32) - tss_key_ = ::TlsAlloc(); - if (tss_key_ == TLS_OUT_OF_INDEXES) - throw std::runtime_error("Cannot create thread-local storage"); -#else // defined(_WIN32) - if (::pthread_key_create(&tss_key_, 0) != 0) - throw std::runtime_error("Cannot create thread-local storage"); -#endif // defined(_WIN32) - } - - // Destructor. - ~tss_bool() - { -#if defined(_WIN32) - ::TlsFree(tss_key_); -#else // defined(_WIN32) - ::pthread_key_delete(tss_key_); -#endif // defined(_WIN32) - } - - // Test the value of the flag. - operator bool() const - { -#if defined(_WIN32) - return ::TlsGetValue(tss_key_) != 0; -#else // defined(_WIN32) - return ::pthread_getspecific(tss_key_) != 0; -#endif // defined(_WIN32) - } - - // Test for the value of the flag being false. - bool operator!() const - { -#if defined(_WIN32) - return ::TlsGetValue(tss_key_) == 0; -#else // defined(_WIN32) - return ::pthread_getspecific(tss_key_) == 0; -#endif // defined(_WIN32) - } - - // Set the value of the flag. - void operator=(bool value) - { -#if defined(_WIN32) - ::TlsSetValue(tss_key_, value ? this : 0); -#else // defined(_WIN32) - ::pthread_setspecific(tss_key_, value ? this : 0); -#endif // defined(_WIN32) - } - -private: - // Thread-specific storage to allow unlocked access to determine whether a - // thread is a member of the pool. -#if defined(_WIN32) - mutable unsigned long tss_key_; +typedef win_tss_bool tss_bool; #else - mutable pthread_key_t tss_key_; +typedef pthread_tss_bool tss_bool; #endif -}; } // namespace detail } // namespace asio diff --git a/asio/src/asio/detail/win_tss_bool.hpp b/asio/src/asio/detail/win_tss_bool.hpp new file mode 100644 index 00000000..d913d862 --- /dev/null +++ b/asio/src/asio/detail/win_tss_bool.hpp @@ -0,0 +1,79 @@ +// +// win_tss_bool.hpp +// ~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003 Christopher M. Kohlhoff (chris@kohlhoff.com) +// +// Permission to use, copy, modify, distribute and sell this software and its +// documentation for any purpose is hereby granted without fee, provided that +// the above copyright notice appears in all copies and that both the copyright +// notice and this permission notice appear in supporting documentation. This +// software is provided "as is" without express or implied warranty, and with +// no claim as to its suitability for any purpose. +// + +#ifndef ASIO_DETAIL_WIN_TSS_BOOL_HPP +#define ASIO_DETAIL_WIN_TSS_BOOL_HPP + +#include "asio/detail/push_options.hpp" + +#if defined(_WIN32) + +#include "asio/detail/push_options.hpp" +#include +#include "asio/detail/pop_options.hpp" + +#include "asio/detail/socket_types.hpp" + +namespace asio { +namespace detail { + +class win_tss_bool +{ +public: + // Constructor. + win_tss_bool() + { + tss_key_ = ::TlsAlloc(); + if (tss_key_ == TLS_OUT_OF_INDEXES) + throw std::runtime_error("Cannot create thread-local storage"); + } + + // Destructor. + ~win_tss_bool() + { + ::TlsFree(tss_key_); + } + + // Test the value of the flag. + operator bool() const + { + return ::TlsGetValue(tss_key_) != 0; + } + + // Test for the value of the flag being false. + bool operator!() const + { + return ::TlsGetValue(tss_key_) == 0; + } + + // Set the value of the flag. + void operator=(bool value) + { + ::TlsSetValue(tss_key_, value ? this : 0); + } + +private: + // Thread-specific storage to allow unlocked access to determine whether a + // thread is a member of the pool. + mutable unsigned long tss_key_; +}; + +} // namespace detail +} // namespace asio + +#endif // defined(_WIN32) + +#include "asio/detail/pop_options.hpp" + +#endif // ASIO_DETAIL_WIN_TSS_BOOL_HPP