thread_info_base.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // detail/thread_info_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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. enum { max_mem_index = parallel_group_tag::end_mem_index };
  81. thread_info_base()
  82. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  83. : has_pending_exception_(0)
  84. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  85. {
  86. for (int i = 0; i < max_mem_index; ++i)
  87. reusable_memory_[i] = 0;
  88. }
  89. ~thread_info_base()
  90. {
  91. for (int i = 0; i < max_mem_index; ++i)
  92. {
  93. // The following test for non-null pointers is technically redundant, but
  94. // it is significantly faster when using a tight io_context::poll() loop
  95. // in latency sensitive applications.
  96. if (reusable_memory_[i])
  97. aligned_delete(reusable_memory_[i]);
  98. }
  99. }
  100. static void* allocate(thread_info_base* this_thread,
  101. std::size_t size, std::size_t align = BOOST_ASIO_DEFAULT_ALIGN)
  102. {
  103. return allocate(default_tag(), this_thread, size, align);
  104. }
  105. static void deallocate(thread_info_base* this_thread,
  106. void* pointer, std::size_t size)
  107. {
  108. deallocate(default_tag(), this_thread, pointer, size);
  109. }
  110. template <typename Purpose>
  111. static void* allocate(Purpose, thread_info_base* this_thread,
  112. std::size_t size, std::size_t align = BOOST_ASIO_DEFAULT_ALIGN)
  113. {
  114. std::size_t chunks = (size + chunk_size - 1) / chunk_size;
  115. if (this_thread)
  116. {
  117. for (int mem_index = Purpose::begin_mem_index;
  118. mem_index < Purpose::end_mem_index; ++mem_index)
  119. {
  120. if (this_thread->reusable_memory_[mem_index])
  121. {
  122. void* const pointer = this_thread->reusable_memory_[mem_index];
  123. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  124. if (static_cast<std::size_t>(mem[0]) >= chunks
  125. && reinterpret_cast<std::size_t>(pointer) % align == 0)
  126. {
  127. this_thread->reusable_memory_[mem_index] = 0;
  128. mem[size] = mem[0];
  129. return pointer;
  130. }
  131. }
  132. }
  133. for (int mem_index = Purpose::begin_mem_index;
  134. mem_index < Purpose::end_mem_index; ++mem_index)
  135. {
  136. if (this_thread->reusable_memory_[mem_index])
  137. {
  138. void* const pointer = this_thread->reusable_memory_[mem_index];
  139. this_thread->reusable_memory_[mem_index] = 0;
  140. aligned_delete(pointer);
  141. break;
  142. }
  143. }
  144. }
  145. void* const pointer = aligned_new(align, chunks * chunk_size + 1);
  146. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  147. mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
  148. return pointer;
  149. }
  150. template <typename Purpose>
  151. static void deallocate(Purpose, thread_info_base* this_thread,
  152. void* pointer, std::size_t size)
  153. {
  154. if (size <= chunk_size * UCHAR_MAX)
  155. {
  156. if (this_thread)
  157. {
  158. for (int mem_index = Purpose::begin_mem_index;
  159. mem_index < Purpose::end_mem_index; ++mem_index)
  160. {
  161. if (this_thread->reusable_memory_[mem_index] == 0)
  162. {
  163. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  164. mem[0] = mem[size];
  165. this_thread->reusable_memory_[mem_index] = pointer;
  166. return;
  167. }
  168. }
  169. }
  170. }
  171. aligned_delete(pointer);
  172. }
  173. void capture_current_exception()
  174. {
  175. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  176. switch (has_pending_exception_)
  177. {
  178. case 0:
  179. has_pending_exception_ = 1;
  180. pending_exception_ = std::current_exception();
  181. break;
  182. case 1:
  183. has_pending_exception_ = 2;
  184. pending_exception_ =
  185. std::make_exception_ptr<multiple_exceptions>(
  186. multiple_exceptions(pending_exception_));
  187. break;
  188. default:
  189. break;
  190. }
  191. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  192. }
  193. void rethrow_pending_exception()
  194. {
  195. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  196. if (has_pending_exception_ > 0)
  197. {
  198. has_pending_exception_ = 0;
  199. std::exception_ptr ex(
  200. static_cast<std::exception_ptr&&>(
  201. pending_exception_));
  202. std::rethrow_exception(ex);
  203. }
  204. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  205. }
  206. private:
  207. #if defined(BOOST_ASIO_HAS_IO_URING)
  208. enum { chunk_size = 8 };
  209. #else // defined(BOOST_ASIO_HAS_IO_URING)
  210. enum { chunk_size = 4 };
  211. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  212. void* reusable_memory_[max_mem_index];
  213. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  214. int has_pending_exception_;
  215. std::exception_ptr pending_exception_;
  216. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  217. };
  218. } // namespace detail
  219. } // namespace asio
  220. } // namespace boost
  221. #include <boost/asio/detail/pop_options.hpp>
  222. #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP