]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 adaptive_mutex.hpp | |
9 | * \author Andrey Semashev | |
10 | * \date 01.08.2010 | |
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_ADAPTIVE_MUTEX_HPP_INCLUDED_ | |
17 | #define BOOST_LOG_DETAIL_ADAPTIVE_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/throw_exception.hpp> | |
28 | #include <boost/thread/exceptions.hpp> | |
29 | ||
30 | #if defined(BOOST_THREAD_POSIX) // This one can be defined by users, so it should go first | |
31 | #define BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD | |
32 | #elif defined(BOOST_WINDOWS) | |
33 | #define BOOST_LOG_ADAPTIVE_MUTEX_USE_WINAPI | |
34 | #elif defined(BOOST_HAS_PTHREADS) | |
35 | #define BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD | |
36 | #endif | |
37 | ||
38 | #if defined(BOOST_LOG_ADAPTIVE_MUTEX_USE_WINAPI) | |
39 | ||
40 | #include <boost/log/detail/pause.hpp> | |
41 | #include <boost/detail/interlocked.hpp> | |
42 | #include <boost/detail/winapi/thread.hpp> | |
43 | ||
44 | #if defined(__INTEL_COMPILER) || defined(_MSC_VER) | |
45 | # if defined(__INTEL_COMPILER) | |
46 | # define BOOST_LOG_COMPILER_BARRIER __memory_barrier() | |
47 | # else | |
48 | extern "C" void _ReadWriteBarrier(void); | |
49 | # if defined(BOOST_MSVC) | |
50 | # pragma intrinsic(_ReadWriteBarrier) | |
51 | # endif | |
52 | # define BOOST_LOG_COMPILER_BARRIER _ReadWriteBarrier() | |
53 | # endif | |
54 | #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) | |
55 | # define BOOST_LOG_COMPILER_BARRIER __asm__ __volatile__("" : : : "memory") | |
56 | #endif | |
57 | ||
58 | #include <boost/log/detail/header.hpp> | |
59 | ||
60 | namespace boost { | |
61 | ||
62 | BOOST_LOG_OPEN_NAMESPACE | |
63 | ||
64 | namespace aux { | |
65 | ||
66 | //! A mutex that performs spinning or thread yielding in case of contention | |
67 | class adaptive_mutex | |
68 | { | |
69 | private: | |
70 | enum state | |
71 | { | |
72 | initial_pause = 2, | |
73 | max_pause = 16 | |
74 | }; | |
75 | ||
76 | long m_State; | |
77 | ||
78 | public: | |
79 | adaptive_mutex() : m_State(0) {} | |
80 | ||
81 | bool try_lock() | |
82 | { | |
83 | return (BOOST_INTERLOCKED_COMPARE_EXCHANGE(&m_State, 1L, 0L) == 0L); | |
84 | } | |
85 | ||
86 | void lock() | |
87 | { | |
88 | #if defined(BOOST_LOG_AUX_PAUSE) | |
89 | unsigned int pause_count = initial_pause; | |
90 | #endif | |
91 | while (!try_lock()) | |
92 | { | |
93 | #if defined(BOOST_LOG_AUX_PAUSE) | |
94 | if (pause_count < max_pause) | |
95 | { | |
96 | for (unsigned int i = 0; i < pause_count; ++i) | |
97 | { | |
98 | BOOST_LOG_AUX_PAUSE; | |
99 | } | |
100 | pause_count += pause_count; | |
101 | } | |
102 | else | |
103 | { | |
104 | // Restart spinning after waking up this thread | |
105 | pause_count = initial_pause; | |
106 | SwitchToThread(); | |
107 | } | |
108 | #else | |
109 | SwitchToThread(); | |
110 | #endif | |
111 | } | |
112 | } | |
113 | ||
114 | void unlock() | |
115 | { | |
116 | #if (defined(_M_IX86) || defined(_M_AMD64)) && defined(BOOST_LOG_COMPILER_BARRIER) | |
117 | BOOST_LOG_COMPILER_BARRIER; | |
118 | m_State = 0L; | |
119 | BOOST_LOG_COMPILER_BARRIER; | |
120 | #else | |
121 | BOOST_INTERLOCKED_EXCHANGE(&m_State, 0L); | |
122 | #endif | |
123 | } | |
124 | ||
125 | // Non-copyable | |
126 | BOOST_DELETED_FUNCTION(adaptive_mutex(adaptive_mutex const&)) | |
127 | BOOST_DELETED_FUNCTION(adaptive_mutex& operator= (adaptive_mutex const&)) | |
128 | }; | |
129 | ||
130 | #undef BOOST_LOG_AUX_PAUSE | |
131 | #undef BOOST_LOG_COMPILER_BARRIER | |
132 | ||
133 | } // namespace aux | |
134 | ||
135 | BOOST_LOG_CLOSE_NAMESPACE // namespace log | |
136 | ||
137 | } // namespace boost | |
138 | ||
139 | #include <boost/log/detail/footer.hpp> | |
140 | ||
141 | #elif defined(BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD) | |
142 | ||
143 | #include <pthread.h> | |
144 | #include <boost/assert.hpp> | |
145 | #include <boost/log/detail/header.hpp> | |
146 | ||
147 | #if defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) | |
148 | #define BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD_MUTEX_ADAPTIVE_NP | |
149 | #endif | |
150 | ||
151 | namespace boost { | |
152 | ||
153 | BOOST_LOG_OPEN_NAMESPACE | |
154 | ||
155 | namespace aux { | |
156 | ||
157 | //! A mutex that performs spinning or thread yielding in case of contention | |
158 | class adaptive_mutex | |
159 | { | |
160 | private: | |
161 | pthread_mutex_t m_State; | |
162 | ||
163 | public: | |
164 | adaptive_mutex() | |
165 | { | |
166 | #if defined(BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD_MUTEX_ADAPTIVE_NP) | |
167 | pthread_mutexattr_t attrs; | |
168 | pthread_mutexattr_init(&attrs); | |
169 | pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_ADAPTIVE_NP); | |
170 | ||
171 | const int err = pthread_mutex_init(&m_State, &attrs); | |
172 | pthread_mutexattr_destroy(&attrs); | |
173 | #else | |
174 | const int err = pthread_mutex_init(&m_State, NULL); | |
175 | #endif | |
176 | if (BOOST_UNLIKELY(err != 0)) | |
177 | throw_exception< thread_resource_error >(err, "Failed to initialize an adaptive mutex", "adaptive_mutex::adaptive_mutex()", __FILE__, __LINE__); | |
178 | } | |
179 | ||
180 | ~adaptive_mutex() | |
181 | { | |
182 | BOOST_VERIFY(pthread_mutex_destroy(&m_State) == 0); | |
183 | } | |
184 | ||
185 | bool try_lock() | |
186 | { | |
187 | const int err = pthread_mutex_trylock(&m_State); | |
188 | if (err == 0) | |
189 | return true; | |
190 | if (BOOST_UNLIKELY(err != EBUSY)) | |
191 | throw_exception< lock_error >(err, "Failed to lock an adaptive mutex", "adaptive_mutex::try_lock()", __FILE__, __LINE__); | |
192 | return false; | |
193 | } | |
194 | ||
195 | void lock() | |
196 | { | |
197 | const int err = pthread_mutex_lock(&m_State); | |
198 | if (BOOST_UNLIKELY(err != 0)) | |
199 | throw_exception< lock_error >(err, "Failed to lock an adaptive mutex", "adaptive_mutex::lock()", __FILE__, __LINE__); | |
200 | } | |
201 | ||
202 | void unlock() | |
203 | { | |
204 | BOOST_VERIFY(pthread_mutex_unlock(&m_State) == 0); | |
205 | } | |
206 | ||
207 | // Non-copyable | |
208 | BOOST_DELETED_FUNCTION(adaptive_mutex(adaptive_mutex const&)) | |
209 | BOOST_DELETED_FUNCTION(adaptive_mutex& operator= (adaptive_mutex const&)) | |
210 | ||
211 | private: | |
212 | template< typename ExceptionT > | |
213 | static BOOST_NOINLINE BOOST_LOG_NORETURN void throw_exception(int err, const char* descr, const char* func, const char* file, int line) | |
214 | { | |
215 | #if !defined(BOOST_EXCEPTION_DISABLE) | |
216 | boost::exception_detail::throw_exception_(ExceptionT(err, descr), func, file, line); | |
217 | #else | |
218 | boost::throw_exception(ExceptionT(err, descr)); | |
219 | #endif | |
220 | } | |
221 | }; | |
222 | ||
223 | } // namespace aux | |
224 | ||
225 | BOOST_LOG_CLOSE_NAMESPACE // namespace log | |
226 | ||
227 | } // namespace boost | |
228 | ||
229 | #include <boost/log/detail/footer.hpp> | |
230 | ||
231 | #endif | |
232 | ||
233 | #endif // BOOST_LOG_NO_THREADS | |
234 | ||
235 | #endif // BOOST_LOG_DETAIL_ADAPTIVE_MUTEX_HPP_INCLUDED_ |