Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
277 lines (230 loc) · 6.86 KB

File metadata and controls

277 lines (230 loc) · 6.86 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright Andrey Semashev 2007 - 2015.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
/*!
* \file event.cpp
* \author Andrey Semashev
* \date 24.07.2011
*
* \brief This header is the Boost.Log library implementation, see the library documentation
* at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
*/
#include <boost/log/detail/config.hpp>
#ifndef BOOST_LOG_NO_THREADS
#include <boost/assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/throw_exception.hpp>
#include <boost/log/detail/event.hpp>
#include <boost/log/exceptions.hpp>
#if defined(BOOST_LOG_EVENT_USE_FUTEX)
#include <stddef.h>
#include <errno.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <boost/memory_order.hpp>
// Some Android NDKs (Google NDK and older Crystax.NET NDK versions) don't define SYS_futex
#if defined(SYS_futex)
#define BOOST_LOG_SYS_FUTEX SYS_futex
#else
#define BOOST_LOG_SYS_FUTEX __NR_futex
#endif
#if defined(FUTEX_WAIT_PRIVATE)
#define BOOST_LOG_FUTEX_WAIT FUTEX_WAIT_PRIVATE
#else
#define BOOST_LOG_FUTEX_WAIT FUTEX_WAIT
#endif
#if defined(FUTEX_WAKE_PRIVATE)
#define BOOST_LOG_FUTEX_WAKE FUTEX_WAKE_PRIVATE
#else
#define BOOST_LOG_FUTEX_WAKE FUTEX_WAKE
#endif
#elif defined(BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE)
#include <errno.h>
#include <semaphore.h>
#include <boost/memory_order.hpp>
#include <boost/atomic/fences.hpp>
#elif defined(BOOST_LOG_EVENT_USE_WINAPI)
#include <windows.h>
#include <boost/detail/interlocked.hpp>
#else
#include <boost/thread/locks.hpp>
#endif
#include <boost/log/detail/header.hpp>
namespace boost {
BOOST_LOG_OPEN_NAMESPACE
namespace aux {
#if defined(BOOST_LOG_EVENT_USE_FUTEX)
//! Default constructor
BOOST_LOG_API futex_based_event::futex_based_event() : m_state(0)
{
}
//! Destructor
BOOST_LOG_API futex_based_event::~futex_based_event()
{
}
//! Waits for the object to become signalled
BOOST_LOG_API void futex_based_event::wait()
{
if (m_state.exchange(0, boost::memory_order_acq_rel) == 0)
{
while (true)
{
if (::syscall(BOOST_LOG_SYS_FUTEX, &m_state.storage(), BOOST_LOG_FUTEX_WAIT, 0, NULL, NULL, 0) == 0)
{
// Another thread has set the event while sleeping
break;
}
const int err = errno;
if (err == EWOULDBLOCK)
{
// Another thread has set the event before sleeping
break;
}
else if (BOOST_UNLIKELY(err != EINTR))
{
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to block on the futex", (err));
}
}
m_state.store(0, boost::memory_order_relaxed);
}
}
//! Sets the object to a signalled state
BOOST_LOG_API void futex_based_event::set_signalled()
{
if (m_state.exchange(1, boost::memory_order_release) == 0)
{
if (BOOST_UNLIKELY(::syscall(BOOST_LOG_SYS_FUTEX, &m_state.storage(), BOOST_LOG_FUTEX_WAKE, 1, NULL, NULL, 0) < 0))
{
const int err = errno;
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to wake threads blocked on the futex", (err));
}
}
}
#elif defined(BOOST_LOG_EVENT_USE_POSIX_SEMAPHORE)
//! Default constructor
BOOST_LOG_API sem_based_event::sem_based_event() : m_state()
{
if (BOOST_UNLIKELY(sem_init(&m_semaphore, 0, 0) != 0))
{
const int err = errno;
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to initialize semaphore", (err));
}
}
//! Destructor
BOOST_LOG_API sem_based_event::~sem_based_event()
{
BOOST_VERIFY(sem_destroy(&m_semaphore) == 0);
}
//! Waits for the object to become signalled
BOOST_LOG_API void sem_based_event::wait()
{
boost::atomic_thread_fence(boost::memory_order_acq_rel);
while (true)
{
if (sem_wait(&m_semaphore) != 0)
{
const int err = errno;
if (BOOST_UNLIKELY(err != EINTR))
{
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to block on the semaphore", (err));
}
}
else
break;
}
m_state.clear(boost::memory_order_relaxed);
}
//! Sets the object to a signalled state
BOOST_LOG_API void sem_based_event::set_signalled()
{
if (!m_state.test_and_set(boost::memory_order_release))
{
if (BOOST_UNLIKELY(sem_post(&m_semaphore) != 0))
{
const int err = errno;
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to wake the blocked thread", (err));
}
}
}
#elif defined(BOOST_LOG_EVENT_USE_WINAPI)
//! Default constructor
BOOST_LOG_API winapi_based_event::winapi_based_event() :
m_state(0),
m_event(CreateEventA(NULL, false, false, NULL))
{
if (BOOST_UNLIKELY(!m_event))
{
const DWORD err = GetLastError();
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to create Windows event", (err));
}
}
//! Destructor
BOOST_LOG_API winapi_based_event::~winapi_based_event()
{
BOOST_VERIFY(CloseHandle(m_event) != 0);
}
//! Waits for the object to become signalled
BOOST_LOG_API void winapi_based_event::wait()
{
// On Windows we assume that memory view is always actual (Intel x86 and x86_64 arch)
if (const_cast< volatile boost::uint32_t& >(m_state) == 0)
{
if (BOOST_UNLIKELY(WaitForSingleObject(m_event, INFINITE) != 0))
{
const DWORD err = GetLastError();
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to block on Windows event", (err));
}
}
const_cast< volatile boost::uint32_t& >(m_state) = 0;
}
//! Sets the object to a signalled state
BOOST_LOG_API void winapi_based_event::set_signalled()
{
if (BOOST_INTERLOCKED_COMPARE_EXCHANGE(reinterpret_cast< long* >(&m_state), 1, 0) == 0)
{
if (BOOST_UNLIKELY(SetEvent(m_event) == 0))
{
const DWORD err = GetLastError();
const_cast< volatile boost::uint32_t& >(m_state) = 0;
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to wake the blocked thread", (err));
}
}
}
#else
//! Default constructor
BOOST_LOG_API generic_event::generic_event() : m_state(false)
{
}
//! Destructor
BOOST_LOG_API generic_event::~generic_event()
{
}
//! Waits for the object to become signalled
BOOST_LOG_API void generic_event::wait()
{
boost::unique_lock< boost::mutex > lock(m_mutex);
while (!m_state)
{
m_cond.wait(lock);
}
m_state = false;
}
//! Sets the object to a signalled state
BOOST_LOG_API void generic_event::set_signalled()
{
boost::lock_guard< boost::mutex > lock(m_mutex);
if (!m_state)
{
m_state = true;
m_cond.notify_one();
}
}
#endif
} // namespace aux
BOOST_LOG_CLOSE_NAMESPACE // namespace log
} // namespace boost
#include <boost/log/detail/footer.hpp>
#endif // BOOST_LOG_NO_THREADS
Morty Proxy This is a proxified and sanitized view of the page, visit original site.