Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:22

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   thread_specific.hpp
0009  * \author Andrey Semashev
0010  * \date   01.03.2008
0011  *
0012  * \brief  This header is the Boost.Log library implementation, see the library documentation
0013  *         at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
0014  */
0015 
0016 #ifndef BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
0017 #define BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_
0018 
0019 #include <boost/type_traits/is_pod.hpp>
0020 #include <boost/log/detail/config.hpp>
0021 
0022 #ifdef BOOST_HAS_PRAGMA_ONCE
0023 #pragma once
0024 #endif
0025 
0026 #if !defined(BOOST_LOG_NO_THREADS)
0027 
0028 #include <boost/log/detail/header.hpp>
0029 
0030 namespace boost {
0031 
0032 BOOST_LOG_OPEN_NAMESPACE
0033 
0034 namespace aux {
0035 
0036 //! Base class for TLS to hide platform-specific storage management
0037 class thread_specific_base
0038 {
0039 private:
0040 #if defined(BOOST_THREAD_PLATFORM_WIN32)
0041     typedef unsigned long key_storage;
0042 #else
0043     typedef void* key_storage;
0044 #endif
0045 
0046     key_storage m_Key;
0047 
0048 protected:
0049     BOOST_LOG_API thread_specific_base();
0050     BOOST_LOG_API ~thread_specific_base();
0051     BOOST_LOG_API void* get_content() const;
0052     BOOST_LOG_API void set_content(void* value) const;
0053 
0054     //  Copying prohibited
0055     BOOST_DELETED_FUNCTION(thread_specific_base(thread_specific_base const&))
0056     BOOST_DELETED_FUNCTION(thread_specific_base& operator= (thread_specific_base const&))
0057 };
0058 
0059 //! A TLS wrapper for small POD types with least possible overhead
0060 template< typename T >
0061 class thread_specific :
0062     public thread_specific_base
0063 {
0064     static_assert(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");
0065 
0066     //! Union to perform type casting
0067     union value_storage
0068     {
0069         void* as_pointer;
0070         T as_value;
0071     };
0072 
0073 public:
0074     //! Default constructor
0075     BOOST_DEFAULTED_FUNCTION(thread_specific(), {})
0076     //! Initializing constructor
0077     thread_specific(T const& value)
0078     {
0079         set(value);
0080     }
0081     //! Assignment
0082     thread_specific& operator= (T const& value)
0083     {
0084         set(value);
0085         return *this;
0086     }
0087 
0088     //! Accessor
0089     T get() const
0090     {
0091         value_storage cast = {};
0092         cast.as_pointer = thread_specific_base::get_content();
0093         return cast.as_value;
0094     }
0095 
0096     //! Setter
0097     void set(T const& value)
0098     {
0099         value_storage cast = {};
0100         cast.as_value = value;
0101         thread_specific_base::set_content(cast.as_pointer);
0102     }
0103 };
0104 
0105 } // namespace aux
0106 
0107 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0108 
0109 } // namespace boost
0110 
0111 #include <boost/log/detail/footer.hpp>
0112 
0113 #endif // !defined(BOOST_LOG_NO_THREADS)
0114 
0115 #endif // BOOST_LOG_DETAIL_THREAD_SPECIFIC_HPP_INCLUDED_