thread_info_base.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // detail/thread_info_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP
  11. #define BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <climits>
  17. #include <cstddef>
  18. #include <boost/asio/detail/memory.hpp>
  19. #include <boost/asio/detail/noncopyable.hpp>
  20. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  21. # include <exception>
  22. # include <boost/asio/multiple_exceptions.hpp>
  23. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. #ifndef BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE
  29. # define BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE 2
  30. #endif // BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE
  31. class thread_info_base
  32. : private noncopyable
  33. {
  34. public:
  35. struct default_tag
  36. {
  37. enum
  38. {
  39. cache_size = BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE,
  40. begin_mem_index = 0,
  41. end_mem_index = cache_size
  42. };
  43. };
  44. struct awaitable_frame_tag
  45. {
  46. enum
  47. {
  48. cache_size = BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE,
  49. begin_mem_index = default_tag::end_mem_index,
  50. end_mem_index = begin_mem_index + cache_size
  51. };
  52. };
  53. struct executor_function_tag
  54. {
  55. enum
  56. {
  57. cache_size = BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE,
  58. begin_mem_index = awaitable_frame_tag::end_mem_index,
  59. end_mem_index = begin_mem_index + cache_size
  60. };
  61. };
  62. struct cancellation_signal_tag
  63. {
  64. enum
  65. {
  66. cache_size = BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE,
  67. begin_mem_index = executor_function_tag::end_mem_index,
  68. end_mem_index = begin_mem_index + cache_size
  69. };
  70. };
  71. struct parallel_group_tag
  72. {
  73. enum
  74. {
  75. cache_size = BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE,
  76. begin_mem_index = cancellation_signal_tag::end_mem_index,
  77. end_mem_index = begin_mem_index + cache_size
  78. };
  79. };
  80. struct timed_cancel_tag
  81. {
  82. enum
  83. {
  84. cache_size = BOOST_ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE,
  85. begin_mem_index = parallel_group_tag::end_mem_index,
  86. end_mem_index = begin_mem_index + cache_size
  87. };
  88. };
  89. enum { max_mem_index = timed_cancel_tag::end_mem_index };
  90. thread_info_base()
  91. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  92. : has_pending_exception_(0)
  93. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  94. {
  95. for (int i = 0; i < max_mem_index; ++i)
  96. reusable_memory_[i] = 0;
  97. }
  98. ~thread_info_base()
  99. {
  100. for (int i = 0; i < max_mem_index; ++i)
  101. {
  102. // The following test for non-null pointers is technically redundant, but
  103. // it is significantly faster when using a tight io_context::poll() loop
  104. // in latency sensitive applications.
  105. if (reusable_memory_[i])
  106. aligned_delete(reusable_memory_[i]);
  107. }
  108. }
  109. static void* allocate(thread_info_base* this_thread,
  110. std::size_t size, std::size_t align = BOOST_ASIO_DEFAULT_ALIGN)
  111. {
  112. return allocate(default_tag(), this_thread, size, align);
  113. }
  114. static void deallocate(thread_info_base* this_thread,
  115. void* pointer, std::size_t size)
  116. {
  117. deallocate(default_tag(), this_thread, pointer, size);
  118. }
  119. template <typename Purpose>
  120. static void* allocate(Purpose, thread_info_base* this_thread,
  121. std::size_t size, std::size_t align = BOOST_ASIO_DEFAULT_ALIGN)
  122. {
  123. std::size_t chunks = (size + chunk_size - 1) / chunk_size;
  124. if (this_thread)
  125. {
  126. for (int mem_index = Purpose::begin_mem_index;
  127. mem_index < Purpose::end_mem_index; ++mem_index)
  128. {
  129. if (this_thread->reusable_memory_[mem_index])
  130. {
  131. void* const pointer = this_thread->reusable_memory_[mem_index];
  132. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  133. if (static_cast<std::size_t>(mem[0]) >= chunks
  134. && reinterpret_cast<std::size_t>(pointer) % align == 0)
  135. {
  136. this_thread->reusable_memory_[mem_index] = 0;
  137. mem[size] = mem[0];
  138. return pointer;
  139. }
  140. }
  141. }
  142. for (int mem_index = Purpose::begin_mem_index;
  143. mem_index < Purpose::end_mem_index; ++mem_index)
  144. {
  145. if (this_thread->reusable_memory_[mem_index])
  146. {
  147. void* const pointer = this_thread->reusable_memory_[mem_index];
  148. this_thread->reusable_memory_[mem_index] = 0;
  149. aligned_delete(pointer);
  150. break;
  151. }
  152. }
  153. }
  154. void* const pointer = aligned_new(align, chunks * chunk_size + 1);
  155. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  156. mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
  157. return pointer;
  158. }
  159. template <typename Purpose>
  160. static void deallocate(Purpose, thread_info_base* this_thread,
  161. void* pointer, std::size_t size)
  162. {
  163. if (size <= chunk_size * UCHAR_MAX)
  164. {
  165. if (this_thread)
  166. {
  167. for (int mem_index = Purpose::begin_mem_index;
  168. mem_index < Purpose::end_mem_index; ++mem_index)
  169. {
  170. if (this_thread->reusable_memory_[mem_index] == 0)
  171. {
  172. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  173. mem[0] = mem[size];
  174. this_thread->reusable_memory_[mem_index] = pointer;
  175. return;
  176. }
  177. }
  178. }
  179. }
  180. aligned_delete(pointer);
  181. }
  182. void capture_current_exception()
  183. {
  184. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  185. switch (has_pending_exception_)
  186. {
  187. case 0:
  188. has_pending_exception_ = 1;
  189. pending_exception_ = std::current_exception();
  190. break;
  191. case 1:
  192. has_pending_exception_ = 2;
  193. pending_exception_ =
  194. std::make_exception_ptr<multiple_exceptions>(
  195. multiple_exceptions(pending_exception_));
  196. break;
  197. default:
  198. break;
  199. }
  200. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  201. }
  202. void rethrow_pending_exception()
  203. {
  204. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  205. if (has_pending_exception_ > 0)
  206. {
  207. has_pending_exception_ = 0;
  208. std::exception_ptr ex(
  209. static_cast<std::exception_ptr&&>(
  210. pending_exception_));
  211. std::rethrow_exception(ex);
  212. }
  213. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  214. }
  215. private:
  216. #if defined(BOOST_ASIO_HAS_IO_URING)
  217. enum { chunk_size = 8 };
  218. #else // defined(BOOST_ASIO_HAS_IO_URING)
  219. enum { chunk_size = 4 };
  220. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  221. void* reusable_memory_[max_mem_index];
  222. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  223. int has_pending_exception_;
  224. std::exception_ptr pending_exception_;
  225. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  226. };
  227. } // namespace detail
  228. } // namespace asio
  229. } // namespace boost
  230. #include <boost/asio/detail/pop_options.hpp>
  231. #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP