threading_models.hpp 4.0 KB

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