From beab239538de1997c844895fd65142fa8ac30adb Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Fri, 1 Nov 2013 19:34:43 +0100 Subject: [PATCH] use thread_specific_ptr in scheduler --- include/boost/fiber/detail/scheduler.hpp | 78 ++---------------------- src/detail/scheduler.cpp | 20 +++--- 2 files changed, 17 insertions(+), 81 deletions(-) diff --git a/include/boost/fiber/detail/scheduler.hpp b/include/boost/fiber/detail/scheduler.hpp index 82b650da..187d0291 100644 --- a/include/boost/fiber/detail/scheduler.hpp +++ b/include/boost/fiber/detail/scheduler.hpp @@ -6,11 +6,8 @@ #ifndef BOOST_FIBERS_DETAIL_SCHEDULER_H #define BOOST_FIBERS_DETAIL_SCHEDULER_H -#if defined(__APPLE__) && defined(BOOST_HAS_PTHREADS) -#include // pthread_key_create, pthread_[gs]etspecific -#endif - #include +#include #include #include @@ -29,83 +26,20 @@ namespace boost { namespace fibers { namespace detail { -// thread_local_ptr was contributed by Nat Goodspeed -#if defined(__APPLE__) && defined(BOOST_HAS_PTHREADS) -template -class thread_local_ptr : private noncopyable -{ -private: - struct dummy - { void nonnull() {} }; - - typedef void ( dummy::*safe_bool)(); - - ::pthread_key_t key_; - -public: - thread_local_ptr() BOOST_NOEXCEPT - { BOOST_ASSERT( ! ::pthread_key_create( & key_, 0) ); } - - T * get() const BOOST_NOEXCEPT - { return static_cast< T * >( ::pthread_getspecific( key_) ); } - - thread_local_ptr & operator=( T * ptr) BOOST_NOEXCEPT - { - ::pthread_setspecific( key_, ptr); - return * this; - } - - T & operator*() const BOOST_NOEXCEPT - { return * get(); } - - T * operator->() const BOOST_NOEXCEPT - { return get(); } - - operator T * () const BOOST_NOEXCEPT - { return get(); } - - operator safe_bool() const BOOST_NOEXCEPT - { return get() ? &dummy::nonnull : 0; } - - bool operator!() const BOOST_NOEXCEPT - { return ! get(); } - - bool operator==( thread_local_ptr const& other) BOOST_NOEXCEPT - { return this->get() == other.get(); } - - bool operator!=( thread_local_ptr const& other) BOOST_NOEXCEPT - { return ! ( * this == other); } -}; - -#endif // __APPLE__ && BOOST_HAS_PTHREADS - class scheduler : private noncopyable { private: -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__DMC__) || \ - (defined(__ICC) && defined(BOOST_WINDOWS)) - static __declspec(thread) algorithm * instance_; -#elif defined(__APPLE__) && defined(BOOST_HAS_PTHREADS) - static detail::thread_local_ptr instance_; -#else - static __thread algorithm * instance_; -#endif + static thread_specific_ptr< algorithm > instance_; public: template< typename F > - static fiber_base::ptr_t extract( F const& f) { - return f.impl_; - } + static fiber_base::ptr_t extract( F const& f) + { return f.impl_; } static algorithm * instance() BOOST_NOEXCEPT - { return instance_; } + { return instance_.get(); } - static algorithm * replace( algorithm * other) BOOST_NOEXCEPT - { - algorithm * old = instance_; - instance_ = other; - return old; - } + static algorithm * replace( algorithm * other) BOOST_NOEXCEPT; }; }}} diff --git a/src/detail/scheduler.cpp b/src/detail/scheduler.cpp index a1359d5a..be0e33b9 100644 --- a/src/detail/scheduler.cpp +++ b/src/detail/scheduler.cpp @@ -15,15 +15,17 @@ namespace boost { namespace fibers { namespace detail { -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__DMC__) || \ - (defined(__ICC) && defined(BOOST_WINDOWS)) -__declspec(thread) algorithm * scheduler::instance_ = 0; -#elif defined(__APPLE__) && defined(BOOST_HAS_PTHREADS) -detail::thread_local_ptr scheduler::instance_; -#else -//algorithm * scheduler::instance_ = 0; -__thread algorithm * scheduler::instance_ = 0; -#endif +static void cleanup_function( algorithm *) {} + +thread_specific_ptr< algorithm > scheduler::instance_( cleanup_function); + +algorithm * +scheduler::replace( algorithm * other) BOOST_NOEXCEPT +{ + algorithm * old = instance_.release(); + instance_.reset( other); + return old; +} }}}