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 16 #ifndef BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_ 17 #define BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_ 18 19 #include <boost/log/detail/config.hpp> 20 21 #ifdef BOOST_HAS_PRAGMA_ONCE 22 #pragma once 23 #endif 24 25 #ifndef BOOST_LOG_NO_THREADS 26 27 #include <boost/log/detail/header.hpp> 28 29 #if defined(BOOST_THREAD_POSIX) // This one can be defined by users, so it should go first 30 #define BOOST_LOG_LWRWMUTEX_USE_PTHREAD 31 #elif defined(BOOST_WINDOWS) && (BOOST_USE_WINAPI_VERSION+0) >= (BOOST_WINAPI_VERSION_WIN6+0) 32 #define BOOST_LOG_LWRWMUTEX_USE_SRWLOCK 33 #elif defined(BOOST_HAS_PTHREADS) 34 #define BOOST_LOG_LWRWMUTEX_USE_PTHREAD 35 #endif 36 37 #if defined(BOOST_LOG_LWRWMUTEX_USE_SRWLOCK) 38 39 #include <boost/winapi/srw_lock.hpp> 40 41 namespace boost { 42 43 BOOST_LOG_OPEN_NAMESPACE 44 45 namespace aux { 46 47 //! A light read/write mutex that uses WinNT 6 and later APIs 48 class light_rw_mutex 49 { 50 boost::winapi::SRWLOCK_ m_Mutex; 51 52 public: light_rw_mutex()53 light_rw_mutex() 54 { 55 boost::winapi::InitializeSRWLock(&m_Mutex); 56 } lock_shared()57 void lock_shared() 58 { 59 boost::winapi::AcquireSRWLockShared(&m_Mutex); 60 } unlock_shared()61 void unlock_shared() 62 { 63 boost::winapi::ReleaseSRWLockShared(&m_Mutex); 64 } lock()65 void lock() 66 { 67 boost::winapi::AcquireSRWLockExclusive(&m_Mutex); 68 } unlock()69 void unlock() 70 { 71 boost::winapi::ReleaseSRWLockExclusive(&m_Mutex); 72 } 73 74 // Noncopyable 75 BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&)) 76 BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&)) 77 }; 78 79 } // namespace aux 80 81 BOOST_LOG_CLOSE_NAMESPACE // namespace log 82 83 } // namespace boost 84 85 #elif defined(BOOST_LOG_LWRWMUTEX_USE_PTHREAD) 86 87 #include <pthread.h> 88 89 namespace boost { 90 91 BOOST_LOG_OPEN_NAMESPACE 92 93 namespace aux { 94 95 //! A light read/write mutex that maps directly onto POSIX threading library 96 class light_rw_mutex 97 { 98 pthread_rwlock_t m_Mutex; 99 100 public: light_rw_mutex()101 light_rw_mutex() 102 { 103 pthread_rwlock_init(&m_Mutex, NULL); 104 } ~light_rw_mutex()105 ~light_rw_mutex() 106 { 107 pthread_rwlock_destroy(&m_Mutex); 108 } lock_shared()109 void lock_shared() 110 { 111 pthread_rwlock_rdlock(&m_Mutex); 112 } unlock_shared()113 void unlock_shared() 114 { 115 pthread_rwlock_unlock(&m_Mutex); 116 } lock()117 void lock() 118 { 119 pthread_rwlock_wrlock(&m_Mutex); 120 } unlock()121 void unlock() 122 { 123 pthread_rwlock_unlock(&m_Mutex); 124 } 125 126 // Noncopyable 127 BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&)) 128 BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&)) 129 }; 130 131 } // namespace aux 132 133 BOOST_LOG_CLOSE_NAMESPACE // namespace log 134 135 } // namespace boost 136 137 #else 138 139 namespace boost { 140 141 BOOST_LOG_OPEN_NAMESPACE 142 143 namespace aux { 144 145 //! A light read/write mutex 146 class light_rw_mutex 147 { 148 struct BOOST_LOG_MAY_ALIAS mutex_state { void* p; } m_Mutex; 149 150 public: 151 BOOST_LOG_API light_rw_mutex(); 152 BOOST_LOG_API ~light_rw_mutex(); 153 BOOST_LOG_API void lock_shared(); 154 BOOST_LOG_API void unlock_shared(); 155 BOOST_LOG_API void lock(); 156 BOOST_LOG_API void unlock(); 157 158 // Noncopyable 159 BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&)) 160 BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&)) 161 }; 162 163 } // namespace aux 164 165 BOOST_LOG_CLOSE_NAMESPACE // namespace log 166 167 } // namespace boost 168 169 #endif 170 171 #include <boost/log/detail/footer.hpp> 172 173 #endif // BOOST_LOG_NO_THREADS 174 175 #endif // BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_ 176