dev_poll_reactor.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // detail/dev_poll_reactor.hpp
  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_DEV_POLL_REACTOR_HPP
  11. #define ASIO_DETAIL_DEV_POLL_REACTOR_HPP
  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_DEV_POLL)
  17. #include <cstddef>
  18. #include <vector>
  19. #include <sys/devpoll.h>
  20. #include "asio/detail/dev_poll_reactor_fwd.hpp"
  21. #include "asio/detail/hash_map.hpp"
  22. #include "asio/detail/mutex.hpp"
  23. #include "asio/detail/op_queue.hpp"
  24. #include "asio/detail/reactor_op.hpp"
  25. #include "asio/detail/reactor_op_queue.hpp"
  26. #include "asio/detail/select_interrupter.hpp"
  27. #include "asio/detail/socket_types.hpp"
  28. #include "asio/detail/timer_op.hpp"
  29. #include "asio/detail/timer_queue_base.hpp"
  30. #include "asio/detail/timer_queue_fwd.hpp"
  31. #include "asio/detail/timer_queue_set.hpp"
  32. #include "asio/io_service.hpp"
  33. #include "asio/detail/push_options.hpp"
  34. namespace asio {
  35. namespace detail {
  36. class dev_poll_reactor
  37. : public asio::detail::service_base<dev_poll_reactor>
  38. {
  39. public:
  40. enum { read_op = 0, write_op = 1,
  41. connect_op = 1, except_op = 2, max_ops = 3 };
  42. // Per-descriptor data.
  43. struct per_descriptor_data
  44. {
  45. };
  46. // Constructor.
  47. ASIO_DECL dev_poll_reactor(asio::io_service& io_service);
  48. // Destructor.
  49. ASIO_DECL ~dev_poll_reactor();
  50. // Destroy all user-defined handler objects owned by the service.
  51. ASIO_DECL void shutdown_service();
  52. // Initialise the task.
  53. ASIO_DECL void init_task();
  54. // Register a socket with the reactor. Returns 0 on success, system error
  55. // code on failure.
  56. ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
  57. // Post a reactor operation for immediate completion.
  58. void post_immediate_completion(reactor_op* op)
  59. {
  60. io_service_.post_immediate_completion(op);
  61. }
  62. // Start a new operation. The reactor operation will be performed when the
  63. // given descriptor is flagged as ready, or an error has occurred.
  64. ASIO_DECL void start_op(int op_type, socket_type descriptor,
  65. per_descriptor_data&, reactor_op* op, bool allow_speculative);
  66. // Cancel all operations associated with the given descriptor. The
  67. // handlers associated with the descriptor will be invoked with the
  68. // operation_aborted error.
  69. ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
  70. // Cancel any operations that are running against the descriptor and remove
  71. // its registration from the reactor.
  72. ASIO_DECL void close_descriptor(
  73. socket_type descriptor, per_descriptor_data&);
  74. // Add a new timer queue to the reactor.
  75. template <typename Time_Traits>
  76. void add_timer_queue(timer_queue<Time_Traits>& queue);
  77. // Remove a timer queue from the reactor.
  78. template <typename Time_Traits>
  79. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  80. // Schedule a new operation in the given timer queue to expire at the
  81. // specified absolute time.
  82. template <typename Time_Traits>
  83. void schedule_timer(timer_queue<Time_Traits>& queue,
  84. const typename Time_Traits::time_type& time,
  85. typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
  86. // Cancel the timer operations associated with the given token. Returns the
  87. // number of operations that have been posted or dispatched.
  88. template <typename Time_Traits>
  89. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  90. typename timer_queue<Time_Traits>::per_timer_data& timer);
  91. // Run /dev/poll once until interrupted or events are ready to be dispatched.
  92. ASIO_DECL void run(bool block, op_queue<operation>& ops);
  93. // Interrupt the select loop.
  94. ASIO_DECL void interrupt();
  95. private:
  96. // Create the /dev/poll file descriptor. Throws an exception if the descriptor
  97. // cannot be created.
  98. ASIO_DECL static int do_dev_poll_create();
  99. // Helper function to add a new timer queue.
  100. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  101. // Helper function to remove a timer queue.
  102. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  103. // Get the timeout value for the /dev/poll DP_POLL operation. The timeout
  104. // value is returned as a number of milliseconds. A return value of -1
  105. // indicates that the poll should block indefinitely.
  106. ASIO_DECL int get_timeout();
  107. // Cancel all operations associated with the given descriptor. The do_cancel
  108. // function of the handler objects will be invoked. This function does not
  109. // acquire the dev_poll_reactor's mutex.
  110. ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
  111. const asio::error_code& ec);
  112. // Add a pending event entry for the given descriptor.
  113. ASIO_DECL ::pollfd& add_pending_event_change(int descriptor);
  114. // The io_service implementation used to post completions.
  115. io_service_impl& io_service_;
  116. // Mutex to protect access to internal data.
  117. asio::detail::mutex mutex_;
  118. // The /dev/poll file descriptor.
  119. int dev_poll_fd_;
  120. // Vector of /dev/poll events waiting to be written to the descriptor.
  121. std::vector< ::pollfd> pending_event_changes_;
  122. // Hash map to associate a descriptor with a pending event change index.
  123. hash_map<int, std::size_t> pending_event_change_index_;
  124. // The interrupter is used to break a blocking DP_POLL operation.
  125. select_interrupter interrupter_;
  126. // The queues of read, write and except operations.
  127. reactor_op_queue<socket_type> op_queue_[max_ops];
  128. // The timer queues.
  129. timer_queue_set timer_queues_;
  130. // Whether the service has been shut down.
  131. bool shutdown_;
  132. };
  133. } // namespace detail
  134. } // namespace asio
  135. #include "asio/detail/pop_options.hpp"
  136. #include "asio/detail/impl/dev_poll_reactor.hpp"
  137. #if defined(ASIO_HEADER_ONLY)
  138. # include "asio/detail/impl/dev_poll_reactor.ipp"
  139. #endif // defined(ASIO_HEADER_ONLY)
  140. #endif // defined(ASIO_HAS_DEV_POLL)
  141. #endif // ASIO_DETAIL_DEV_POLL_REACTOR_HPP