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)
8 * \file thread_specific.hpp
9 * \author Andrey Semashev
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.
16 #ifndef BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
19 #include <boost/static_assert.hpp>
20 #include <boost/type_traits/is_pod.hpp>
21 #include <boost/log/detail/config.hpp>
23 #ifdef BOOST_HAS_PRAGMA_ONCE
27 #if !defined(BOOST_LOG_NO_THREADS)
29 #include <boost/log/detail/header.hpp>
33 BOOST_LOG_OPEN_NAMESPACE
37 //! Base class for TLS to hide platform-specific storage management
38 class thread_specific_base
41 #if defined(BOOST_THREAD_PLATFORM_WIN32)
42 typedef unsigned long key_storage;
44 typedef void* key_storage;
50 BOOST_LOG_API thread_specific_base();
51 BOOST_LOG_API ~thread_specific_base();
52 BOOST_LOG_API void* get_content() const;
53 BOOST_LOG_API void set_content(void* value) const;
56 BOOST_DELETED_FUNCTION(thread_specific_base(thread_specific_base const&))
57 BOOST_DELETED_FUNCTION(thread_specific_base& operator= (thread_specific_base const&))
60 //! A TLS wrapper for small POD types with least possible overhead
61 template< typename T >
62 class thread_specific :
63 public thread_specific_base
65 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");
67 //! Union to perform type casting
75 //! Default constructor
76 BOOST_DEFAULTED_FUNCTION(thread_specific(), {})
77 //! Initializing constructor
78 thread_specific(T const& value)
83 thread_specific& operator= (T const& value)
92 value_storage cast = {};
93 cast.as_pointer = thread_specific_base::get_content();
98 void set(T const& value)
100 value_storage cast = {};
101 cast.as_value = value;
102 thread_specific_base::set_content(cast.as_pointer);
108 BOOST_LOG_CLOSE_NAMESPACE // namespace log
112 #include <boost/log/detail/footer.hpp>
114 #endif // !defined(BOOST_LOG_NO_THREADS)
116 #endif // BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_