epoll_reactor.ipp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. //
  2. // detail/impl/epoll_reactor.ipp
  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_IMPL_EPOLL_REACTOR_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_EPOLL_REACTOR_IPP
  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. #if defined(BOOST_ASIO_HAS_EPOLL)
  17. #include <cstddef>
  18. #include <sys/epoll.h>
  19. #include <boost/asio/detail/epoll_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(BOOST_ASIO_HAS_TIMERFD)
  24. # include <sys/timerfd.h>
  25. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. epoll_reactor::epoll_reactor(boost::asio::execution_context& ctx)
  31. : execution_context_service_base<epoll_reactor>(ctx),
  32. scheduler_(use_service<scheduler>(ctx)),
  33. mutex_(BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING(
  34. REACTOR_REGISTRATION, scheduler_.concurrency_hint())),
  35. interrupter_(),
  36. epoll_fd_(do_epoll_create()),
  37. timer_fd_(do_timerfd_create()),
  38. shutdown_(false),
  39. registered_descriptors_mutex_(mutex_.enabled())
  40. {
  41. // Add the interrupter's descriptor to epoll.
  42. epoll_event ev = { 0, { 0 } };
  43. ev.events = EPOLLIN | EPOLLERR | EPOLLET;
  44. ev.data.ptr = &interrupter_;
  45. epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, interrupter_.read_descriptor(), &ev);
  46. interrupter_.interrupt();
  47. // Add the timer descriptor to epoll.
  48. if (timer_fd_ != -1)
  49. {
  50. ev.events = EPOLLIN | EPOLLERR;
  51. ev.data.ptr = &timer_fd_;
  52. epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, timer_fd_, &ev);
  53. }
  54. }
  55. epoll_reactor::~epoll_reactor()
  56. {
  57. if (epoll_fd_ != -1)
  58. close(epoll_fd_);
  59. if (timer_fd_ != -1)
  60. close(timer_fd_);
  61. }
  62. void epoll_reactor::shutdown()
  63. {
  64. mutex::scoped_lock lock(mutex_);
  65. shutdown_ = true;
  66. lock.unlock();
  67. op_queue<operation> ops;
  68. while (descriptor_state* state = registered_descriptors_.first())
  69. {
  70. for (int i = 0; i < max_ops; ++i)
  71. ops.push(state->op_queue_[i]);
  72. state->shutdown_ = true;
  73. registered_descriptors_.free(state);
  74. }
  75. timer_queues_.get_all_timers(ops);
  76. scheduler_.abandon_operations(ops);
  77. }
  78. void epoll_reactor::notify_fork(
  79. boost::asio::execution_context::fork_event fork_ev)
  80. {
  81. if (fork_ev == boost::asio::execution_context::fork_child)
  82. {
  83. if (epoll_fd_ != -1)
  84. ::close(epoll_fd_);
  85. epoll_fd_ = -1;
  86. epoll_fd_ = do_epoll_create();
  87. if (timer_fd_ != -1)
  88. ::close(timer_fd_);
  89. timer_fd_ = -1;
  90. timer_fd_ = do_timerfd_create();
  91. interrupter_.recreate();
  92. // Add the interrupter's descriptor to epoll.
  93. epoll_event ev = { 0, { 0 } };
  94. ev.events = EPOLLIN | EPOLLERR | EPOLLET;
  95. ev.data.ptr = &interrupter_;
  96. epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, interrupter_.read_descriptor(), &ev);
  97. interrupter_.interrupt();
  98. // Add the timer descriptor to epoll.
  99. if (timer_fd_ != -1)
  100. {
  101. ev.events = EPOLLIN | EPOLLERR;
  102. ev.data.ptr = &timer_fd_;
  103. epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, timer_fd_, &ev);
  104. }
  105. update_timeout();
  106. // Re-register all descriptors with epoll.
  107. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  108. for (descriptor_state* state = registered_descriptors_.first();
  109. state != 0; state = state->next_)
  110. {
  111. ev.events = state->registered_events_;
  112. ev.data.ptr = state;
  113. int result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, state->descriptor_, &ev);
  114. if (result != 0)
  115. {
  116. boost::system::error_code ec(errno,
  117. boost::asio::error::get_system_category());
  118. boost::asio::detail::throw_error(ec, "epoll re-registration");
  119. }
  120. }
  121. }
  122. }
  123. void epoll_reactor::init_task()
  124. {
  125. scheduler_.init_task();
  126. }
  127. int epoll_reactor::register_descriptor(socket_type descriptor,
  128. epoll_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. {
  135. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  136. descriptor_data->reactor_ = this;
  137. descriptor_data->descriptor_ = descriptor;
  138. descriptor_data->shutdown_ = false;
  139. for (int i = 0; i < max_ops; ++i)
  140. descriptor_data->try_speculative_[i] = true;
  141. }
  142. epoll_event ev = { 0, { 0 } };
  143. ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLPRI | EPOLLET;
  144. descriptor_data->registered_events_ = ev.events;
  145. ev.data.ptr = descriptor_data;
  146. int result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
  147. if (result != 0)
  148. {
  149. if (errno == EPERM)
  150. {
  151. // This file descriptor type is not supported by epoll. However, if it is
  152. // a regular file then operations on it will not block. We will allow
  153. // this descriptor to be used and fail later if an operation on it would
  154. // otherwise require a trip through the reactor.
  155. descriptor_data->registered_events_ = 0;
  156. return 0;
  157. }
  158. return errno;
  159. }
  160. return 0;
  161. }
  162. int epoll_reactor::register_internal_descriptor(
  163. int op_type, socket_type descriptor,
  164. epoll_reactor::per_descriptor_data& descriptor_data, reactor_op* op)
  165. {
  166. descriptor_data = allocate_descriptor_state();
  167. BOOST_ASIO_HANDLER_REACTOR_REGISTRATION((
  168. context(), static_cast<uintmax_t>(descriptor),
  169. reinterpret_cast<uintmax_t>(descriptor_data)));
  170. {
  171. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  172. descriptor_data->reactor_ = this;
  173. descriptor_data->descriptor_ = descriptor;
  174. descriptor_data->shutdown_ = false;
  175. descriptor_data->op_queue_[op_type].push(op);
  176. for (int i = 0; i < max_ops; ++i)
  177. descriptor_data->try_speculative_[i] = true;
  178. }
  179. epoll_event ev = { 0, { 0 } };
  180. ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLPRI | EPOLLET;
  181. descriptor_data->registered_events_ = ev.events;
  182. ev.data.ptr = descriptor_data;
  183. int result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
  184. if (result != 0)
  185. return errno;
  186. return 0;
  187. }
  188. void epoll_reactor::move_descriptor(socket_type,
  189. epoll_reactor::per_descriptor_data& target_descriptor_data,
  190. epoll_reactor::per_descriptor_data& source_descriptor_data)
  191. {
  192. target_descriptor_data = source_descriptor_data;
  193. source_descriptor_data = 0;
  194. }
  195. void epoll_reactor::call_post_immediate_completion(
  196. operation* op, bool is_continuation, const void* self)
  197. {
  198. static_cast<const epoll_reactor*>(self)->post_immediate_completion(
  199. op, is_continuation);
  200. }
  201. void epoll_reactor::start_op(int op_type, socket_type descriptor,
  202. epoll_reactor::per_descriptor_data& descriptor_data, reactor_op* op,
  203. bool is_continuation, bool allow_speculative,
  204. void (*on_immediate)(operation*, bool, const void*),
  205. const void* immediate_arg)
  206. {
  207. if (!descriptor_data)
  208. {
  209. op->ec_ = boost::asio::error::bad_descriptor;
  210. on_immediate(op, is_continuation, immediate_arg);
  211. return;
  212. }
  213. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  214. if (descriptor_data->shutdown_)
  215. {
  216. on_immediate(op, is_continuation, immediate_arg);
  217. return;
  218. }
  219. if (descriptor_data->op_queue_[op_type].empty())
  220. {
  221. if (allow_speculative
  222. && (op_type != read_op
  223. || descriptor_data->op_queue_[except_op].empty()))
  224. {
  225. if (descriptor_data->try_speculative_[op_type])
  226. {
  227. if (reactor_op::status status = op->perform())
  228. {
  229. if (status == reactor_op::done_and_exhausted)
  230. if (descriptor_data->registered_events_ != 0)
  231. descriptor_data->try_speculative_[op_type] = false;
  232. descriptor_lock.unlock();
  233. on_immediate(op, is_continuation, immediate_arg);
  234. return;
  235. }
  236. }
  237. if (descriptor_data->registered_events_ == 0)
  238. {
  239. op->ec_ = boost::asio::error::operation_not_supported;
  240. on_immediate(op, is_continuation, immediate_arg);
  241. return;
  242. }
  243. if (op_type == write_op)
  244. {
  245. if ((descriptor_data->registered_events_ & EPOLLOUT) == 0)
  246. {
  247. epoll_event ev = { 0, { 0 } };
  248. ev.events = descriptor_data->registered_events_ | EPOLLOUT;
  249. ev.data.ptr = descriptor_data;
  250. if (epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev) == 0)
  251. {
  252. descriptor_data->registered_events_ |= ev.events;
  253. }
  254. else
  255. {
  256. op->ec_ = boost::system::error_code(errno,
  257. boost::asio::error::get_system_category());
  258. on_immediate(op, is_continuation, immediate_arg);
  259. return;
  260. }
  261. }
  262. }
  263. }
  264. else if (descriptor_data->registered_events_ == 0)
  265. {
  266. op->ec_ = boost::asio::error::operation_not_supported;
  267. on_immediate(op, is_continuation, immediate_arg);
  268. return;
  269. }
  270. else
  271. {
  272. if (op_type == write_op)
  273. {
  274. descriptor_data->registered_events_ |= EPOLLOUT;
  275. }
  276. epoll_event ev = { 0, { 0 } };
  277. ev.events = descriptor_data->registered_events_;
  278. ev.data.ptr = descriptor_data;
  279. epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
  280. }
  281. }
  282. descriptor_data->op_queue_[op_type].push(op);
  283. scheduler_.work_started();
  284. }
  285. void epoll_reactor::cancel_ops(socket_type,
  286. epoll_reactor::per_descriptor_data& descriptor_data)
  287. {
  288. if (!descriptor_data)
  289. return;
  290. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  291. op_queue<operation> ops;
  292. for (int i = 0; i < max_ops; ++i)
  293. {
  294. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  295. {
  296. op->ec_ = boost::asio::error::operation_aborted;
  297. descriptor_data->op_queue_[i].pop();
  298. ops.push(op);
  299. }
  300. }
  301. descriptor_lock.unlock();
  302. scheduler_.post_deferred_completions(ops);
  303. }
  304. void epoll_reactor::cancel_ops_by_key(socket_type,
  305. epoll_reactor::per_descriptor_data& descriptor_data,
  306. int op_type, void* cancellation_key)
  307. {
  308. if (!descriptor_data)
  309. return;
  310. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  311. op_queue<operation> ops;
  312. op_queue<reactor_op> other_ops;
  313. while (reactor_op* op = descriptor_data->op_queue_[op_type].front())
  314. {
  315. descriptor_data->op_queue_[op_type].pop();
  316. if (op->cancellation_key_ == cancellation_key)
  317. {
  318. op->ec_ = boost::asio::error::operation_aborted;
  319. ops.push(op);
  320. }
  321. else
  322. other_ops.push(op);
  323. }
  324. descriptor_data->op_queue_[op_type].push(other_ops);
  325. descriptor_lock.unlock();
  326. scheduler_.post_deferred_completions(ops);
  327. }
  328. void epoll_reactor::deregister_descriptor(socket_type descriptor,
  329. epoll_reactor::per_descriptor_data& descriptor_data, bool closing)
  330. {
  331. if (!descriptor_data)
  332. return;
  333. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  334. if (!descriptor_data->shutdown_)
  335. {
  336. if (closing)
  337. {
  338. // The descriptor will be automatically removed from the epoll set when
  339. // it is closed.
  340. }
  341. else if (descriptor_data->registered_events_ != 0)
  342. {
  343. epoll_event ev = { 0, { 0 } };
  344. epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, descriptor, &ev);
  345. }
  346. op_queue<operation> ops;
  347. for (int i = 0; i < max_ops; ++i)
  348. {
  349. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  350. {
  351. op->ec_ = boost::asio::error::operation_aborted;
  352. descriptor_data->op_queue_[i].pop();
  353. ops.push(op);
  354. }
  355. }
  356. descriptor_data->descriptor_ = -1;
  357. descriptor_data->shutdown_ = true;
  358. descriptor_lock.unlock();
  359. BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION((
  360. context(), static_cast<uintmax_t>(descriptor),
  361. reinterpret_cast<uintmax_t>(descriptor_data)));
  362. scheduler_.post_deferred_completions(ops);
  363. // Leave descriptor_data set so that it will be freed by the subsequent
  364. // call to cleanup_descriptor_data.
  365. }
  366. else
  367. {
  368. // We are shutting down, so prevent cleanup_descriptor_data from freeing
  369. // the descriptor_data object and let the destructor free it instead.
  370. descriptor_data = 0;
  371. }
  372. }
  373. void epoll_reactor::deregister_internal_descriptor(socket_type descriptor,
  374. epoll_reactor::per_descriptor_data& descriptor_data)
  375. {
  376. if (!descriptor_data)
  377. return;
  378. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  379. if (!descriptor_data->shutdown_)
  380. {
  381. epoll_event ev = { 0, { 0 } };
  382. epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, descriptor, &ev);
  383. op_queue<operation> ops;
  384. for (int i = 0; i < max_ops; ++i)
  385. ops.push(descriptor_data->op_queue_[i]);
  386. descriptor_data->descriptor_ = -1;
  387. descriptor_data->shutdown_ = true;
  388. descriptor_lock.unlock();
  389. BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION((
  390. context(), static_cast<uintmax_t>(descriptor),
  391. reinterpret_cast<uintmax_t>(descriptor_data)));
  392. // Leave descriptor_data set so that it will be freed by the subsequent
  393. // call to cleanup_descriptor_data.
  394. }
  395. else
  396. {
  397. // We are shutting down, so prevent cleanup_descriptor_data from freeing
  398. // the descriptor_data object and let the destructor free it instead.
  399. descriptor_data = 0;
  400. }
  401. }
  402. void epoll_reactor::cleanup_descriptor_data(
  403. per_descriptor_data& descriptor_data)
  404. {
  405. if (descriptor_data)
  406. {
  407. free_descriptor_state(descriptor_data);
  408. descriptor_data = 0;
  409. }
  410. }
  411. void epoll_reactor::run(long usec, op_queue<operation>& ops)
  412. {
  413. // This code relies on the fact that the scheduler queues the reactor task
  414. // behind all descriptor operations generated by this function. This means,
  415. // that by the time we reach this point, any previously returned descriptor
  416. // operations have already been dequeued. Therefore it is now safe for us to
  417. // reuse and return them for the scheduler to queue again.
  418. // Calculate timeout. Check the timer queues only if timerfd is not in use.
  419. int timeout;
  420. if (usec == 0)
  421. timeout = 0;
  422. else
  423. {
  424. timeout = (usec < 0) ? -1 : ((usec - 1) / 1000 + 1);
  425. if (timer_fd_ == -1)
  426. {
  427. mutex::scoped_lock lock(mutex_);
  428. timeout = get_timeout(timeout);
  429. }
  430. }
  431. // Block on the epoll descriptor.
  432. epoll_event events[128];
  433. int num_events = epoll_wait(epoll_fd_, events, 128, timeout);
  434. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  435. // Trace the waiting events.
  436. for (int i = 0; i < num_events; ++i)
  437. {
  438. void* ptr = events[i].data.ptr;
  439. if (ptr == &interrupter_)
  440. {
  441. // Ignore.
  442. }
  443. # if defined(BOOST_ASIO_HAS_TIMERFD)
  444. else if (ptr == &timer_fd_)
  445. {
  446. // Ignore.
  447. }
  448. # endif // defined(BOOST_ASIO_HAS_TIMERFD)
  449. else
  450. {
  451. unsigned event_mask = 0;
  452. if ((events[i].events & EPOLLIN) != 0)
  453. event_mask |= BOOST_ASIO_HANDLER_REACTOR_READ_EVENT;
  454. if ((events[i].events & EPOLLOUT))
  455. event_mask |= BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT;
  456. if ((events[i].events & (EPOLLERR | EPOLLHUP)) != 0)
  457. event_mask |= BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT;
  458. BOOST_ASIO_HANDLER_REACTOR_EVENTS((context(),
  459. reinterpret_cast<uintmax_t>(ptr), event_mask));
  460. }
  461. }
  462. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  463. #if defined(BOOST_ASIO_HAS_TIMERFD)
  464. bool check_timers = (timer_fd_ == -1);
  465. #else // defined(BOOST_ASIO_HAS_TIMERFD)
  466. bool check_timers = true;
  467. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  468. // Dispatch the waiting events.
  469. for (int i = 0; i < num_events; ++i)
  470. {
  471. void* ptr = events[i].data.ptr;
  472. if (ptr == &interrupter_)
  473. {
  474. // No need to reset the interrupter since we're leaving the descriptor
  475. // in a ready-to-read state and relying on edge-triggered notifications
  476. // to make it so that we only get woken up when the descriptor's epoll
  477. // registration is updated.
  478. #if defined(BOOST_ASIO_HAS_TIMERFD)
  479. if (timer_fd_ == -1)
  480. check_timers = true;
  481. #else // defined(BOOST_ASIO_HAS_TIMERFD)
  482. check_timers = true;
  483. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  484. }
  485. #if defined(BOOST_ASIO_HAS_TIMERFD)
  486. else if (ptr == &timer_fd_)
  487. {
  488. check_timers = true;
  489. }
  490. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  491. else
  492. {
  493. // The descriptor operation doesn't count as work in and of itself, so we
  494. // don't call work_started() here. This still allows the scheduler to
  495. // stop if the only remaining operations are descriptor operations.
  496. descriptor_state* descriptor_data = static_cast<descriptor_state*>(ptr);
  497. if (!ops.is_enqueued(descriptor_data))
  498. {
  499. descriptor_data->set_ready_events(events[i].events);
  500. ops.push(descriptor_data);
  501. }
  502. else
  503. {
  504. descriptor_data->add_ready_events(events[i].events);
  505. }
  506. }
  507. }
  508. if (check_timers)
  509. {
  510. mutex::scoped_lock common_lock(mutex_);
  511. timer_queues_.get_ready_timers(ops);
  512. #if defined(BOOST_ASIO_HAS_TIMERFD)
  513. if (timer_fd_ != -1)
  514. {
  515. itimerspec new_timeout;
  516. itimerspec old_timeout;
  517. int flags = get_timeout(new_timeout);
  518. timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
  519. }
  520. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  521. }
  522. }
  523. void epoll_reactor::interrupt()
  524. {
  525. epoll_event ev = { 0, { 0 } };
  526. ev.events = EPOLLIN | EPOLLERR | EPOLLET;
  527. ev.data.ptr = &interrupter_;
  528. epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, interrupter_.read_descriptor(), &ev);
  529. }
  530. int epoll_reactor::do_epoll_create()
  531. {
  532. #if defined(EPOLL_CLOEXEC)
  533. int fd = epoll_create1(EPOLL_CLOEXEC);
  534. #else // defined(EPOLL_CLOEXEC)
  535. int fd = -1;
  536. errno = EINVAL;
  537. #endif // defined(EPOLL_CLOEXEC)
  538. if (fd == -1 && (errno == EINVAL || errno == ENOSYS))
  539. {
  540. fd = epoll_create(epoll_size);
  541. if (fd != -1)
  542. ::fcntl(fd, F_SETFD, FD_CLOEXEC);
  543. }
  544. if (fd == -1)
  545. {
  546. boost::system::error_code ec(errno,
  547. boost::asio::error::get_system_category());
  548. boost::asio::detail::throw_error(ec, "epoll");
  549. }
  550. return fd;
  551. }
  552. int epoll_reactor::do_timerfd_create()
  553. {
  554. #if defined(BOOST_ASIO_HAS_TIMERFD)
  555. # if defined(TFD_CLOEXEC)
  556. int fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
  557. # else // defined(TFD_CLOEXEC)
  558. int fd = -1;
  559. errno = EINVAL;
  560. # endif // defined(TFD_CLOEXEC)
  561. if (fd == -1 && errno == EINVAL)
  562. {
  563. fd = timerfd_create(CLOCK_MONOTONIC, 0);
  564. if (fd != -1)
  565. ::fcntl(fd, F_SETFD, FD_CLOEXEC);
  566. }
  567. return fd;
  568. #else // defined(BOOST_ASIO_HAS_TIMERFD)
  569. return -1;
  570. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  571. }
  572. epoll_reactor::descriptor_state* epoll_reactor::allocate_descriptor_state()
  573. {
  574. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  575. return registered_descriptors_.alloc(BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING(
  576. REACTOR_IO, scheduler_.concurrency_hint()));
  577. }
  578. void epoll_reactor::free_descriptor_state(epoll_reactor::descriptor_state* s)
  579. {
  580. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  581. registered_descriptors_.free(s);
  582. }
  583. void epoll_reactor::do_add_timer_queue(timer_queue_base& queue)
  584. {
  585. mutex::scoped_lock lock(mutex_);
  586. timer_queues_.insert(&queue);
  587. }
  588. void epoll_reactor::do_remove_timer_queue(timer_queue_base& queue)
  589. {
  590. mutex::scoped_lock lock(mutex_);
  591. timer_queues_.erase(&queue);
  592. }
  593. void epoll_reactor::update_timeout()
  594. {
  595. #if defined(BOOST_ASIO_HAS_TIMERFD)
  596. if (timer_fd_ != -1)
  597. {
  598. itimerspec new_timeout;
  599. itimerspec old_timeout;
  600. int flags = get_timeout(new_timeout);
  601. timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
  602. return;
  603. }
  604. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  605. interrupt();
  606. }
  607. int epoll_reactor::get_timeout(int msec)
  608. {
  609. // By default we will wait no longer than 5 minutes. This will ensure that
  610. // any changes to the system clock are detected after no longer than this.
  611. const int max_msec = 5 * 60 * 1000;
  612. return timer_queues_.wait_duration_msec(
  613. (msec < 0 || max_msec < msec) ? max_msec : msec);
  614. }
  615. #if defined(BOOST_ASIO_HAS_TIMERFD)
  616. int epoll_reactor::get_timeout(itimerspec& ts)
  617. {
  618. ts.it_interval.tv_sec = 0;
  619. ts.it_interval.tv_nsec = 0;
  620. long usec = timer_queues_.wait_duration_usec(5 * 60 * 1000 * 1000);
  621. ts.it_value.tv_sec = usec / 1000000;
  622. ts.it_value.tv_nsec = usec ? (usec % 1000000) * 1000 : 1;
  623. return usec ? 0 : TFD_TIMER_ABSTIME;
  624. }
  625. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  626. struct epoll_reactor::perform_io_cleanup_on_block_exit
  627. {
  628. explicit perform_io_cleanup_on_block_exit(epoll_reactor* r)
  629. : reactor_(r), first_op_(0)
  630. {
  631. }
  632. ~perform_io_cleanup_on_block_exit()
  633. {
  634. if (first_op_)
  635. {
  636. // Post the remaining completed operations for invocation.
  637. if (!ops_.empty())
  638. reactor_->scheduler_.post_deferred_completions(ops_);
  639. // A user-initiated operation has completed, but there's no need to
  640. // explicitly call work_finished() here. Instead, we'll take advantage of
  641. // the fact that the scheduler will call work_finished() once we return.
  642. }
  643. else
  644. {
  645. // No user-initiated operations have completed, so we need to compensate
  646. // for the work_finished() call that the scheduler will make once this
  647. // operation returns.
  648. reactor_->scheduler_.compensating_work_started();
  649. }
  650. }
  651. epoll_reactor* reactor_;
  652. op_queue<operation> ops_;
  653. operation* first_op_;
  654. };
  655. epoll_reactor::descriptor_state::descriptor_state(bool locking)
  656. : operation(&epoll_reactor::descriptor_state::do_complete),
  657. mutex_(locking)
  658. {
  659. }
  660. operation* epoll_reactor::descriptor_state::perform_io(uint32_t events)
  661. {
  662. mutex_.lock();
  663. perform_io_cleanup_on_block_exit io_cleanup(reactor_);
  664. mutex::scoped_lock descriptor_lock(mutex_, mutex::scoped_lock::adopt_lock);
  665. // Exception operations must be processed first to ensure that any
  666. // out-of-band data is read before normal data.
  667. static const int flag[max_ops] = { EPOLLIN, EPOLLOUT, EPOLLPRI };
  668. for (int j = max_ops - 1; j >= 0; --j)
  669. {
  670. if (events & (flag[j] | EPOLLERR | EPOLLHUP))
  671. {
  672. try_speculative_[j] = true;
  673. while (reactor_op* op = op_queue_[j].front())
  674. {
  675. if (reactor_op::status status = op->perform())
  676. {
  677. op_queue_[j].pop();
  678. io_cleanup.ops_.push(op);
  679. if (status == reactor_op::done_and_exhausted)
  680. {
  681. try_speculative_[j] = false;
  682. break;
  683. }
  684. }
  685. else
  686. break;
  687. }
  688. }
  689. }
  690. // The first operation will be returned for completion now. The others will
  691. // be posted for later by the io_cleanup object's destructor.
  692. io_cleanup.first_op_ = io_cleanup.ops_.front();
  693. io_cleanup.ops_.pop();
  694. return io_cleanup.first_op_;
  695. }
  696. void epoll_reactor::descriptor_state::do_complete(
  697. void* owner, operation* base,
  698. const boost::system::error_code& ec, std::size_t bytes_transferred)
  699. {
  700. if (owner)
  701. {
  702. descriptor_state* descriptor_data = static_cast<descriptor_state*>(base);
  703. uint32_t events = static_cast<uint32_t>(bytes_transferred);
  704. if (operation* op = descriptor_data->perform_io(events))
  705. {
  706. op->complete(owner, ec, 0);
  707. }
  708. }
  709. }
  710. } // namespace detail
  711. } // namespace asio
  712. } // namespace boost
  713. #include <boost/asio/detail/pop_options.hpp>
  714. #endif // defined(BOOST_ASIO_HAS_EPOLL)
  715. #endif // BOOST_ASIO_DETAIL_IMPL_EPOLL_REACTOR_IPP