kqueue_reactor.ipp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //
  2. // detail/impl/kqueue_reactor.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_IPP
  12. #define BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_KQUEUE)
  18. #include <boost/asio/config.hpp>
  19. #include <boost/asio/detail/kqueue_reactor.hpp>
  20. #include <boost/asio/detail/scheduler.hpp>
  21. #include <boost/asio/detail/throw_error.hpp>
  22. #include <boost/asio/error.hpp>
  23. #if defined(__NetBSD__)
  24. # include <sys/param.h>
  25. #endif
  26. #include <boost/asio/detail/push_options.hpp>
  27. #if defined(__NetBSD__) && __NetBSD_Version__ < 999001500
  28. # define BOOST_ASIO_KQUEUE_EV_SET(ev, ident, filt, flags, fflags, data, udata) \
  29. EV_SET(ev, ident, filt, flags, fflags, data, \
  30. reinterpret_cast<intptr_t>(static_cast<void*>(udata)))
  31. #else
  32. # define BOOST_ASIO_KQUEUE_EV_SET(ev, ident, filt, flags, fflags, data, udata) \
  33. EV_SET(ev, ident, filt, flags, fflags, data, udata)
  34. #endif
  35. namespace boost {
  36. namespace asio {
  37. namespace detail {
  38. kqueue_reactor::kqueue_reactor(boost::asio::execution_context& ctx)
  39. : execution_context_service_base<kqueue_reactor>(ctx),
  40. scheduler_(use_service<scheduler>(ctx)),
  41. mutex_(config(ctx).get("reactor", "registration_locking", true),
  42. config(ctx).get("reactor", "registration_locking_spin_count", 0)),
  43. kqueue_fd_(do_kqueue_create()),
  44. interrupter_(),
  45. shutdown_(false),
  46. io_locking_(config(ctx).get("reactor", "io_locking", true)),
  47. io_locking_spin_count_(
  48. config(ctx).get("reactor", "io_locking_spin_count", 0)),
  49. registered_descriptors_mutex_(mutex_.enabled()),
  50. registered_descriptors_(execution_context::allocator<void>(ctx),
  51. config(ctx).get("reactor", "preallocated_io_objects", 0U),
  52. io_locking_, io_locking_spin_count_)
  53. {
  54. struct kevent events[1];
  55. BOOST_ASIO_KQUEUE_EV_SET(&events[0], interrupter_.read_descriptor(),
  56. EVFILT_READ, EV_ADD, 0, 0, &interrupter_);
  57. if (::kevent(kqueue_fd_, events, 1, 0, 0, 0) == -1)
  58. {
  59. boost::system::error_code error(errno,
  60. boost::asio::error::get_system_category());
  61. boost::asio::detail::throw_error(error);
  62. }
  63. }
  64. kqueue_reactor::~kqueue_reactor()
  65. {
  66. close(kqueue_fd_);
  67. }
  68. void kqueue_reactor::shutdown()
  69. {
  70. mutex::scoped_lock lock(mutex_);
  71. shutdown_ = true;
  72. lock.unlock();
  73. op_queue<operation> ops;
  74. while (descriptor_state* state = registered_descriptors_.first())
  75. {
  76. for (int i = 0; i < max_ops; ++i)
  77. ops.push(state->op_queue_[i]);
  78. state->shutdown_ = true;
  79. registered_descriptors_.free(state);
  80. }
  81. timer_queues_.get_all_timers(ops);
  82. scheduler_.abandon_operations(ops);
  83. }
  84. void kqueue_reactor::notify_fork(
  85. boost::asio::execution_context::fork_event fork_ev)
  86. {
  87. if (fork_ev == boost::asio::execution_context::fork_child)
  88. {
  89. // The kqueue descriptor is automatically closed in the child.
  90. kqueue_fd_ = -1;
  91. kqueue_fd_ = do_kqueue_create();
  92. interrupter_.recreate();
  93. struct kevent events[2];
  94. BOOST_ASIO_KQUEUE_EV_SET(&events[0], interrupter_.read_descriptor(),
  95. EVFILT_READ, EV_ADD, 0, 0, &interrupter_);
  96. if (::kevent(kqueue_fd_, events, 1, 0, 0, 0) == -1)
  97. {
  98. boost::system::error_code ec(errno,
  99. boost::asio::error::get_system_category());
  100. boost::asio::detail::throw_error(ec, "kqueue interrupter registration");
  101. }
  102. // Re-register all descriptors with kqueue.
  103. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  104. for (descriptor_state* state = registered_descriptors_.first();
  105. state != 0; state = state->next_)
  106. {
  107. if (state->num_kevents_ > 0)
  108. {
  109. BOOST_ASIO_KQUEUE_EV_SET(&events[0], state->descriptor_,
  110. EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, state);
  111. BOOST_ASIO_KQUEUE_EV_SET(&events[1], state->descriptor_,
  112. EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, state);
  113. if (::kevent(kqueue_fd_, events, state->num_kevents_, 0, 0, 0) == -1)
  114. {
  115. boost::system::error_code ec(errno,
  116. boost::asio::error::get_system_category());
  117. boost::asio::detail::throw_error(ec, "kqueue re-registration");
  118. }
  119. }
  120. }
  121. }
  122. }
  123. void kqueue_reactor::init_task()
  124. {
  125. scheduler_.init_task();
  126. }
  127. int kqueue_reactor::register_descriptor(socket_type descriptor,
  128. kqueue_reactor::per_descriptor_data& descriptor_data)
  129. {
  130. descriptor_data = allocate_descriptor_state();
  131. BOOST_ASIO_HANDLER_REACTOR_REGISTRATION((
  132. context(), static_cast<uintmax_t>(descriptor),
  133. reinterpret_cast<uintmax_t>(descriptor_data)));
  134. mutex::scoped_lock lock(descriptor_data->mutex_);
  135. descriptor_data->descriptor_ = descriptor;
  136. descriptor_data->num_kevents_ = 0;
  137. descriptor_data->shutdown_ = false;
  138. return 0;
  139. }
  140. int kqueue_reactor::register_internal_descriptor(
  141. int op_type, socket_type descriptor,
  142. kqueue_reactor::per_descriptor_data& descriptor_data, reactor_op* op)
  143. {
  144. descriptor_data = allocate_descriptor_state();
  145. BOOST_ASIO_HANDLER_REACTOR_REGISTRATION((
  146. context(), static_cast<uintmax_t>(descriptor),
  147. reinterpret_cast<uintmax_t>(descriptor_data)));
  148. mutex::scoped_lock lock(descriptor_data->mutex_);
  149. descriptor_data->descriptor_ = descriptor;
  150. descriptor_data->num_kevents_ = 1;
  151. descriptor_data->shutdown_ = false;
  152. descriptor_data->op_queue_[op_type].push(op);
  153. struct kevent events[1];
  154. BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, EVFILT_READ,
  155. EV_ADD | EV_CLEAR, 0, 0, descriptor_data);
  156. if (::kevent(kqueue_fd_, events, 1, 0, 0, 0) == -1)
  157. return errno;
  158. return 0;
  159. }
  160. void kqueue_reactor::move_descriptor(socket_type,
  161. kqueue_reactor::per_descriptor_data& target_descriptor_data,
  162. kqueue_reactor::per_descriptor_data& source_descriptor_data)
  163. {
  164. target_descriptor_data = source_descriptor_data;
  165. source_descriptor_data = 0;
  166. }
  167. void kqueue_reactor::call_post_immediate_completion(
  168. operation* op, bool is_continuation, const void* self)
  169. {
  170. static_cast<const kqueue_reactor*>(self)->post_immediate_completion(
  171. op, is_continuation);
  172. }
  173. void kqueue_reactor::start_op(int op_type, socket_type descriptor,
  174. kqueue_reactor::per_descriptor_data& descriptor_data, reactor_op* op,
  175. bool is_continuation, bool allow_speculative,
  176. void (*on_immediate)(operation*, bool, const void*),
  177. const void* immediate_arg)
  178. {
  179. if (!descriptor_data)
  180. {
  181. op->ec_ = boost::asio::error::bad_descriptor;
  182. on_immediate(op, is_continuation, immediate_arg);
  183. return;
  184. }
  185. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  186. if (descriptor_data->shutdown_)
  187. {
  188. on_immediate(op, is_continuation, immediate_arg);
  189. return;
  190. }
  191. if (descriptor_data->op_queue_[op_type].empty())
  192. {
  193. static const int num_kevents[max_ops] = { 1, 2, 1 };
  194. if (allow_speculative
  195. && (op_type != read_op
  196. || descriptor_data->op_queue_[except_op].empty()))
  197. {
  198. if (op->perform())
  199. {
  200. descriptor_lock.unlock();
  201. on_immediate(op, is_continuation, immediate_arg);
  202. return;
  203. }
  204. if (descriptor_data->num_kevents_ < num_kevents[op_type])
  205. {
  206. struct kevent events[2];
  207. BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, EVFILT_READ,
  208. EV_ADD | EV_CLEAR, 0, 0, descriptor_data);
  209. BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, EVFILT_WRITE,
  210. EV_ADD | EV_CLEAR, 0, 0, descriptor_data);
  211. if (::kevent(kqueue_fd_, events, num_kevents[op_type], 0, 0, 0) != -1)
  212. {
  213. descriptor_data->num_kevents_ = num_kevents[op_type];
  214. }
  215. else
  216. {
  217. op->ec_ = boost::system::error_code(errno,
  218. boost::asio::error::get_system_category());
  219. on_immediate(op, is_continuation, immediate_arg);
  220. return;
  221. }
  222. }
  223. }
  224. else
  225. {
  226. if (descriptor_data->num_kevents_ < num_kevents[op_type])
  227. descriptor_data->num_kevents_ = num_kevents[op_type];
  228. struct kevent events[2];
  229. BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, EVFILT_READ,
  230. EV_ADD | EV_CLEAR, 0, 0, descriptor_data);
  231. BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, EVFILT_WRITE,
  232. EV_ADD | EV_CLEAR, 0, 0, descriptor_data);
  233. ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0);
  234. }
  235. }
  236. descriptor_data->op_queue_[op_type].push(op);
  237. scheduler_.work_started();
  238. }
  239. void kqueue_reactor::cancel_ops(socket_type,
  240. kqueue_reactor::per_descriptor_data& descriptor_data)
  241. {
  242. if (!descriptor_data)
  243. return;
  244. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  245. op_queue<operation> ops;
  246. for (int i = 0; i < max_ops; ++i)
  247. {
  248. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  249. {
  250. op->ec_ = boost::asio::error::operation_aborted;
  251. descriptor_data->op_queue_[i].pop();
  252. ops.push(op);
  253. }
  254. }
  255. descriptor_lock.unlock();
  256. scheduler_.post_deferred_completions(ops);
  257. }
  258. void kqueue_reactor::cancel_ops_by_key(socket_type,
  259. kqueue_reactor::per_descriptor_data& descriptor_data,
  260. int op_type, void* cancellation_key)
  261. {
  262. if (!descriptor_data)
  263. return;
  264. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  265. op_queue<operation> ops;
  266. op_queue<reactor_op> other_ops;
  267. while (reactor_op* op = descriptor_data->op_queue_[op_type].front())
  268. {
  269. descriptor_data->op_queue_[op_type].pop();
  270. if (op->cancellation_key_ == cancellation_key)
  271. {
  272. op->ec_ = boost::asio::error::operation_aborted;
  273. ops.push(op);
  274. }
  275. else
  276. other_ops.push(op);
  277. }
  278. descriptor_data->op_queue_[op_type].push(other_ops);
  279. descriptor_lock.unlock();
  280. scheduler_.post_deferred_completions(ops);
  281. }
  282. void kqueue_reactor::deregister_descriptor(socket_type descriptor,
  283. kqueue_reactor::per_descriptor_data& descriptor_data, bool closing)
  284. {
  285. if (!descriptor_data)
  286. return;
  287. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  288. if (!descriptor_data->shutdown_)
  289. {
  290. if (closing)
  291. {
  292. // The descriptor will be automatically removed from the kqueue when it
  293. // is closed.
  294. }
  295. else
  296. {
  297. struct kevent events[2];
  298. BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor,
  299. EVFILT_READ, EV_DELETE, 0, 0, 0);
  300. BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor,
  301. EVFILT_WRITE, EV_DELETE, 0, 0, 0);
  302. ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0);
  303. }
  304. op_queue<operation> ops;
  305. for (int i = 0; i < max_ops; ++i)
  306. {
  307. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  308. {
  309. op->ec_ = boost::asio::error::operation_aborted;
  310. descriptor_data->op_queue_[i].pop();
  311. ops.push(op);
  312. }
  313. }
  314. descriptor_data->descriptor_ = -1;
  315. descriptor_data->shutdown_ = true;
  316. descriptor_lock.unlock();
  317. BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION((
  318. context(), static_cast<uintmax_t>(descriptor),
  319. reinterpret_cast<uintmax_t>(descriptor_data)));
  320. scheduler_.post_deferred_completions(ops);
  321. // Leave descriptor_data set so that it will be freed by the subsequent
  322. // call to cleanup_descriptor_data.
  323. }
  324. else
  325. {
  326. // We are shutting down, so prevent cleanup_descriptor_data from freeing
  327. // the descriptor_data object and let the destructor free it instead.
  328. descriptor_data = 0;
  329. }
  330. }
  331. void kqueue_reactor::deregister_internal_descriptor(socket_type descriptor,
  332. kqueue_reactor::per_descriptor_data& descriptor_data)
  333. {
  334. if (!descriptor_data)
  335. return;
  336. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  337. if (!descriptor_data->shutdown_)
  338. {
  339. struct kevent events[2];
  340. BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor,
  341. EVFILT_READ, EV_DELETE, 0, 0, 0);
  342. BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor,
  343. EVFILT_WRITE, EV_DELETE, 0, 0, 0);
  344. ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0);
  345. op_queue<operation> ops;
  346. for (int i = 0; i < max_ops; ++i)
  347. ops.push(descriptor_data->op_queue_[i]);
  348. descriptor_data->descriptor_ = -1;
  349. descriptor_data->shutdown_ = true;
  350. descriptor_lock.unlock();
  351. BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION((
  352. context(), static_cast<uintmax_t>(descriptor),
  353. reinterpret_cast<uintmax_t>(descriptor_data)));
  354. // Leave descriptor_data set so that it will be freed by the subsequent
  355. // call to cleanup_descriptor_data.
  356. }
  357. else
  358. {
  359. // We are shutting down, so prevent cleanup_descriptor_data from freeing
  360. // the descriptor_data object and let the destructor free it instead.
  361. descriptor_data = 0;
  362. }
  363. }
  364. void kqueue_reactor::cleanup_descriptor_data(
  365. per_descriptor_data& descriptor_data)
  366. {
  367. if (descriptor_data)
  368. {
  369. free_descriptor_state(descriptor_data);
  370. descriptor_data = 0;
  371. }
  372. }
  373. void kqueue_reactor::run(long usec, op_queue<operation>& ops)
  374. {
  375. mutex::scoped_lock lock(mutex_);
  376. // Determine how long to block while waiting for events.
  377. timespec timeout_buf = { 0, 0 };
  378. timespec* timeout = usec ? get_timeout(usec, timeout_buf) : &timeout_buf;
  379. lock.unlock();
  380. // Block on the kqueue descriptor.
  381. struct kevent events[128];
  382. int num_events = kevent(kqueue_fd_, 0, 0, events, 128, timeout);
  383. if (num_events > 0)
  384. (void)ref_count_read_acquire(allocation_counter_);
  385. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  386. // Trace the waiting events.
  387. for (int i = 0; i < num_events; ++i)
  388. {
  389. void* ptr = reinterpret_cast<void*>(events[i].udata);
  390. if (ptr != &interrupter_)
  391. {
  392. unsigned event_mask = 0;
  393. switch (events[i].filter)
  394. {
  395. case EVFILT_READ:
  396. event_mask |= BOOST_ASIO_HANDLER_REACTOR_READ_EVENT;
  397. break;
  398. case EVFILT_WRITE:
  399. event_mask |= BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT;
  400. break;
  401. }
  402. if ((events[i].flags & (EV_ERROR | EV_OOBAND)) != 0)
  403. event_mask |= BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT;
  404. BOOST_ASIO_HANDLER_REACTOR_EVENTS((context(),
  405. reinterpret_cast<uintmax_t>(ptr), event_mask));
  406. }
  407. }
  408. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  409. // Dispatch the waiting events.
  410. for (int i = 0; i < num_events; ++i)
  411. {
  412. void* ptr = reinterpret_cast<void*>(events[i].udata);
  413. if (ptr == &interrupter_)
  414. {
  415. interrupter_.reset();
  416. }
  417. else
  418. {
  419. descriptor_state* descriptor_data = static_cast<descriptor_state*>(ptr);
  420. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  421. if (events[i].filter == EVFILT_WRITE
  422. && descriptor_data->num_kevents_ == 2
  423. && descriptor_data->op_queue_[write_op].empty())
  424. {
  425. // Some descriptor types, like serial ports, don't seem to support
  426. // EV_CLEAR with EVFILT_WRITE. Since we have no pending write
  427. // operations we'll remove the EVFILT_WRITE registration here so that
  428. // we don't end up in a tight spin.
  429. struct kevent delete_events[1];
  430. BOOST_ASIO_KQUEUE_EV_SET(&delete_events[0],
  431. descriptor_data->descriptor_, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
  432. ::kevent(kqueue_fd_, delete_events, 1, 0, 0, 0);
  433. descriptor_data->num_kevents_ = 1;
  434. }
  435. // Exception operations must be processed first to ensure that any
  436. // out-of-band data is read before normal data.
  437. #if defined(__NetBSD__)
  438. static const unsigned int filter[max_ops] =
  439. #else
  440. static const int filter[max_ops] =
  441. #endif
  442. { EVFILT_READ, EVFILT_WRITE, EVFILT_READ };
  443. for (int j = max_ops - 1; j >= 0; --j)
  444. {
  445. if (events[i].filter == filter[j])
  446. {
  447. if (j != except_op || events[i].flags & EV_OOBAND)
  448. {
  449. while (reactor_op* op = descriptor_data->op_queue_[j].front())
  450. {
  451. if (events[i].flags & EV_ERROR)
  452. {
  453. op->ec_ = boost::system::error_code(
  454. static_cast<int>(events[i].data),
  455. boost::asio::error::get_system_category());
  456. descriptor_data->op_queue_[j].pop();
  457. ops.push(op);
  458. }
  459. if (op->perform())
  460. {
  461. descriptor_data->op_queue_[j].pop();
  462. ops.push(op);
  463. }
  464. else
  465. break;
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. lock.lock();
  473. timer_queues_.get_ready_timers(ops);
  474. }
  475. void kqueue_reactor::interrupt()
  476. {
  477. interrupter_.interrupt();
  478. }
  479. int kqueue_reactor::do_kqueue_create()
  480. {
  481. int fd = ::kqueue();
  482. if (fd == -1)
  483. {
  484. boost::system::error_code ec(errno,
  485. boost::asio::error::get_system_category());
  486. boost::asio::detail::throw_error(ec, "kqueue");
  487. }
  488. return fd;
  489. }
  490. kqueue_reactor::descriptor_state* kqueue_reactor::allocate_descriptor_state()
  491. {
  492. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  493. auto* s = registered_descriptors_.alloc(io_locking_, io_locking_spin_count_);
  494. ref_count_up_release(allocation_counter_);
  495. return s;
  496. }
  497. void kqueue_reactor::free_descriptor_state(kqueue_reactor::descriptor_state* s)
  498. {
  499. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  500. registered_descriptors_.free(s);
  501. }
  502. void kqueue_reactor::do_add_timer_queue(timer_queue_base& queue)
  503. {
  504. mutex::scoped_lock lock(mutex_);
  505. timer_queues_.insert(&queue);
  506. }
  507. void kqueue_reactor::do_remove_timer_queue(timer_queue_base& queue)
  508. {
  509. mutex::scoped_lock lock(mutex_);
  510. timer_queues_.erase(&queue);
  511. }
  512. timespec* kqueue_reactor::get_timeout(long usec, timespec& ts)
  513. {
  514. // By default we will wait no longer than 5 minutes. This will ensure that
  515. // any changes to the system clock are detected after no longer than this.
  516. const long max_usec = 5 * 60 * 1000 * 1000;
  517. usec = timer_queues_.wait_duration_usec(
  518. (usec < 0 || max_usec < usec) ? max_usec : usec);
  519. ts.tv_sec = usec / 1000000;
  520. ts.tv_nsec = (usec % 1000000) * 1000;
  521. return &ts;
  522. }
  523. } // namespace detail
  524. } // namespace asio
  525. } // namespace boost
  526. #undef BOOST_ASIO_KQUEUE_EV_SET
  527. #include <boost/asio/detail/pop_options.hpp>
  528. #endif // defined(BOOST_ASIO_HAS_KQUEUE)
  529. #endif // BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_IPP