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 
16 #ifndef BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
18 
19 #include <boost/static_assert.hpp>
20 #include <boost/type_traits/is_pod.hpp>
21 #include <boost/log/detail/config.hpp>
22 
23 #ifdef BOOST_HAS_PRAGMA_ONCE
24 #pragma once
25 #endif
26 
27 #if !defined(BOOST_LOG_NO_THREADS)
28 
29 #include <boost/log/detail/header.hpp>
30 
31 namespace boost {
32 
33 BOOST_LOG_OPEN_NAMESPACE
34 
35 namespace aux {
36 
37 //! Base class for TLS to hide platform-specific storage management
38 class thread_specific_base
39 {
40 private:
41 #if defined(BOOST_THREAD_PLATFORM_WIN32)
42     typedef unsigned long key_storage;
43 #else
44     typedef void* key_storage;
45 #endif
46 
47     key_storage m_Key;
48 
49 protected:
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;
54 
55     //  Copying prohibited
56     BOOST_DELETED_FUNCTION(thread_specific_base(thread_specific_base const&))
57     BOOST_DELETED_FUNCTION(thread_specific_base& operator= (thread_specific_base const&))
58 };
59 
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
64 {
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");
66 
67     //! Union to perform type casting
68     union value_storage
69     {
70         void* as_pointer;
71         T as_value;
72     };
73 
74 public:
75     //! Default constructor
thread_specific()76     BOOST_DEFAULTED_FUNCTION(thread_specific(), {})
77     //! Initializing constructor
78     thread_specific(T const& value)
79     {
80         set(value);
81     }
82     //! Assignment
operator =(T const & value)83     thread_specific& operator= (T const& value)
84     {
85         set(value);
86         return *this;
87     }
88 
89     //! Accessor
get() const90     T get() const
91     {
92         value_storage cast = {};
93         cast.as_pointer = thread_specific_base::get_content();
94         return cast.as_value;
95     }
96 
97     //! Setter
set(T const & value)98     void set(T const& value)
99     {
100         value_storage cast = {};
101         cast.as_value = value;
102         thread_specific_base::set_content(cast.as_pointer);
103     }
104 };
105 
106 } // namespace aux
107 
108 BOOST_LOG_CLOSE_NAMESPACE // namespace log
109 
110 } // namespace boost
111 
112 #include <boost/log/detail/footer.hpp>
113 
114 #endif // !defined(BOOST_LOG_NO_THREADS)
115 
116 #endif // BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
117