Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file light_rw_mutex.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 24.03.2009
|
Chris@16
|
11 *
|
Chris@16
|
12 * \brief This header is the Boost.Log library implementation, see the library documentation
|
Chris@16
|
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/log/detail/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
22 #pragma once
|
Chris@16
|
23 #endif
|
Chris@16
|
24
|
Chris@16
|
25 #ifndef BOOST_LOG_NO_THREADS
|
Chris@16
|
26
|
Chris@16
|
27 #include <boost/log/detail/header.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #if defined(BOOST_THREAD_POSIX) // This one can be defined by users, so it should go first
|
Chris@16
|
30 #define BOOST_LOG_LWRWMUTEX_USE_PTHREAD
|
Chris@16
|
31 #elif defined(BOOST_WINDOWS) && defined(BOOST_LOG_USE_WINNT6_API)
|
Chris@16
|
32 #define BOOST_LOG_LWRWMUTEX_USE_SRWLOCK
|
Chris@16
|
33 #elif defined(BOOST_HAS_PTHREADS)
|
Chris@16
|
34 #define BOOST_LOG_LWRWMUTEX_USE_PTHREAD
|
Chris@16
|
35 #endif
|
Chris@16
|
36
|
Chris@16
|
37 #if defined(BOOST_LOG_LWRWMUTEX_USE_SRWLOCK)
|
Chris@16
|
38
|
Chris@16
|
39 #if defined(BOOST_USE_WINDOWS_H)
|
Chris@16
|
40
|
Chris@16
|
41 #ifndef _WIN32_WINNT
|
Chris@16
|
42 #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_LONGHORN
|
Chris@16
|
43 #endif
|
Chris@16
|
44
|
Chris@16
|
45 #include <windows.h>
|
Chris@16
|
46
|
Chris@16
|
47 #else // defined(BOOST_USE_WINDOWS_H)
|
Chris@16
|
48
|
Chris@16
|
49 namespace boost {
|
Chris@16
|
50
|
Chris@16
|
51 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
52
|
Chris@16
|
53 namespace aux {
|
Chris@16
|
54
|
Chris@16
|
55 extern "C" {
|
Chris@16
|
56
|
Chris@16
|
57 struct SRWLOCK { void* p; };
|
Chris@16
|
58 __declspec(dllimport) void __stdcall InitializeSRWLock(SRWLOCK*);
|
Chris@16
|
59 __declspec(dllimport) void __stdcall ReleaseSRWLockExclusive(SRWLOCK*);
|
Chris@16
|
60 __declspec(dllimport) void __stdcall ReleaseSRWLockShared(SRWLOCK*);
|
Chris@16
|
61 __declspec(dllimport) void __stdcall AcquireSRWLockExclusive(SRWLOCK*);
|
Chris@16
|
62 __declspec(dllimport) void __stdcall AcquireSRWLockShared(SRWLOCK*);
|
Chris@16
|
63
|
Chris@16
|
64 } // extern "C"
|
Chris@16
|
65
|
Chris@16
|
66 } // namespace aux
|
Chris@16
|
67
|
Chris@16
|
68 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
69
|
Chris@16
|
70 } // namespace boost
|
Chris@16
|
71
|
Chris@16
|
72 #endif // BOOST_USE_WINDOWS_H
|
Chris@16
|
73
|
Chris@16
|
74 namespace boost {
|
Chris@16
|
75
|
Chris@16
|
76 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
77
|
Chris@16
|
78 namespace aux {
|
Chris@16
|
79
|
Chris@16
|
80 //! A light read/write mutex that uses WinNT 6 and later APIs
|
Chris@16
|
81 class light_rw_mutex
|
Chris@16
|
82 {
|
Chris@16
|
83 SRWLOCK m_Mutex;
|
Chris@16
|
84
|
Chris@16
|
85 public:
|
Chris@16
|
86 light_rw_mutex()
|
Chris@16
|
87 {
|
Chris@16
|
88 InitializeSRWLock(&m_Mutex);
|
Chris@16
|
89 }
|
Chris@16
|
90 void lock_shared()
|
Chris@16
|
91 {
|
Chris@16
|
92 AcquireSRWLockShared(&m_Mutex);
|
Chris@16
|
93 }
|
Chris@16
|
94 void unlock_shared()
|
Chris@16
|
95 {
|
Chris@16
|
96 ReleaseSRWLockShared(&m_Mutex);
|
Chris@16
|
97 }
|
Chris@16
|
98 void lock()
|
Chris@16
|
99 {
|
Chris@16
|
100 AcquireSRWLockExclusive(&m_Mutex);
|
Chris@16
|
101 }
|
Chris@16
|
102 void unlock()
|
Chris@16
|
103 {
|
Chris@16
|
104 ReleaseSRWLockExclusive(&m_Mutex);
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 // Noncopyable
|
Chris@16
|
108 BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
|
Chris@16
|
109 BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
|
Chris@16
|
110 };
|
Chris@16
|
111
|
Chris@16
|
112 } // namespace aux
|
Chris@16
|
113
|
Chris@16
|
114 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
115
|
Chris@16
|
116 } // namespace boost
|
Chris@16
|
117
|
Chris@16
|
118 #elif defined(BOOST_LOG_LWRWMUTEX_USE_PTHREAD)
|
Chris@16
|
119
|
Chris@16
|
120 #include <pthread.h>
|
Chris@16
|
121
|
Chris@16
|
122 namespace boost {
|
Chris@16
|
123
|
Chris@16
|
124 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
125
|
Chris@16
|
126 namespace aux {
|
Chris@16
|
127
|
Chris@16
|
128 //! A light read/write mutex that maps directly onto POSIX threading library
|
Chris@16
|
129 class light_rw_mutex
|
Chris@16
|
130 {
|
Chris@16
|
131 pthread_rwlock_t m_Mutex;
|
Chris@16
|
132
|
Chris@16
|
133 public:
|
Chris@16
|
134 light_rw_mutex()
|
Chris@16
|
135 {
|
Chris@16
|
136 pthread_rwlock_init(&m_Mutex, NULL);
|
Chris@16
|
137 }
|
Chris@16
|
138 ~light_rw_mutex()
|
Chris@16
|
139 {
|
Chris@16
|
140 pthread_rwlock_destroy(&m_Mutex);
|
Chris@16
|
141 }
|
Chris@16
|
142 void lock_shared()
|
Chris@16
|
143 {
|
Chris@16
|
144 pthread_rwlock_rdlock(&m_Mutex);
|
Chris@16
|
145 }
|
Chris@16
|
146 void unlock_shared()
|
Chris@16
|
147 {
|
Chris@16
|
148 pthread_rwlock_unlock(&m_Mutex);
|
Chris@16
|
149 }
|
Chris@16
|
150 void lock()
|
Chris@16
|
151 {
|
Chris@16
|
152 pthread_rwlock_wrlock(&m_Mutex);
|
Chris@16
|
153 }
|
Chris@16
|
154 void unlock()
|
Chris@16
|
155 {
|
Chris@16
|
156 pthread_rwlock_unlock(&m_Mutex);
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 // Noncopyable
|
Chris@16
|
160 BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
|
Chris@16
|
161 BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
|
Chris@16
|
162 };
|
Chris@16
|
163
|
Chris@16
|
164 } // namespace aux
|
Chris@16
|
165
|
Chris@16
|
166 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
167
|
Chris@16
|
168 } // namespace boost
|
Chris@16
|
169
|
Chris@16
|
170 #else
|
Chris@16
|
171
|
Chris@16
|
172 namespace boost {
|
Chris@16
|
173
|
Chris@16
|
174 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
175
|
Chris@16
|
176 namespace aux {
|
Chris@16
|
177
|
Chris@16
|
178 //! A light read/write mutex
|
Chris@16
|
179 class light_rw_mutex
|
Chris@16
|
180 {
|
Chris@16
|
181 struct { void* p; } m_Mutex;
|
Chris@16
|
182
|
Chris@16
|
183 public:
|
Chris@16
|
184 BOOST_LOG_API light_rw_mutex();
|
Chris@16
|
185 BOOST_LOG_API ~light_rw_mutex();
|
Chris@16
|
186 BOOST_LOG_API void lock_shared();
|
Chris@16
|
187 BOOST_LOG_API void unlock_shared();
|
Chris@16
|
188 BOOST_LOG_API void lock();
|
Chris@16
|
189 BOOST_LOG_API void unlock();
|
Chris@16
|
190
|
Chris@16
|
191 // Noncopyable
|
Chris@16
|
192 BOOST_DELETED_FUNCTION(light_rw_mutex(light_rw_mutex const&))
|
Chris@16
|
193 BOOST_DELETED_FUNCTION(light_rw_mutex& operator= (light_rw_mutex const&))
|
Chris@16
|
194 };
|
Chris@16
|
195
|
Chris@16
|
196 } // namespace aux
|
Chris@16
|
197
|
Chris@16
|
198 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
199
|
Chris@16
|
200 } // namespace boost
|
Chris@16
|
201
|
Chris@16
|
202 #endif
|
Chris@16
|
203
|
Chris@16
|
204 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
205
|
Chris@16
|
206 #endif // BOOST_LOG_NO_THREADS
|
Chris@16
|
207
|
Chris@16
|
208 #endif // BOOST_LOG_DETAIL_LIGHT_RW_MUTEX_HPP_INCLUDED_
|