Home | History | Annotate | Download | only in detail
      1 //
      2 // detail/posix_tss_ptr.hpp
      3 // ~~~~~~~~~~~~~~~~~~~~~~~~
      4 //
      5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
      6 //
      7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
      8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      9 //
     10 
     11 #ifndef ASIO_DETAIL_POSIX_TSS_PTR_HPP
     12 #define ASIO_DETAIL_POSIX_TSS_PTR_HPP
     13 
     14 
     15 #include "asio/detail/config.hpp"
     16 
     17 #if defined(ASIO_HAS_PTHREADS)
     18 
     19 #include <pthread.h>
     20 #include "asio/detail/noncopyable.hpp"
     21 
     22 #include "asio/detail/push_options.hpp"
     23 
     24 namespace asio {
     25 namespace detail {
     26 
     27 // Helper function to create thread-specific storage.
     28 ASIO_DECL void posix_tss_ptr_create(pthread_key_t& key);
     29 
     30 template <typename T>
     31 class posix_tss_ptr
     32   : private noncopyable
     33 {
     34 public:
     35   // Constructor.
     36   posix_tss_ptr()
     37   {
     38     posix_tss_ptr_create(tss_key_);
     39   }
     40 
     41   // Destructor.
     42   ~posix_tss_ptr()
     43   {
     44     ::pthread_key_delete(tss_key_);
     45   }
     46 
     47   // Get the value.
     48   operator T*() const
     49   {
     50     return static_cast<T*>(::pthread_getspecific(tss_key_));
     51   }
     52 
     53   // Set the value.
     54   void operator=(T* value)
     55   {
     56     ::pthread_setspecific(tss_key_, value);
     57   }
     58 
     59 private:
     60   // Thread-specific storage to allow unlocked access to determine whether a
     61   // thread is a member of the pool.
     62   pthread_key_t tss_key_;
     63 };
     64 
     65 } // namespace detail
     66 } // namespace asio
     67 
     68 #include "asio/detail/pop_options.hpp"
     69 
     70 # include "asio/detail/impl/posix_tss_ptr.ipp"
     71 
     72 #endif // defined(ASIO_HAS_PTHREADS)
     73 
     74 #endif // ASIO_DETAIL_POSIX_TSS_PTR_HPP
     75