light_rw_mutex.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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) && defined(BOOST_LOG_USE_WINNT6_API)
  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. #if defined(BOOST_USE_WINDOWS_H)
  32. #ifndef _WIN32_WINNT
  33. #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_LONGHORN
  34. #endif
  35. #include <windows.h>
  36. #else // defined(BOOST_USE_WINDOWS_H)
  37. namespace boost {
  38. BOOST_LOG_OPEN_NAMESPACE
  39. namespace aux {
  40. extern "C" {
  41. struct SRWLOCK { void* p; };
  42. __declspec(dllimport) void __stdcall InitializeSRWLock(SRWLOCK*);
  43. __declspec(dllimport) void __stdcall ReleaseSRWLockExclusive(SRWLOCK*);
  44. __declspec(dllimport) void __stdcall ReleaseSRWLockShared(SRWLOCK*);
  45. __declspec(dllimport) void __stdcall AcquireSRWLockExclusive(SRWLOCK*);
  46. __declspec(dllimport) void __stdcall AcquireSRWLockShared(SRWLOCK*);
  47. } // extern "C"
  48. } // namespace aux
  49. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  50. } // namespace boost
  51. #endif // BOOST_USE_WINDOWS_H
  52. namespace boost {
  53. BOOST_LOG_OPEN_NAMESPACE
  54. namespace aux {
  55. //! A light read/write mutex that uses WinNT 6 and later APIs
  56. class light_rw_mutex
  57. {
  58. SRWLOCK m_Mutex;
  59. public:
  60. light_rw_mutex()
  61. {
  62. InitializeSRWLock(&m_Mutex);
  63. }
  64. void lock_shared()
  65. {
  66. AcquireSRWLockShared(&m_Mutex);
  67. }
  68. void unlock_shared()
  69. {
  70. ReleaseSRWLockShared(&m_Mutex);
  71. }
  72. void lock()
  73. {
  74. AcquireSRWLockExclusive(&m_Mutex);
  75. }
  76. void unlock()
  77. {
  78. ReleaseSRWLockExclusive(&m_Mutex);
  79. }
  80. // Noncopyable
  81. BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
  82. BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
  83. };
  84. } // namespace aux
  85. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  86. } // namespace boost
  87. #elif defined(BOOST_LOG_LWRWMUTEX_USE_PTHREAD)
  88. #include <pthread.h>
  89. namespace boost {
  90. BOOST_LOG_OPEN_NAMESPACE
  91. namespace aux {
  92. //! A light read/write mutex that maps directly onto POSIX threading library
  93. class light_rw_mutex
  94. {
  95. pthread_rwlock_t m_Mutex;
  96. public:
  97. light_rw_mutex()
  98. {
  99. pthread_rwlock_init(&m_Mutex, NULL);
  100. }
  101. ~light_rw_mutex()
  102. {
  103. pthread_rwlock_destroy(&m_Mutex);
  104. }
  105. void lock_shared()
  106. {
  107. pthread_rwlock_rdlock(&m_Mutex);
  108. }
  109. void unlock_shared()
  110. {
  111. pthread_rwlock_unlock(&m_Mutex);
  112. }
  113. void lock()
  114. {
  115. pthread_rwlock_wrlock(&m_Mutex);
  116. }
  117. void unlock()
  118. {
  119. pthread_rwlock_unlock(&m_Mutex);
  120. }
  121. // Noncopyable
  122. BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
  123. BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
  124. };
  125. } // namespace aux
  126. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  127. } // namespace boost
  128. #else
  129. namespace boost {
  130. BOOST_LOG_OPEN_NAMESPACE
  131. namespace aux {
  132. //! A light read/write mutex
  133. class light_rw_mutex
  134. {
  135. struct { void* p; } m_Mutex;
  136. public:
  137. BOOST_LOG_API light_rw_mutex();
  138. BOOST_LOG_API ~light_rw_mutex();
  139. BOOST_LOG_API void lock_shared();
  140. BOOST_LOG_API void unlock_shared();
  141. BOOST_LOG_API void lock();
  142. BOOST_LOG_API void unlock();
  143. // Noncopyable
  144. BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
  145. BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
  146. };
  147. } // namespace aux
  148. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  149. } // namespace boost
  150. #endif
  151. #include <boost/log/detail/footer.hpp>
  152. #endif // BOOST_LOG_NO_THREADS
  153. #endif // BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_