forked from boostorg/log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_id.cpp
More file actions
272 lines (201 loc) · 6.33 KB
/
thread_id.cpp
File metadata and controls
272 lines (201 loc) · 6.33 KB
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
/*
* 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 thread_id.cpp
* \author Andrey Semashev
* \date 08.1.2012
*
* \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>
#if !defined(BOOST_LOG_NO_THREADS)
#include <new>
#include <iostream>
#include <boost/throw_exception.hpp>
#if !defined(BOOST_WINDOWS)
#include <cstring>
#include <boost/predef/other/endian.h>
#endif
#include <boost/log/detail/thread_id.hpp>
#if defined(BOOST_LOG_USE_COMPILER_TLS)
#include <boost/aligned_storage.hpp>
#include <boost/type_traits/alignment_of.hpp>
#elif defined(BOOST_WINDOWS)
#include <boost/thread/thread.hpp>
#include <boost/log/detail/thread_specific.hpp>
#else
#include <boost/log/exceptions.hpp>
#include <boost/log/utility/once_block.hpp>
#endif
#if !defined(BOOST_LOG_USE_COMPILER_TLS)
#include <boost/log/detail/singleton.hpp>
#endif
#include "id_formatting.hpp"
#include <boost/log/detail/header.hpp>
#if defined(BOOST_WINDOWS)
#include <windows.h>
namespace boost {
BOOST_LOG_OPEN_NAMESPACE
namespace aux {
enum { tid_size = sizeof(GetCurrentThreadId()) };
BOOST_LOG_ANONYMOUS_NAMESPACE {
//! The function returns current process identifier
inline thread::id get_id_impl()
{
return thread::id(GetCurrentThreadId());
}
} // namespace
} // namespace aux
BOOST_LOG_CLOSE_NAMESPACE // namespace log
} // namespace boost
#else // defined(BOOST_WINDOWS)
#include <pthread.h>
namespace boost {
BOOST_LOG_OPEN_NAMESPACE
namespace aux {
enum
{
headroom_size = sizeof(pthread_t) > sizeof(uintmax_t) ? 0u : (sizeof(uintmax_t) - sizeof(pthread_t)),
tid_size = sizeof(uintmax_t) - headroom_size
};
BOOST_LOG_ANONYMOUS_NAMESPACE {
//! The function returns current thread identifier
inline thread::id get_id_impl()
{
// According to POSIX, pthread_t may not be an integer type:
// http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html
// For now we use the hackish cast to get some opaque number that hopefully correlates with system thread identification.
thread::id::native_type int_id = 0;
pthread_t pthread_id = pthread_self();
#if BOOST_ENDIAN_BIG_BYTE || BOOST_ENDIAN_BIG_WORD
std::memcpy(reinterpret_cast< unsigned char* >(&int_id) + headroom_size, &pthread_id, tid_size);
#else
std::memcpy(&int_id, &pthread_id, tid_size);
#endif
return thread::id(int_id);
}
} // namespace
} // namespace aux
BOOST_LOG_CLOSE_NAMESPACE // namespace log
} // namespace boost
#endif // defined(BOOST_WINDOWS)
namespace boost {
BOOST_LOG_OPEN_NAMESPACE
namespace aux {
namespace this_thread {
#if defined(BOOST_LOG_USE_COMPILER_TLS)
BOOST_LOG_ANONYMOUS_NAMESPACE {
struct id_storage
{
aligned_storage< sizeof(thread::id), alignment_of< thread::id >::value >::type m_storage;
bool m_initialized;
};
BOOST_LOG_TLS id_storage g_id_storage = {};
} // namespace
//! The function returns current thread identifier
BOOST_LOG_API thread::id const& get_id()
{
id_storage& s = g_id_storage;
if (BOOST_UNLIKELY(!s.m_initialized))
{
new (s.m_storage.address()) thread::id(get_id_impl());
s.m_initialized = true;
}
return *static_cast< thread::id const* >(s.m_storage.address());
}
#elif defined(BOOST_WINDOWS)
BOOST_LOG_ANONYMOUS_NAMESPACE {
struct id_storage :
lazy_singleton< id_storage >
{
struct deleter
{
typedef void result_type;
explicit deleter(thread::id const* p) : m_p(p) {}
result_type operator() () const
{
delete m_p;
}
private:
thread::id const* m_p;
};
thread_specific< thread::id const* > m_id;
};
} // namespace
//! The function returns current thread identifier
BOOST_LOG_API thread::id const& get_id()
{
id_storage& s = id_storage::get();
thread::id const* p = s.m_id.get();
if (BOOST_UNLIKELY(!p))
{
p = new thread::id(get_id_impl());
s.m_id.set(p);
boost::this_thread::at_thread_exit(id_storage::deleter(p));
}
return *p;
}
#else
BOOST_LOG_ANONYMOUS_NAMESPACE {
pthread_key_t g_key;
void deleter(void* p)
{
delete static_cast< thread::id* >(p);
}
} // namespace
//! The function returns current thread identifier
BOOST_LOG_API thread::id const& get_id()
{
BOOST_LOG_ONCE_BLOCK()
{
if (int err = pthread_key_create(&g_key, &deleter))
{
BOOST_LOG_THROW_DESCR_PARAMS(system_error, "Failed to create a thread-specific storage for thread id", (err));
}
}
thread::id* p = static_cast< thread::id* >(pthread_getspecific(g_key));
if (BOOST_UNLIKELY(!p))
{
p = new thread::id(get_id_impl());
pthread_setspecific(g_key, p);
}
return *p;
}
#endif
} // namespace this_thread
// Used in default_sink.cpp
void format_thread_id(char* buf, std::size_t size, thread::id tid)
{
format_id< tid_size >(buf, size, tid.native_id(), false);
}
template< typename CharT, typename TraitsT >
BOOST_LOG_API std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, thread::id const& tid)
{
if (strm.good())
{
CharT buf[tid_size * 2 + 3]; // 2 chars per byte + 3 chars for the leading 0x and terminating zero
format_id< tid_size >(buf, sizeof(buf) / sizeof(*buf), tid.native_id(), (strm.flags() & std::ios_base::uppercase) != 0);
strm << buf;
}
return strm;
}
#if defined(BOOST_LOG_USE_CHAR)
template BOOST_LOG_API
std::basic_ostream< char, std::char_traits< char > >&
operator<< (std::basic_ostream< char, std::char_traits< char > >& strm, thread::id const& tid);
#endif // defined(BOOST_LOG_USE_CHAR)
#if defined(BOOST_LOG_USE_WCHAR_T)
template BOOST_LOG_API
std::basic_ostream< wchar_t, std::char_traits< wchar_t > >&
operator<< (std::basic_ostream< wchar_t, std::char_traits< wchar_t > >& strm, thread::id const& tid);
#endif // defined(BOOST_LOG_USE_WCHAR_T)
} // namespace aux
BOOST_LOG_CLOSE_NAMESPACE // namespace log
} // namespace boost
#include <boost/log/detail/footer.hpp>
#endif // !defined(BOOST_LOG_NO_THREADS)