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