2 // detail/thread_info_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP
12 #define BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
21 #include <boost/asio/detail/noncopyable.hpp>
23 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
24 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
26 # include <boost/asio/multiple_exceptions.hpp>
27 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
28 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
30 #include <boost/asio/detail/push_options.hpp>
36 class thread_info_base
42 enum { mem_index = 0 };
45 struct awaitable_frame_tag
47 enum { mem_index = 1 };
50 struct executor_function_tag
52 enum { mem_index = 2 };
56 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
57 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
58 : has_pending_exception_(0)
59 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
60 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
62 for (int i = 0; i < max_mem_index; ++i)
63 reusable_memory_[i] = 0;
68 for (int i = 0; i < max_mem_index; ++i)
70 // The following test for non-null pointers is technically redundant, but
71 // it is significantly faster when using a tight io_context::poll() loop
72 // in latency sensitive applications.
73 if (reusable_memory_[i])
74 ::operator delete(reusable_memory_[i]);
78 static void* allocate(thread_info_base* this_thread, std::size_t size)
80 return allocate(default_tag(), this_thread, size);
83 static void deallocate(thread_info_base* this_thread,
84 void* pointer, std::size_t size)
86 deallocate(default_tag(), this_thread, pointer, size);
89 template <typename Purpose>
90 static void* allocate(Purpose, thread_info_base* this_thread,
93 std::size_t chunks = (size + chunk_size - 1) / chunk_size;
95 if (this_thread && this_thread->reusable_memory_[Purpose::mem_index])
97 void* const pointer = this_thread->reusable_memory_[Purpose::mem_index];
98 this_thread->reusable_memory_[Purpose::mem_index] = 0;
100 unsigned char* const mem = static_cast<unsigned char*>(pointer);
101 if (static_cast<std::size_t>(mem[0]) >= chunks)
107 ::operator delete(pointer);
110 void* const pointer = ::operator new(chunks * chunk_size + 1);
111 unsigned char* const mem = static_cast<unsigned char*>(pointer);
112 mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
116 template <typename Purpose>
117 static void deallocate(Purpose, thread_info_base* this_thread,
118 void* pointer, std::size_t size)
120 if (size <= chunk_size * UCHAR_MAX)
122 if (this_thread && this_thread->reusable_memory_[Purpose::mem_index] == 0)
124 unsigned char* const mem = static_cast<unsigned char*>(pointer);
126 this_thread->reusable_memory_[Purpose::mem_index] = pointer;
131 ::operator delete(pointer);
134 void capture_current_exception()
136 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
137 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
138 switch (has_pending_exception_)
141 has_pending_exception_ = 1;
142 pending_exception_ = std::current_exception();
145 has_pending_exception_ = 2;
147 std::make_exception_ptr<multiple_exceptions>(
148 multiple_exceptions(pending_exception_));
153 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
154 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
157 void rethrow_pending_exception()
159 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
160 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
161 if (has_pending_exception_ > 0)
163 has_pending_exception_ = 0;
164 std::exception_ptr ex(
165 BOOST_ASIO_MOVE_CAST(std::exception_ptr)(
166 pending_exception_));
167 std::rethrow_exception(ex);
169 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
170 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
174 enum { chunk_size = 4 };
175 enum { max_mem_index = 3 };
176 void* reusable_memory_[max_mem_index];
178 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
179 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
180 int has_pending_exception_;
181 std::exception_ptr pending_exception_;
182 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
183 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
186 } // namespace detail
190 #include <boost/asio/detail/pop_options.hpp>
192 #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP