posix_tss_ptr.hpp
Go to the documentation of this file.
00001 //
00002 // posix_tss_ptr.hpp
00003 // ~~~~~~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #ifndef ASIO_DETAIL_POSIX_TSS_PTR_HPP
00012 #define ASIO_DETAIL_POSIX_TSS_PTR_HPP
00013 
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017 
00018 #include "asio/detail/push_options.hpp"
00019 
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include "asio/detail/pop_options.hpp"
00023 
00024 #if defined(BOOST_HAS_PTHREADS)
00025 
00026 #include "asio/detail/push_options.hpp"
00027 #include <boost/throw_exception.hpp>
00028 #include <pthread.h>
00029 #include "asio/detail/pop_options.hpp"
00030 
00031 #include "asio/error.hpp"
00032 #include "asio/system_error.hpp"
00033 #include "asio/detail/noncopyable.hpp"
00034 
00035 namespace asio {
00036 namespace detail {
00037 
00038 template <typename T>
00039 class posix_tss_ptr
00040   : private noncopyable
00041 {
00042 public:
00043   // Constructor.
00044   posix_tss_ptr()
00045   {
00046     int error = ::pthread_key_create(&tss_key_, 0);
00047     if (error != 0)
00048     {
00049       asio::system_error e(
00050           asio::error_code(error,
00051             asio::error::get_system_category()),
00052           "tss");
00053       boost::throw_exception(e);
00054     }
00055   }
00056 
00057   // Destructor.
00058   ~posix_tss_ptr()
00059   {
00060     ::pthread_key_delete(tss_key_);
00061   }
00062 
00063   // Get the value.
00064   operator T*() const
00065   {
00066     return static_cast<T*>(::pthread_getspecific(tss_key_));
00067   }
00068 
00069   // Set the value.
00070   void operator=(T* value)
00071   {
00072     ::pthread_setspecific(tss_key_, value);
00073   }
00074 
00075 private:
00076   // Thread-specific storage to allow unlocked access to determine whether a
00077   // thread is a member of the pool.
00078   pthread_key_t tss_key_;
00079 };
00080 
00081 } // namespace detail
00082 } // namespace asio
00083 
00084 #endif // defined(BOOST_HAS_PTHREADS)
00085 
00086 #include "asio/detail/pop_options.hpp"
00087 
00088 #endif // ASIO_DETAIL_POSIX_TSS_PTR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


Castor
Author(s): Carpe Noctem
autogenerated on Fri Nov 8 2013 11:05:39