light_rw_mutex.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 light_rw_mutex.hpp
  9. * \author Andrey Semashev
  10. * \date 24.03.2009
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_
  17. #include <boost/log/detail/config.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. #ifndef BOOST_LOG_NO_THREADS
  22. #include <boost/log/detail/header.hpp>
  23. #if defined(BOOST_THREAD_POSIX) // This one can be defined by users, so it should go first
  24. #define BOOST_LOG_LWRWMUTEX_USE_PTHREAD
  25. #elif defined(BOOST_WINDOWS) && (BOOST_USE_WINAPI_VERSION+0) >= (BOOST_WINAPI_VERSION_WIN6+0)
  26. #define BOOST_LOG_LWRWMUTEX_USE_SRWLOCK
  27. #elif defined(BOOST_HAS_PTHREADS)
  28. #define BOOST_LOG_LWRWMUTEX_USE_PTHREAD
  29. #endif
  30. #if defined(BOOST_LOG_LWRWMUTEX_USE_SRWLOCK)
  31. #include <boost/winapi/srw_lock.hpp>
  32. namespace boost {
  33. BOOST_LOG_OPEN_NAMESPACE
  34. namespace aux {
  35. //! A light read/write mutex that uses WinNT 6 and later APIs
  36. class light_rw_mutex
  37. {
  38. boost::winapi::SRWLOCK_ m_Mutex;
  39. public:
  40. light_rw_mutex()
  41. {
  42. boost::winapi::InitializeSRWLock(&m_Mutex);
  43. }
  44. void lock_shared()
  45. {
  46. boost::winapi::AcquireSRWLockShared(&m_Mutex);
  47. }
  48. void unlock_shared()
  49. {
  50. boost::winapi::ReleaseSRWLockShared(&m_Mutex);
  51. }
  52. void lock()
  53. {
  54. boost::winapi::AcquireSRWLockExclusive(&m_Mutex);
  55. }
  56. void unlock()
  57. {
  58. boost::winapi::ReleaseSRWLockExclusive(&m_Mutex);
  59. }
  60. // Noncopyable
  61. BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
  62. BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
  63. };
  64. } // namespace aux
  65. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  66. } // namespace boost
  67. #elif defined(BOOST_LOG_LWRWMUTEX_USE_PTHREAD)
  68. #include <pthread.h>
  69. namespace boost {
  70. BOOST_LOG_OPEN_NAMESPACE
  71. namespace aux {
  72. //! A light read/write mutex that maps directly onto POSIX threading library
  73. class light_rw_mutex
  74. {
  75. pthread_rwlock_t m_Mutex;
  76. public:
  77. light_rw_mutex()
  78. {
  79. pthread_rwlock_init(&m_Mutex, NULL);
  80. }
  81. ~light_rw_mutex()
  82. {
  83. pthread_rwlock_destroy(&m_Mutex);
  84. }
  85. void lock_shared()
  86. {
  87. pthread_rwlock_rdlock(&m_Mutex);
  88. }
  89. void unlock_shared()
  90. {
  91. pthread_rwlock_unlock(&m_Mutex);
  92. }
  93. void lock()
  94. {
  95. pthread_rwlock_wrlock(&m_Mutex);
  96. }
  97. void unlock()
  98. {
  99. pthread_rwlock_unlock(&m_Mutex);
  100. }
  101. // Noncopyable
  102. BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
  103. BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
  104. };
  105. } // namespace aux
  106. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  107. } // namespace boost
  108. #else
  109. namespace boost {
  110. BOOST_LOG_OPEN_NAMESPACE
  111. namespace aux {
  112. //! A light read/write mutex
  113. class light_rw_mutex
  114. {
  115. struct BOOST_LOG_MAY_ALIAS mutex_state { void* p; } m_Mutex;
  116. public:
  117. BOOST_LOG_API light_rw_mutex();
  118. BOOST_LOG_API ~light_rw_mutex();
  119. BOOST_LOG_API void lock_shared();
  120. BOOST_LOG_API void unlock_shared();
  121. BOOST_LOG_API void lock();
  122. BOOST_LOG_API void unlock();
  123. // Noncopyable
  124. BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
  125. BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
  126. };
  127. } // namespace aux
  128. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  129. } // namespace boost
  130. #endif
  131. #include <boost/log/detail/footer.hpp>
  132. #endif // BOOST_LOG_NO_THREADS
  133. #endif // BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_