win_tss_ptr.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // detail/win_tss_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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/config.hpp>
  16. #if defined(BOOST_ASIO_WINDOWS)
  17. #include <boost/asio/detail/noncopyable.hpp>
  18. #include <boost/asio/detail/socket_types.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. // Helper function to create thread-specific storage.
  24. BOOST_ASIO_DECL DWORD win_tss_ptr_create();
  25. template <typename T>
  26. class win_tss_ptr
  27. : private noncopyable
  28. {
  29. public:
  30. // Constructor.
  31. win_tss_ptr()
  32. : tss_key_(win_tss_ptr_create())
  33. {
  34. }
  35. // Destructor.
  36. ~win_tss_ptr()
  37. {
  38. ::TlsFree(tss_key_);
  39. }
  40. // Get the value.
  41. operator T*() const
  42. {
  43. return static_cast<T*>(::TlsGetValue(tss_key_));
  44. }
  45. // Set the value.
  46. void operator=(T* value)
  47. {
  48. ::TlsSetValue(tss_key_, value);
  49. }
  50. private:
  51. // Thread-specific storage to allow unlocked access to determine whether a
  52. // thread is a member of the pool.
  53. DWORD tss_key_;
  54. };
  55. } // namespace detail
  56. } // namespace asio
  57. } // namespace boost
  58. #include <boost/asio/detail/pop_options.hpp>
  59. #if defined(BOOST_ASIO_HEADER_ONLY)
  60. # include <boost/asio/detail/impl/win_tss_ptr.ipp>
  61. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  62. #endif // defined(BOOST_ASIO_WINDOWS)
  63. #endif // BOOST_ASIO_DETAIL_WIN_TSS_PTR_HPP