win_tss_ptr.hpp 2.0 KB

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