win_tss_ptr.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // win_tss_ptr.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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 BOOST_ASIO_DETAIL_WIN_TSS_PTR_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_TSS_PTR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/system/system_error.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #if defined(BOOST_WINDOWS)
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/noncopyable.hpp>
  23. #include <boost/asio/detail/socket_types.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. #include <boost/throw_exception.hpp>
  26. #include <boost/asio/detail/pop_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. template <typename T>
  31. class win_tss_ptr
  32. : private noncopyable
  33. {
  34. public:
  35. #if defined(UNDER_CE)
  36. enum { out_of_indexes = 0xFFFFFFFF };
  37. #else
  38. enum { out_of_indexes = TLS_OUT_OF_INDEXES };
  39. #endif
  40. // Constructor.
  41. win_tss_ptr()
  42. {
  43. tss_key_ = ::TlsAlloc();
  44. if (tss_key_ == out_of_indexes)
  45. {
  46. DWORD last_error = ::GetLastError();
  47. boost::system::system_error e(
  48. boost::system::error_code(last_error,
  49. boost::asio::error::get_system_category()),
  50. "tss");
  51. boost::throw_exception(e);
  52. }
  53. }
  54. // Destructor.
  55. ~win_tss_ptr()
  56. {
  57. ::TlsFree(tss_key_);
  58. }
  59. // Get the value.
  60. operator T*() const
  61. {
  62. return static_cast<T*>(::TlsGetValue(tss_key_));
  63. }
  64. // Set the value.
  65. void operator=(T* value)
  66. {
  67. ::TlsSetValue(tss_key_, value);
  68. }
  69. private:
  70. // Thread-specific storage to allow unlocked access to determine whether a
  71. // thread is a member of the pool.
  72. DWORD tss_key_;
  73. };
  74. } // namespace detail
  75. } // namespace asio
  76. } // namespace boost
  77. #endif // defined(BOOST_WINDOWS)
  78. #include <boost/asio/detail/pop_options.hpp>
  79. #endif // BOOST_ASIO_DETAIL_WIN_TSS_PTR_HPP