epoll_reactor.ipp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // detail/impl/epoll_reactor.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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/throw_error.hpp"
  21. #include "asio/error.hpp"
  22. #if defined(ASIO_HAS_TIMERFD)
  23. # include <sys/timerfd.h>
  24. #endif // defined(ASIO_HAS_TIMERFD)
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace detail {
  28. epoll_reactor::epoll_reactor(asio::io_service& io_service)
  29. : asio::detail::service_base<epoll_reactor>(io_service),
  30. io_service_(use_service<io_service_impl>(io_service)),
  31. mutex_(),
  32. epoll_fd_(do_epoll_create()),
  33. #if defined(ASIO_HAS_TIMERFD)
  34. timer_fd_(timerfd_create(CLOCK_MONOTONIC, 0)),
  35. #else // defined(ASIO_HAS_TIMERFD)
  36. timer_fd_(-1),
  37. #endif // defined(ASIO_HAS_TIMERFD)
  38. interrupter_(),
  39. shutdown_(false)
  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. close(epoll_fd_);
  58. if (timer_fd_ != -1)
  59. close(timer_fd_);
  60. }
  61. void epoll_reactor::shutdown_service()
  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. }
  76. void epoll_reactor::init_task()
  77. {
  78. io_service_.init_task();
  79. }
  80. int epoll_reactor::register_descriptor(socket_type descriptor,
  81. epoll_reactor::per_descriptor_data& descriptor_data)
  82. {
  83. mutex::scoped_lock lock(registered_descriptors_mutex_);
  84. descriptor_data = registered_descriptors_.alloc();
  85. descriptor_data->shutdown_ = false;
  86. lock.unlock();
  87. epoll_event ev = { 0, { 0 } };
  88. ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLOUT | EPOLLPRI | EPOLLET;
  89. ev.data.ptr = descriptor_data;
  90. int result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
  91. if (result != 0)
  92. return errno;
  93. return 0;
  94. }
  95. void epoll_reactor::start_op(int op_type, socket_type descriptor,
  96. epoll_reactor::per_descriptor_data& descriptor_data,
  97. reactor_op* op, bool allow_speculative)
  98. {
  99. if (!descriptor_data)
  100. {
  101. op->ec_ = asio::error::bad_descriptor;
  102. post_immediate_completion(op);
  103. return;
  104. }
  105. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  106. if (descriptor_data->shutdown_)
  107. {
  108. post_immediate_completion(op);
  109. return;
  110. }
  111. if (descriptor_data->op_queue_[op_type].empty())
  112. {
  113. if (allow_speculative
  114. && (op_type != read_op
  115. || descriptor_data->op_queue_[except_op].empty()))
  116. {
  117. if (op->perform())
  118. {
  119. descriptor_lock.unlock();
  120. io_service_.post_immediate_completion(op);
  121. return;
  122. }
  123. }
  124. else
  125. {
  126. epoll_event ev = { 0, { 0 } };
  127. ev.events = EPOLLIN | EPOLLERR | EPOLLHUP
  128. | EPOLLOUT | EPOLLPRI | EPOLLET;
  129. ev.data.ptr = descriptor_data;
  130. epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, descriptor, &ev);
  131. }
  132. }
  133. descriptor_data->op_queue_[op_type].push(op);
  134. io_service_.work_started();
  135. }
  136. void epoll_reactor::cancel_ops(socket_type,
  137. epoll_reactor::per_descriptor_data& descriptor_data)
  138. {
  139. if (!descriptor_data)
  140. return;
  141. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  142. op_queue<operation> ops;
  143. for (int i = 0; i < max_ops; ++i)
  144. {
  145. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  146. {
  147. op->ec_ = asio::error::operation_aborted;
  148. descriptor_data->op_queue_[i].pop();
  149. ops.push(op);
  150. }
  151. }
  152. descriptor_lock.unlock();
  153. io_service_.post_deferred_completions(ops);
  154. }
  155. void epoll_reactor::close_descriptor(socket_type,
  156. epoll_reactor::per_descriptor_data& descriptor_data)
  157. {
  158. if (!descriptor_data)
  159. return;
  160. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  161. mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_);
  162. if (!descriptor_data->shutdown_)
  163. {
  164. // Remove the descriptor from the set of known descriptors. The descriptor
  165. // will be automatically removed from the epoll set when it is closed.
  166. op_queue<operation> ops;
  167. for (int i = 0; i < max_ops; ++i)
  168. {
  169. while (reactor_op* op = descriptor_data->op_queue_[i].front())
  170. {
  171. op->ec_ = asio::error::operation_aborted;
  172. descriptor_data->op_queue_[i].pop();
  173. ops.push(op);
  174. }
  175. }
  176. descriptor_data->shutdown_ = true;
  177. descriptor_lock.unlock();
  178. registered_descriptors_.free(descriptor_data);
  179. descriptor_data = 0;
  180. descriptors_lock.unlock();
  181. io_service_.post_deferred_completions(ops);
  182. }
  183. }
  184. void epoll_reactor::run(bool block, op_queue<operation>& ops)
  185. {
  186. // Calculate a timeout only if timerfd is not used.
  187. int timeout;
  188. if (timer_fd_ != -1)
  189. timeout = block ? -1 : 0;
  190. else
  191. {
  192. mutex::scoped_lock lock(mutex_);
  193. timeout = block ? get_timeout() : 0;
  194. }
  195. // Block on the epoll descriptor.
  196. epoll_event events[128];
  197. int num_events = epoll_wait(epoll_fd_, events, 128, timeout);
  198. #if defined(ASIO_HAS_TIMERFD)
  199. bool check_timers = (timer_fd_ == -1);
  200. #else // defined(ASIO_HAS_TIMERFD)
  201. bool check_timers = true;
  202. #endif // defined(ASIO_HAS_TIMERFD)
  203. // Dispatch the waiting events.
  204. for (int i = 0; i < num_events; ++i)
  205. {
  206. void* ptr = events[i].data.ptr;
  207. if (ptr == &interrupter_)
  208. {
  209. // No need to reset the interrupter since we're leaving the descriptor
  210. // in a ready-to-read state and relying on edge-triggered notifications
  211. // to make it so that we only get woken up when the descriptor's epoll
  212. // registration is updated.
  213. #if defined(ASIO_HAS_TIMERFD)
  214. if (timer_fd_ == -1)
  215. check_timers = true;
  216. #else // defined(ASIO_HAS_TIMERFD)
  217. check_timers = true;
  218. #endif // defined(ASIO_HAS_TIMERFD)
  219. }
  220. #if defined(ASIO_HAS_TIMERFD)
  221. else if (ptr == &timer_fd_)
  222. {
  223. check_timers = true;
  224. }
  225. #endif // defined(ASIO_HAS_TIMERFD)
  226. else
  227. {
  228. descriptor_state* descriptor_data = static_cast<descriptor_state*>(ptr);
  229. mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
  230. // Exception operations must be processed first to ensure that any
  231. // out-of-band data is read before normal data.
  232. static const int flag[max_ops] = { EPOLLIN, EPOLLOUT, EPOLLPRI };
  233. for (int j = max_ops - 1; j >= 0; --j)
  234. {
  235. if (events[i].events & (flag[j] | EPOLLERR | EPOLLHUP))
  236. {
  237. while (reactor_op* op = descriptor_data->op_queue_[j].front())
  238. {
  239. if (op->perform())
  240. {
  241. descriptor_data->op_queue_[j].pop();
  242. ops.push(op);
  243. }
  244. else
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. if (check_timers)
  252. {
  253. mutex::scoped_lock common_lock(mutex_);
  254. timer_queues_.get_ready_timers(ops);
  255. #if defined(ASIO_HAS_TIMERFD)
  256. if (timer_fd_ != -1)
  257. {
  258. itimerspec new_timeout;
  259. itimerspec old_timeout;
  260. int flags = get_timeout(new_timeout);
  261. timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
  262. }
  263. #endif // defined(ASIO_HAS_TIMERFD)
  264. }
  265. }
  266. void epoll_reactor::interrupt()
  267. {
  268. epoll_event ev = { 0, { 0 } };
  269. ev.events = EPOLLIN | EPOLLERR | EPOLLET;
  270. ev.data.ptr = &interrupter_;
  271. epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, interrupter_.read_descriptor(), &ev);
  272. }
  273. int epoll_reactor::do_epoll_create()
  274. {
  275. int fd = epoll_create(epoll_size);
  276. if (fd == -1)
  277. {
  278. asio::error_code ec(errno,
  279. asio::error::get_system_category());
  280. asio::detail::throw_error(ec, "epoll");
  281. }
  282. return fd;
  283. }
  284. void epoll_reactor::do_add_timer_queue(timer_queue_base& queue)
  285. {
  286. mutex::scoped_lock lock(mutex_);
  287. timer_queues_.insert(&queue);
  288. }
  289. void epoll_reactor::do_remove_timer_queue(timer_queue_base& queue)
  290. {
  291. mutex::scoped_lock lock(mutex_);
  292. timer_queues_.erase(&queue);
  293. }
  294. void epoll_reactor::update_timeout()
  295. {
  296. #if defined(ASIO_HAS_TIMERFD)
  297. if (timer_fd_ != -1)
  298. {
  299. itimerspec new_timeout;
  300. itimerspec old_timeout;
  301. int flags = get_timeout(new_timeout);
  302. timerfd_settime(timer_fd_, flags, &new_timeout, &old_timeout);
  303. return;
  304. }
  305. #endif // defined(ASIO_HAS_TIMERFD)
  306. interrupt();
  307. }
  308. int epoll_reactor::get_timeout()
  309. {
  310. // By default we will wait no longer than 5 minutes. This will ensure that
  311. // any changes to the system clock are detected after no longer than this.
  312. return timer_queues_.wait_duration_msec(5 * 60 * 1000);
  313. }
  314. #if defined(ASIO_HAS_TIMERFD)
  315. int epoll_reactor::get_timeout(itimerspec& ts)
  316. {
  317. ts.it_interval.tv_sec = 0;
  318. ts.it_interval.tv_nsec = 0;
  319. long usec = timer_queues_.wait_duration_usec(5 * 60 * 1000 * 1000);
  320. ts.it_value.tv_sec = usec / 1000000;
  321. ts.it_value.tv_nsec = usec ? (usec % 1000000) * 1000 : 1;
  322. return usec ? 0 : TFD_TIMER_ABSTIME;
  323. }
  324. #endif // defined(ASIO_HAS_TIMERFD)
  325. } // namespace detail
  326. } // namespace asio
  327. #include "asio/detail/pop_options.hpp"
  328. #endif // defined(ASIO_HAS_EPOLL)
  329. #endif // ASIO_DETAIL_IMPL_EPOLL_REACTOR_IPP