posix_tss_ptr.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // posix_tss_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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. #ifndef ASIO_DETAIL_POSIX_TSS_PTR_HPP
  11. #define ASIO_DETAIL_POSIX_TSS_PTR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include "asio/detail/pop_options.hpp"
  19. #if defined(BOOST_HAS_PTHREADS)
  20. #include "asio/detail/push_options.hpp"
  21. #include <boost/throw_exception.hpp>
  22. #include <pthread.h>
  23. #include "asio/detail/pop_options.hpp"
  24. #include "asio/error.hpp"
  25. #include "asio/system_error.hpp"
  26. #include "asio/detail/noncopyable.hpp"
  27. namespace asio {
  28. namespace detail {
  29. template <typename T>
  30. class posix_tss_ptr
  31. : private noncopyable
  32. {
  33. public:
  34. // Constructor.
  35. posix_tss_ptr()
  36. {
  37. int error = ::pthread_key_create(&tss_key_, 0);
  38. if (error != 0)
  39. {
  40. asio::system_error e(
  41. asio::error_code(error,
  42. asio::error::get_system_category()),
  43. "tss");
  44. boost::throw_exception(e);
  45. }
  46. }
  47. // Destructor.
  48. ~posix_tss_ptr()
  49. {
  50. ::pthread_key_delete(tss_key_);
  51. }
  52. // Get the value.
  53. operator T*() const
  54. {
  55. return static_cast<T*>(::pthread_getspecific(tss_key_));
  56. }
  57. // Set the value.
  58. void operator=(T* value)
  59. {
  60. ::pthread_setspecific(tss_key_, value);
  61. }
  62. private:
  63. // Thread-specific storage to allow unlocked access to determine whether a
  64. // thread is a member of the pool.
  65. pthread_key_t tss_key_;
  66. };
  67. } // namespace detail
  68. } // namespace asio
  69. #endif // defined(BOOST_HAS_PTHREADS)
  70. #include "asio/detail/pop_options.hpp"
  71. #endif // ASIO_DETAIL_POSIX_TSS_PTR_HPP