Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / log / sources / threading_models.hpp
1 /*
2  *          Copyright Andrey Semashev 2007 - 2014.
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   sources/threading_models.hpp
9  * \author Andrey Semashev
10  * \date   04.10.2008
11  *
12  * The header contains definition of threading models that can be used in loggers.
13  * The header also provides a number of tags that can be used to express lock requirements
14  * on a function callee.
15  */
16
17 #ifndef BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_
18 #define BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_
19
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/detail/locks.hpp> // is_mutex_type
22 #if !defined(BOOST_LOG_NO_THREADS)
23 #include <boost/mpl/bool.hpp>
24 #endif
25 #include <boost/log/detail/header.hpp>
26
27 #ifdef BOOST_HAS_PRAGMA_ONCE
28 #pragma once
29 #endif
30
31 namespace boost {
32
33 BOOST_LOG_OPEN_NAMESPACE
34
35 namespace sources {
36
37 //! Single thread locking model
38 struct single_thread_model
39 {
40     // We provide methods for the most advanced locking concept: UpgradeLockable
41     void lock_shared() const {}
42     bool try_lock_shared() const { return true; }
43     template< typename TimeT >
44     bool timed_lock_shared(TimeT const&) const { return true; }
45     void unlock_shared() const {}
46     void lock() const {}
47     bool try_lock() const { return true; }
48     template< typename TimeT >
49     bool timed_lock(TimeT const&) const { return true; }
50     void unlock() const {}
51     void lock_upgrade() const {}
52     bool try_lock_upgrade() const { return true; }
53     template< typename TimeT >
54     bool timed_lock_upgrade(TimeT const&) const { return true; }
55     void unlock_upgrade() const {}
56     void unlock_upgrade_and_lock() const {}
57     void unlock_and_lock_upgrade() const {}
58     void unlock_and_lock_shared() const {}
59     void unlock_upgrade_and_lock_shared() const {}
60
61     void swap(single_thread_model&) {}
62 };
63
64 #if !defined(BOOST_LOG_NO_THREADS)
65
66 //! Multi-thread locking model with maximum locking capabilities
67 template< typename MutexT >
68 struct multi_thread_model
69 {
70     multi_thread_model() {}
71     multi_thread_model(multi_thread_model const&) {}
72     multi_thread_model& operator= (multi_thread_model const&) { return *this; }
73
74     void lock_shared() const { m_Mutex.lock_shared(); }
75     bool try_lock_shared() const { return m_Mutex.try_lock_shared(); }
76     template< typename TimeT >
77     bool timed_lock_shared(TimeT const& t) const { return m_Mutex.timed_lock_shared(t); }
78     void unlock_shared() const { m_Mutex.unlock_shared(); }
79     void lock() const { m_Mutex.lock(); }
80     bool try_lock() const { return m_Mutex.try_lock(); }
81     template< typename TimeT >
82     bool timed_lock(TimeT const& t) const { return m_Mutex.timed_lock(t); }
83     void unlock() const { m_Mutex.unlock(); }
84     void lock_upgrade() const { m_Mutex.lock_upgrade(); }
85     bool try_lock_upgrade() const { return m_Mutex.try_lock_upgrade(); }
86     template< typename TimeT >
87     bool timed_lock_upgrade(TimeT const& t) const { return m_Mutex.timed_lock_upgrade(t); }
88     void unlock_upgrade() const { m_Mutex.unlock_upgrade(); }
89     void unlock_upgrade_and_lock() const { m_Mutex.unlock_upgrade_and_lock(); }
90     void unlock_and_lock_upgrade() const { m_Mutex.unlock_and_lock_upgrade(); }
91     void unlock_and_lock_shared() const { m_Mutex.unlock_and_lock_shared(); }
92     void unlock_upgrade_and_lock_shared() const { m_Mutex.unlock_upgrade_and_lock_shared(); }
93
94     void swap(multi_thread_model&) {}
95
96 private:
97     //! Synchronization primitive
98     mutable MutexT m_Mutex;
99 };
100
101 #endif // !defined(BOOST_LOG_NO_THREADS)
102
103 } // namespace sources
104
105 BOOST_LOG_CLOSE_NAMESPACE // namespace log
106
107 #if !defined(BOOST_LOG_NO_THREADS) && !defined(BOOST_LOG_DOXYGEN_PASS)
108
109 template< >
110 struct is_mutex_type< boost::log::sources::single_thread_model > : mpl::true_
111 {
112 };
113
114 template< typename T >
115 struct is_mutex_type< boost::log::sources::multi_thread_model< T > > : mpl::true_
116 {
117 };
118
119 #endif // !defined(BOOST_LOG_NO_THREADS)
120
121 } // namespace boost
122
123 #include <boost/log/detail/footer.hpp>
124
125 #endif // BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_