epoll_reactor.ipp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. //
  2. // detail/impl/epoll_reactor.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #if defined(BOOST_ASIO_HAS_TIMERFD)
  23. # include <sys/timerfd.h>
  24. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. epoll_reactor::epoll_reactor(boost::asio::execution_context& ctx)
  30. : execution_context_service_base<epoll_reactor>(ctx),
  31. scheduler_(use_service<scheduler>(ctx)),
  32. mutex_(BOOST_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. boost::asio::execution_context::fork_event fork_ev)
  79. {
  80. if (fork_ev == boost::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. boost::system::error_code ec(errno,
  116. boost::asio::error::get_system_category());
  117. boost::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. BOOST_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. BOOST_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::start_op(int op_type, socket_type descriptor,
  195. epoll_reactor::per_descriptor_data& descriptor_data, reactor_op* op,
  196. bool is_continuation, bool allow_speculative)
  197. {
  198. if (!descriptor_data)
  199. {
  200. op->ec_ = boost::asio::error::bad_descriptor;
  201. post_immediate_completion(op, is_continuation);
  202. return;
  203. }
  204. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  205. if (descriptor_data->shutdown_)
  206. {
  207. post_immediate_completion(op, is_continuation);
  208. return;
  209. }
  210. if (descriptor_data->op_queue_[op_type].empty())
  211. {
  212. if (allow_speculative
  213. && (op_type != read_op
  214. || descriptor_data->op_queue_[except_op].empty()))
  215. {
  216. if (descriptor_data->try_speculative_[op_type])
  217. {
  218. if (reactor_op::status status = op->perform())
  219. {
  220. if (status == reactor_op::done_and_exhausted)
  221. if (descriptor_data->registered_events_ != 0)
  222. descriptor_data->try_speculative_[op_type] = false;
  223. descriptor_lock.unlock();
  224. scheduler_.post_immediate_completion(op, is_continuation);
  225. return;
  226. }
  227. }
  228. if (descriptor_data->registered_events_ == 0)
  229. {
  230. op->ec_ = boost::asio::error::operation_not_supported;
  231. scheduler_.post_immediate_completion(op, is_continuation);
  232. return;
  233. }
  234. if (op_type == write_op)
  235. {
  236. if ((descriptor_data->registered_events_ & EPOLLOUT) == 0)
  237. {
  238. epoll_event ev = { 0, { 0 } };
  239. ev.events = descriptor_data->registered_events_ | EPOLLOUT;
  240. ev.data.ptr = descriptor_data;
  241. if (epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev) == 0)
  242. {
  243. descriptor_data->registered_events_ |= ev.events;
  244. }
  245. else
  246. {
  247. op->ec_ = boost::system::error_code(errno,
  248. boost::asio::error::get_system_category());
  249. scheduler_.post_immediate_completion(op, is_continuation);
  250. return;
  251. }
  252. }
  253. }
  254. }
  255. else if (descriptor_data->registered_events_ == 0)
  256. {
  257. op->ec_ = boost::asio::error::operation_not_supported;
  258. scheduler_.post_immediate_completion(op, is_continuation);
  259. return;
  260. }
  261. else
  262. {
  263. if (op_type == write_op)
  264. {
  265. descriptor_data->registered_events_ |= EPOLLOUT;
  266. }
  267. epoll_event ev = { 0, { 0 } };
  268. ev.events = descriptor_data->registered_events_;
  269. ev.data.ptr = descriptor_data;
  270. epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
  271. }
  272. }
  273. descriptor_data->op_queue_[op_type].push(op);
  274. scheduler_.work_started();
  275. }
  276. void epoll_reactor::cancel_ops(socket_type,
  277. epoll_reactor::per_descriptor_data& descriptor_data)
  278. {
  279. if (!descriptor_data)
  280. return;
  281. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  282. op_queue<operation> ops;
  283. for (int i = 0; i < max_ops; ++i)
  284. {
  285. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  286. {
  287. op->ec_ = boost::asio::error::operation_aborted;
  288. descriptor_data->op_queue_[i].pop();
  289. ops.push(op);
  290. }
  291. }
  292. descriptor_lock.unlock();
  293. scheduler_.post_deferred_completions(ops);
  294. }
  295. void epoll_reactor::deregister_descriptor(socket_type descriptor,
  296. epoll_reactor::per_descriptor_data& descriptor_data, bool closing)
  297. {
  298. if (!descriptor_data)
  299. return;
  300. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  301. if (!descriptor_data->shutdown_)
  302. {
  303. if (closing)
  304. {
  305. // The descriptor will be automatically removed from the epoll set when
  306. // it is closed.
  307. }
  308. else if (descriptor_data->registered_events_ != 0)
  309. {
  310. epoll_event ev = { 0, { 0 } };
  311. epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, descriptor, &ev);
  312. }
  313. op_queue<operation> ops;
  314. for (int i = 0; i < max_ops; ++i)
  315. {
  316. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  317. {
  318. op->ec_ = boost::asio::error::operation_aborted;
  319. descriptor_data->op_queue_[i].pop();
  320. ops.push(op);
  321. }
  322. }
  323. descriptor_data->descriptor_ = -1;
  324. descriptor_data->shutdown_ = true;
  325. descriptor_lock.unlock();
  326. BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION((
  327. context(), static_cast<uintmax_t>(descriptor),
  328. reinterpret_cast<uintmax_t>(descriptor_data)));
  329. scheduler_.post_deferred_completions(ops);
  330. // Leave descriptor_data set so that it will be freed by the subsequent
  331. // call to cleanup_descriptor_data.
  332. }
  333. else
  334. {
  335. // We are shutting down, so prevent cleanup_descriptor_data from freeing
  336. // the descriptor_data object and let the destructor free it instead.
  337. descriptor_data = 0;
  338. }
  339. }
  340. void epoll_reactor::deregister_internal_descriptor(socket_type descriptor,
  341. epoll_reactor::per_descriptor_data& descriptor_data)
  342. {
  343. if (!descriptor_data)
  344. return;
  345. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  346. if (!descriptor_data->shutdown_)
  347. {
  348. epoll_event ev = { 0, { 0 } };
  349. epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, descriptor, &ev);
  350. op_queue<operation> ops;
  351. for (int i = 0; i < max_ops; ++i)
  352. ops.push(descriptor_data->op_queue_[i]);
  353. descriptor_data->descriptor_ = -1;
  354. descriptor_data->shutdown_ = true;
  355. descriptor_lock.unlock();
  356. BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION((
  357. context(), static_cast<uintmax_t>(descriptor),
  358. reinterpret_cast<uintmax_t>(descriptor_data)));
  359. // Leave descriptor_data set so that it will be freed by the subsequent
  360. // call to cleanup_descriptor_data.
  361. }
  362. else
  363. {
  364. // We are shutting down, so prevent cleanup_descriptor_data from freeing
  365. // the descriptor_data object and let the destructor free it instead.
  366. descriptor_data = 0;
  367. }
  368. }
  369. void epoll_reactor::cleanup_descriptor_data(
  370. per_descriptor_data& descriptor_data)
  371. {
  372. if (descriptor_data)
  373. {
  374. free_descriptor_state(descriptor_data);
  375. descriptor_data = 0;
  376. }
  377. }
  378. void epoll_reactor::run(long usec, op_queue<operation>& ops)
  379. {
  380. // This code relies on the fact that the scheduler queues the reactor task
  381. // behind all descriptor operations generated by this function. This means,
  382. // that by the time we reach this point, any previously returned descriptor
  383. // operations have already been dequeued. Therefore it is now safe for us to
  384. // reuse and return them for the scheduler to queue again.
  385. // Calculate timeout. Check the timer queues only if timerfd is not in use.
  386. int timeout;
  387. if (usec == 0)
  388. timeout = 0;
  389. else
  390. {
  391. timeout = (usec < 0) ? -1 : ((usec - 1) / 1000 + 1);
  392. if (timer_fd_ == -1)
  393. {
  394. mutex::scoped_lock lock(mutex_);
  395. timeout = get_timeout(timeout);
  396. }
  397. }
  398. // Block on the epoll descriptor.
  399. epoll_event events[128];
  400. int num_events = epoll_wait(epoll_fd_, events, 128, timeout);
  401. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  402. // Trace the waiting events.
  403. for (int i = 0; i < num_events; ++i)
  404. {
  405. void* ptr = events[i].data.ptr;
  406. if (ptr == &interrupter_)
  407. {
  408. // Ignore.
  409. }
  410. # if defined(BOOST_ASIO_HAS_TIMERFD)
  411. else if (ptr == &timer_fd_)
  412. {
  413. // Ignore.
  414. }
  415. # endif // defined(BOOST_ASIO_HAS_TIMERFD)
  416. else
  417. {
  418. unsigned event_mask = 0;
  419. if ((events[i].events & EPOLLIN) != 0)
  420. event_mask |= BOOST_ASIO_HANDLER_REACTOR_READ_EVENT;
  421. if ((events[i].events & EPOLLOUT))
  422. event_mask |= BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT;
  423. if ((events[i].events & (EPOLLERR | EPOLLHUP)) != 0)
  424. event_mask |= BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT;
  425. BOOST_ASIO_HANDLER_REACTOR_EVENTS((context(),
  426. reinterpret_cast<uintmax_t>(ptr), event_mask));
  427. }
  428. }
  429. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  430. #if defined(BOOST_ASIO_HAS_TIMERFD)
  431. bool check_timers = (timer_fd_ == -1);
  432. #else // defined(BOOST_ASIO_HAS_TIMERFD)
  433. bool check_timers = true;
  434. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  435. // Dispatch 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. // No need to reset the interrupter since we're leaving the descriptor
  442. // in a ready-to-read state and relying on edge-triggered notifications
  443. // to make it so that we only get woken up when the descriptor's epoll
  444. // registration is updated.
  445. #if defined(BOOST_ASIO_HAS_TIMERFD)
  446. if (timer_fd_ == -1)
  447. check_timers = true;
  448. #else // defined(BOOST_ASIO_HAS_TIMERFD)
  449. check_timers = true;
  450. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  451. }
  452. #if defined(BOOST_ASIO_HAS_TIMERFD)
  453. else if (ptr == &timer_fd_)
  454. {
  455. check_timers = true;
  456. }
  457. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  458. else
  459. {
  460. // The descriptor operation doesn't count as work in and of itself, so we
  461. // don't call work_started() here. This still allows the scheduler to
  462. // stop if the only remaining operations are descriptor operations.
  463. descriptor_state* descriptor_data = static_cast<descriptor_state*>(ptr);
  464. if (!ops.is_enqueued(descriptor_data))
  465. {
  466. descriptor_data->set_ready_events(events[i].events);
  467. ops.push(descriptor_data);
  468. }
  469. else
  470. {
  471. descriptor_data->add_ready_events(events[i].events);
  472. }
  473. }
  474. }
  475. if (check_timers)
  476. {
  477. mutex::scoped_lock common_lock(mutex_);
  478. timer_queues_.get_ready_timers(ops);
  479. #if defined(BOOST_ASIO_HAS_TIMERFD)
  480. if (timer_fd_ != -1)
  481. {
  482. itimerspec new_timeout;
  483. itimerspec old_timeout;
  484. int flags = get_timeout(new_timeout);
  485. timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
  486. }
  487. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  488. }
  489. }
  490. void epoll_reactor::interrupt()
  491. {
  492. epoll_event ev = { 0, { 0 } };
  493. ev.events = EPOLLIN | EPOLLERR | EPOLLET;
  494. ev.data.ptr = &interrupter_;
  495. epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, interrupter_.read_descriptor(), &ev);
  496. }
  497. int epoll_reactor::do_epoll_create()
  498. {
  499. #if defined(EPOLL_CLOEXEC)
  500. int fd = epoll_create1(EPOLL_CLOEXEC);
  501. #else // defined(EPOLL_CLOEXEC)
  502. int fd = -1;
  503. errno = EINVAL;
  504. #endif // defined(EPOLL_CLOEXEC)
  505. if (fd == -1 && (errno == EINVAL || errno == ENOSYS))
  506. {
  507. fd = epoll_create(epoll_size);
  508. if (fd != -1)
  509. ::fcntl(fd, F_SETFD, FD_CLOEXEC);
  510. }
  511. if (fd == -1)
  512. {
  513. boost::system::error_code ec(errno,
  514. boost::asio::error::get_system_category());
  515. boost::asio::detail::throw_error(ec, "epoll");
  516. }
  517. return fd;
  518. }
  519. int epoll_reactor::do_timerfd_create()
  520. {
  521. #if defined(BOOST_ASIO_HAS_TIMERFD)
  522. # if defined(TFD_CLOEXEC)
  523. int fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
  524. # else // defined(TFD_CLOEXEC)
  525. int fd = -1;
  526. errno = EINVAL;
  527. # endif // defined(TFD_CLOEXEC)
  528. if (fd == -1 && errno == EINVAL)
  529. {
  530. fd = timerfd_create(CLOCK_MONOTONIC, 0);
  531. if (fd != -1)
  532. ::fcntl(fd, F_SETFD, FD_CLOEXEC);
  533. }
  534. return fd;
  535. #else // defined(BOOST_ASIO_HAS_TIMERFD)
  536. return -1;
  537. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  538. }
  539. epoll_reactor::descriptor_state* epoll_reactor::allocate_descriptor_state()
  540. {
  541. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  542. return registered_descriptors_.alloc(BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING(
  543. REACTOR_IO, scheduler_.concurrency_hint()));
  544. }
  545. void epoll_reactor::free_descriptor_state(epoll_reactor::descriptor_state* s)
  546. {
  547. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  548. registered_descriptors_.free(s);
  549. }
  550. void epoll_reactor::do_add_timer_queue(timer_queue_base& queue)
  551. {
  552. mutex::scoped_lock lock(mutex_);
  553. timer_queues_.insert(&queue);
  554. }
  555. void epoll_reactor::do_remove_timer_queue(timer_queue_base& queue)
  556. {
  557. mutex::scoped_lock lock(mutex_);
  558. timer_queues_.erase(&queue);
  559. }
  560. void epoll_reactor::update_timeout()
  561. {
  562. #if defined(BOOST_ASIO_HAS_TIMERFD)
  563. if (timer_fd_ != -1)
  564. {
  565. itimerspec new_timeout;
  566. itimerspec old_timeout;
  567. int flags = get_timeout(new_timeout);
  568. timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
  569. return;
  570. }
  571. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  572. interrupt();
  573. }
  574. int epoll_reactor::get_timeout(int msec)
  575. {
  576. // By default we will wait no longer than 5 minutes. This will ensure that
  577. // any changes to the system clock are detected after no longer than this.
  578. const int max_msec = 5 * 60 * 1000;
  579. return timer_queues_.wait_duration_msec(
  580. (msec < 0 || max_msec < msec) ? max_msec : msec);
  581. }
  582. #if defined(BOOST_ASIO_HAS_TIMERFD)
  583. int epoll_reactor::get_timeout(itimerspec& ts)
  584. {
  585. ts.it_interval.tv_sec = 0;
  586. ts.it_interval.tv_nsec = 0;
  587. long usec = timer_queues_.wait_duration_usec(5 * 60 * 1000 * 1000);
  588. ts.it_value.tv_sec = usec / 1000000;
  589. ts.it_value.tv_nsec = usec ? (usec % 1000000) * 1000 : 1;
  590. return usec ? 0 : TFD_TIMER_ABSTIME;
  591. }
  592. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  593. struct epoll_reactor::perform_io_cleanup_on_block_exit
  594. {
  595. explicit perform_io_cleanup_on_block_exit(epoll_reactor* r)
  596. : reactor_(r), first_op_(0)
  597. {
  598. }
  599. ~perform_io_cleanup_on_block_exit()
  600. {
  601. if (first_op_)
  602. {
  603. // Post the remaining completed operations for invocation.
  604. if (!ops_.empty())
  605. reactor_->scheduler_.post_deferred_completions(ops_);
  606. // A user-initiated operation has completed, but there's no need to
  607. // explicitly call work_finished() here. Instead, we'll take advantage of
  608. // the fact that the scheduler will call work_finished() once we return.
  609. }
  610. else
  611. {
  612. // No user-initiated operations have completed, so we need to compensate
  613. // for the work_finished() call that the scheduler will make once this
  614. // operation returns.
  615. reactor_->scheduler_.compensating_work_started();
  616. }
  617. }
  618. epoll_reactor* reactor_;
  619. op_queue<operation> ops_;
  620. operation* first_op_;
  621. };
  622. epoll_reactor::descriptor_state::descriptor_state(bool locking)
  623. : operation(&epoll_reactor::descriptor_state::do_complete),
  624. mutex_(locking)
  625. {
  626. }
  627. operation* epoll_reactor::descriptor_state::perform_io(uint32_t events)
  628. {
  629. mutex_.lock();
  630. perform_io_cleanup_on_block_exit io_cleanup(reactor_);
  631. mutex::scoped_lock descriptor_lock(mutex_, mutex::scoped_lock::adopt_lock);
  632. // Exception operations must be processed first to ensure that any
  633. // out-of-band data is read before normal data.
  634. static const int flag[max_ops] = { EPOLLIN, EPOLLOUT, EPOLLPRI };
  635. for (int j = max_ops - 1; j >= 0; --j)
  636. {
  637. if (events & (flag[j] | EPOLLERR | EPOLLHUP))
  638. {
  639. try_speculative_[j] = true;
  640. while (reactor_op* op = op_queue_[j].front())
  641. {
  642. if (reactor_op::status status = op->perform())
  643. {
  644. op_queue_[j].pop();
  645. io_cleanup.ops_.push(op);
  646. if (status == reactor_op::done_and_exhausted)
  647. {
  648. try_speculative_[j] = false;
  649. break;
  650. }
  651. }
  652. else
  653. break;
  654. }
  655. }
  656. }
  657. // The first operation will be returned for completion now. The others will
  658. // be posted for later by the io_cleanup object's destructor.
  659. io_cleanup.first_op_ = io_cleanup.ops_.front();
  660. io_cleanup.ops_.pop();
  661. return io_cleanup.first_op_;
  662. }
  663. void epoll_reactor::descriptor_state::do_complete(
  664. void* owner, operation* base,
  665. const boost::system::error_code& ec, std::size_t bytes_transferred)
  666. {
  667. if (owner)
  668. {
  669. descriptor_state* descriptor_data = static_cast<descriptor_state*>(base);
  670. uint32_t events = static_cast<uint32_t>(bytes_transferred);
  671. if (operation* op = descriptor_data->perform_io(events))
  672. {
  673. op->complete(owner, ec, 0);
  674. }
  675. }
  676. }
  677. } // namespace detail
  678. } // namespace asio
  679. } // namespace boost
  680. #include <boost/asio/detail/pop_options.hpp>
  681. #endif // defined(BOOST_ASIO_HAS_EPOLL)
  682. #endif // BOOST_ASIO_DETAIL_IMPL_EPOLL_REACTOR_IPP