posix_tss_ptr.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // detail/posix_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_POSIX_TSS_PTR_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_PTHREADS)
  17. #include <pthread.h>
  18. #include <boost/asio/detail/noncopyable.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 void posix_tss_ptr_create(pthread_key_t& key);
  25. template <typename T>
  26. class posix_tss_ptr
  27. : private noncopyable
  28. {
  29. public:
  30. // Constructor.
  31. posix_tss_ptr()
  32. {
  33. posix_tss_ptr_create(tss_key_);
  34. }
  35. // Destructor.
  36. ~posix_tss_ptr()
  37. {
  38. ::pthread_key_delete(tss_key_);
  39. }
  40. // Get the value.
  41. operator T*() const
  42. {
  43. return static_cast<T*>(::pthread_getspecific(tss_key_));
  44. }
  45. // Set the value.
  46. void operator=(T* value)
  47. {
  48. ::pthread_setspecific(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. pthread_key_t 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/posix_tss_ptr.ipp>
  61. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  62. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  63. #endif // BOOST_ASIO_DETAIL_POSIX_TSS_PTR_HPP