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 sources/threading_models.hpp
9 * \author Andrey Semashev
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.
17 #ifndef BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_
18 #define BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_
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>
25 #include <boost/log/detail/header.hpp>
27 #ifdef BOOST_HAS_PRAGMA_ONCE
33 BOOST_LOG_OPEN_NAMESPACE
37 //! Single thread locking model
38 struct single_thread_model
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 {}
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 {}
61 void swap(single_thread_model&) {}
64 #if !defined(BOOST_LOG_NO_THREADS)
66 //! Multi-thread locking model with maximum locking capabilities
67 template< typename MutexT >
68 struct multi_thread_model
70 multi_thread_model() {}
71 multi_thread_model(multi_thread_model const&) {}
72 multi_thread_model& operator= (multi_thread_model const&) { return *this; }
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(); }
94 void swap(multi_thread_model&) {}
97 //! Synchronization primitive
98 mutable MutexT m_Mutex;
101 #endif // !defined(BOOST_LOG_NO_THREADS)
103 } // namespace sources
105 BOOST_LOG_CLOSE_NAMESPACE // namespace log
107 #if !defined(BOOST_LOG_NO_THREADS) && !defined(BOOST_LOG_DOXYGEN_PASS)
110 struct is_mutex_type< boost::log::sources::single_thread_model > : mpl::true_
114 template< typename T >
115 struct is_mutex_type< boost::log::sources::multi_thread_model< T > > : mpl::true_
119 #endif // !defined(BOOST_LOG_NO_THREADS)
123 #include <boost/log/detail/footer.hpp>
125 #endif // BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_