thread_info_base.hpp
4.91 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
//
// detail/thread_info_base.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// 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)
//
#ifndef ASIO_DETAIL_THREAD_INFO_BASE_HPP
#define ASIO_DETAIL_THREAD_INFO_BASE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include <climits>
#include <cstddef>
#include "asio/detail/noncopyable.hpp"
#if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
&& !defined(ASIO_NO_EXCEPTIONS)
# include <exception>
# include "asio/multiple_exceptions.hpp"
#endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
// && !defined(ASIO_NO_EXCEPTIONS)
#include "asio/detail/push_options.hpp"
namespace asio {
namespace detail {
class thread_info_base
: private noncopyable
{
public:
struct default_tag
{
enum { mem_index = 0 };
};
struct awaitable_frame_tag
{
enum { mem_index = 1 };
};
struct executor_function_tag
{
enum { mem_index = 2 };
};
thread_info_base()
#if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
&& !defined(ASIO_NO_EXCEPTIONS)
: has_pending_exception_(0)
#endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
// && !defined(ASIO_NO_EXCEPTIONS)
{
for (int i = 0; i < max_mem_index; ++i)
reusable_memory_[i] = 0;
}
~thread_info_base()
{
for (int i = 0; i < max_mem_index; ++i)
{
// The following test for non-null pointers is technically redundant, but
// it is significantly faster when using a tight io_context::poll() loop
// in latency sensitive applications.
if (reusable_memory_[i])
::operator delete(reusable_memory_[i]);
}
}
static void* allocate(thread_info_base* this_thread, std::size_t size)
{
return allocate(default_tag(), this_thread, size);
}
static void deallocate(thread_info_base* this_thread,
void* pointer, std::size_t size)
{
deallocate(default_tag(), this_thread, pointer, size);
}
template <typename Purpose>
static void* allocate(Purpose, thread_info_base* this_thread,
std::size_t size)
{
std::size_t chunks = (size + chunk_size - 1) / chunk_size;
if (this_thread && this_thread->reusable_memory_[Purpose::mem_index])
{
void* const pointer = this_thread->reusable_memory_[Purpose::mem_index];
this_thread->reusable_memory_[Purpose::mem_index] = 0;
unsigned char* const mem = static_cast<unsigned char*>(pointer);
if (static_cast<std::size_t>(mem[0]) >= chunks)
{
mem[size] = mem[0];
return pointer;
}
::operator delete(pointer);
}
void* const pointer = ::operator new(chunks * chunk_size + 1);
unsigned char* const mem = static_cast<unsigned char*>(pointer);
mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
return pointer;
}
template <typename Purpose>
static void deallocate(Purpose, thread_info_base* this_thread,
void* pointer, std::size_t size)
{
if (size <= chunk_size * UCHAR_MAX)
{
if (this_thread && this_thread->reusable_memory_[Purpose::mem_index] == 0)
{
unsigned char* const mem = static_cast<unsigned char*>(pointer);
mem[0] = mem[size];
this_thread->reusable_memory_[Purpose::mem_index] = pointer;
return;
}
}
::operator delete(pointer);
}
void capture_current_exception()
{
#if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
&& !defined(ASIO_NO_EXCEPTIONS)
switch (has_pending_exception_)
{
case 0:
has_pending_exception_ = 1;
pending_exception_ = std::current_exception();
break;
case 1:
has_pending_exception_ = 2;
pending_exception_ =
std::make_exception_ptr<multiple_exceptions>(
multiple_exceptions(pending_exception_));
break;
default:
break;
}
#endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
// && !defined(ASIO_NO_EXCEPTIONS)
}
void rethrow_pending_exception()
{
#if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
&& !defined(ASIO_NO_EXCEPTIONS)
if (has_pending_exception_ > 0)
{
has_pending_exception_ = 0;
std::exception_ptr ex(
ASIO_MOVE_CAST(std::exception_ptr)(
pending_exception_));
std::rethrow_exception(ex);
}
#endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
// && !defined(ASIO_NO_EXCEPTIONS)
}
private:
enum { chunk_size = 4 };
enum { max_mem_index = 3 };
void* reusable_memory_[max_mem_index];
#if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
&& !defined(ASIO_NO_EXCEPTIONS)
int has_pending_exception_;
std::exception_ptr pending_exception_;
#endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
// && !defined(ASIO_NO_EXCEPTIONS)
};
} // namespace detail
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_DETAIL_THREAD_INFO_BASE_HPP