epoll_reactor.ipp 22 KB

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