thread_info_base.hpp 6.9 KB

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