threading_models.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 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. #ifndef BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_
  17. #define BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_
  18. #include <boost/type_traits/has_nothrow_constructor.hpp>
  19. #include <boost/log/detail/config.hpp>
  20. #include <boost/log/detail/locks.hpp> // is_mutex_type
  21. #if !defined(BOOST_LOG_NO_THREADS)
  22. #include <boost/mpl/bool.hpp>
  23. #endif
  24. #include <boost/log/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. BOOST_LOG_OPEN_NAMESPACE
  30. namespace sources {
  31. //! Single thread locking model
  32. struct single_thread_model
  33. {
  34. // We provide methods for the most advanced locking concept: UpgradeLockable
  35. void lock_shared() const BOOST_NOEXCEPT {}
  36. bool try_lock_shared() const BOOST_NOEXCEPT { return true; }
  37. template< typename TimeT >
  38. bool timed_lock_shared(TimeT const&) const BOOST_NOEXCEPT { return true; }
  39. void unlock_shared() const BOOST_NOEXCEPT {}
  40. void lock() const BOOST_NOEXCEPT {}
  41. bool try_lock() const BOOST_NOEXCEPT { return true; }
  42. template< typename TimeT >
  43. bool timed_lock(TimeT const&) const BOOST_NOEXCEPT { return true; }
  44. void unlock() const BOOST_NOEXCEPT {}
  45. void lock_upgrade() const BOOST_NOEXCEPT {}
  46. bool try_lock_upgrade() const BOOST_NOEXCEPT { return true; }
  47. template< typename TimeT >
  48. bool timed_lock_upgrade(TimeT const&) const BOOST_NOEXCEPT { return true; }
  49. void unlock_upgrade() const BOOST_NOEXCEPT {}
  50. void unlock_upgrade_and_lock() const BOOST_NOEXCEPT {}
  51. void unlock_and_lock_upgrade() const BOOST_NOEXCEPT {}
  52. void unlock_and_lock_shared() const BOOST_NOEXCEPT {}
  53. void unlock_upgrade_and_lock_shared() const BOOST_NOEXCEPT {}
  54. void swap(single_thread_model&) BOOST_NOEXCEPT {}
  55. };
  56. inline void swap(single_thread_model&, single_thread_model&) BOOST_NOEXCEPT
  57. {
  58. }
  59. #if !defined(BOOST_LOG_NO_THREADS)
  60. //! Multi-thread locking model with maximum locking capabilities
  61. template< typename MutexT >
  62. struct multi_thread_model
  63. {
  64. multi_thread_model() BOOST_NOEXCEPT_IF(boost::has_nothrow_constructor< MutexT >::value) {}
  65. multi_thread_model(multi_thread_model const&) BOOST_NOEXCEPT_IF(boost::has_nothrow_constructor< MutexT >::value) {}
  66. multi_thread_model& operator= (multi_thread_model const&) BOOST_NOEXCEPT { return *this; }
  67. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  68. multi_thread_model(multi_thread_model&&) BOOST_NOEXCEPT_IF(boost::has_nothrow_constructor< MutexT >::value) {}
  69. multi_thread_model& operator= (multi_thread_model&&) BOOST_NOEXCEPT { return *this; }
  70. #endif
  71. void lock_shared() const { m_Mutex.lock_shared(); }
  72. bool try_lock_shared() const { return m_Mutex.try_lock_shared(); }
  73. template< typename TimeT >
  74. bool timed_lock_shared(TimeT const& t) const { return m_Mutex.timed_lock_shared(t); }
  75. void unlock_shared() const BOOST_NOEXCEPT { m_Mutex.unlock_shared(); }
  76. void lock() const { m_Mutex.lock(); }
  77. bool try_lock() const { return m_Mutex.try_lock(); }
  78. template< typename TimeT >
  79. bool timed_lock(TimeT const& t) const { return m_Mutex.timed_lock(t); }
  80. void unlock() const BOOST_NOEXCEPT { m_Mutex.unlock(); }
  81. void lock_upgrade() const { m_Mutex.lock_upgrade(); }
  82. bool try_lock_upgrade() const { return m_Mutex.try_lock_upgrade(); }
  83. template< typename TimeT >
  84. bool timed_lock_upgrade(TimeT const& t) const { return m_Mutex.timed_lock_upgrade(t); }
  85. void unlock_upgrade() const BOOST_NOEXCEPT { m_Mutex.unlock_upgrade(); }
  86. void unlock_upgrade_and_lock() const { m_Mutex.unlock_upgrade_and_lock(); }
  87. void unlock_and_lock_upgrade() const { m_Mutex.unlock_and_lock_upgrade(); }
  88. void unlock_and_lock_shared() const { m_Mutex.unlock_and_lock_shared(); }
  89. void unlock_upgrade_and_lock_shared() const { m_Mutex.unlock_upgrade_and_lock_shared(); }
  90. void swap(multi_thread_model&) BOOST_NOEXCEPT {}
  91. private:
  92. //! Synchronization primitive
  93. mutable MutexT m_Mutex;
  94. };
  95. template< typename MutexT >
  96. inline void swap(multi_thread_model< MutexT >&, multi_thread_model< MutexT >&) BOOST_NOEXCEPT
  97. {
  98. }
  99. #endif // !defined(BOOST_LOG_NO_THREADS)
  100. } // namespace sources
  101. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  102. #if !defined(BOOST_LOG_NO_THREADS) && !defined(BOOST_LOG_DOXYGEN_PASS)
  103. template< >
  104. struct is_mutex_type< boost::log::sources::single_thread_model > : mpl::true_
  105. {
  106. };
  107. template< typename T >
  108. struct is_mutex_type< boost::log::sources::multi_thread_model< T > > : mpl::true_
  109. {
  110. };
  111. #endif // !defined(BOOST_LOG_NO_THREADS)
  112. } // namespace boost
  113. #include <boost/log/detail/footer.hpp>
  114. #endif // BOOST_LOG_SOURCES_THREADING_MODELS_HPP_INCLUDED_