thread_specific.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file thread_specific.hpp
  9. * \author Andrey Semashev
  10. * \date 01.03.2008
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
  17. #include <boost/static_assert.hpp>
  18. #include <boost/type_traits/is_pod.hpp>
  19. #include <boost/log/detail/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. #if !defined(BOOST_LOG_NO_THREADS)
  24. #include <boost/log/detail/header.hpp>
  25. namespace boost {
  26. BOOST_LOG_OPEN_NAMESPACE
  27. namespace aux {
  28. //! Base class for TLS to hide platform-specific storage management
  29. class thread_specific_base
  30. {
  31. private:
  32. #if defined(BOOST_THREAD_PLATFORM_WIN32)
  33. typedef unsigned long key_storage;
  34. #else
  35. typedef void* key_storage;
  36. #endif
  37. key_storage m_Key;
  38. protected:
  39. BOOST_LOG_API thread_specific_base();
  40. BOOST_LOG_API ~thread_specific_base();
  41. BOOST_LOG_API void* get_content() const;
  42. BOOST_LOG_API void set_content(void* value) const;
  43. // Copying prohibited
  44. BOOST_DELETED_FUNCTION(thread_specific_base(thread_specific_base const&))
  45. BOOST_DELETED_FUNCTION(thread_specific_base& operator= (thread_specific_base const&))
  46. };
  47. //! A TLS wrapper for small POD types with least possible overhead
  48. template< typename T >
  49. class thread_specific :
  50. public thread_specific_base
  51. {
  52. BOOST_STATIC_ASSERT_MSG(sizeof(T) <= sizeof(void*) && is_pod< T >::value, "Boost.Log: Thread-specific values must be PODs and must not exceed the size of a pointer");
  53. //! Union to perform type casting
  54. union value_storage
  55. {
  56. void* as_pointer;
  57. T as_value;
  58. };
  59. public:
  60. //! Default constructor
  61. BOOST_DEFAULTED_FUNCTION(thread_specific(), {})
  62. //! Initializing constructor
  63. thread_specific(T const& value)
  64. {
  65. set(value);
  66. }
  67. //! Assignment
  68. thread_specific& operator= (T const& value)
  69. {
  70. set(value);
  71. return *this;
  72. }
  73. //! Accessor
  74. T get() const
  75. {
  76. value_storage cast = {};
  77. cast.as_pointer = thread_specific_base::get_content();
  78. return cast.as_value;
  79. }
  80. //! Setter
  81. void set(T const& value)
  82. {
  83. value_storage cast = {};
  84. cast.as_value = value;
  85. thread_specific_base::set_content(cast.as_pointer);
  86. }
  87. };
  88. } // namespace aux
  89. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  90. } // namespace boost
  91. #include <boost/log/detail/footer.hpp>
  92. #endif // !defined(BOOST_LOG_NO_THREADS)
  93. #endif // BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_