kqueue_reactor.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // detail/kqueue_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_DETAIL_KQUEUE_REACTOR_HPP
  12. #define ASIO_DETAIL_KQUEUE_REACTOR_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_KQUEUE)
  18. #include <cstddef>
  19. #include <sys/types.h>
  20. #include <sys/event.h>
  21. #include <sys/time.h>
  22. #include "asio/detail/kqueue_reactor_fwd.hpp"
  23. #include "asio/detail/mutex.hpp"
  24. #include "asio/detail/object_pool.hpp"
  25. #include "asio/detail/op_queue.hpp"
  26. #include "asio/detail/reactor_op.hpp"
  27. #include "asio/detail/select_interrupter.hpp"
  28. #include "asio/detail/socket_types.hpp"
  29. #include "asio/detail/timer_op.hpp"
  30. #include "asio/detail/timer_queue_base.hpp"
  31. #include "asio/detail/timer_queue_fwd.hpp"
  32. #include "asio/detail/timer_queue_set.hpp"
  33. #include "asio/error.hpp"
  34. #include "asio/io_service.hpp"
  35. // Older versions of Mac OS X may not define EV_OOBAND.
  36. #if !defined(EV_OOBAND)
  37. # define EV_OOBAND EV_FLAG1
  38. #endif // !defined(EV_OOBAND)
  39. #include "asio/detail/push_options.hpp"
  40. namespace asio {
  41. namespace detail {
  42. class kqueue_reactor
  43. : public asio::detail::service_base<kqueue_reactor>
  44. {
  45. public:
  46. enum op_types { read_op = 0, write_op = 1,
  47. connect_op = 1, except_op = 2, max_ops = 3 };
  48. // Per-descriptor queues.
  49. struct descriptor_state
  50. {
  51. friend class kqueue_reactor;
  52. friend class object_pool_access;
  53. mutex mutex_;
  54. op_queue<reactor_op> op_queue_[max_ops];
  55. bool shutdown_;
  56. descriptor_state* next_;
  57. descriptor_state* prev_;
  58. };
  59. // Per-descriptor data.
  60. typedef descriptor_state* per_descriptor_data;
  61. // Constructor.
  62. ASIO_DECL kqueue_reactor(asio::io_service& io_service);
  63. // Destructor.
  64. ASIO_DECL ~kqueue_reactor();
  65. // Destroy all user-defined handler objects owned by the service.
  66. ASIO_DECL void shutdown_service();
  67. // Initialise the task.
  68. ASIO_DECL void init_task();
  69. // Register a socket with the reactor. Returns 0 on success, system error
  70. // code on failure.
  71. ASIO_DECL int register_descriptor(socket_type descriptor,
  72. per_descriptor_data& descriptor_data);
  73. // Post a reactor operation for immediate completion.
  74. void post_immediate_completion(reactor_op* op)
  75. {
  76. io_service_.post_immediate_completion(op);
  77. }
  78. // Start a new operation. The reactor operation will be performed when the
  79. // given descriptor is flagged as ready, or an error has occurred.
  80. ASIO_DECL void start_op(int op_type, socket_type descriptor,
  81. per_descriptor_data& descriptor_data,
  82. reactor_op* op, bool allow_speculative);
  83. // Cancel all operations associated with the given descriptor. The
  84. // handlers associated with the descriptor will be invoked with the
  85. // operation_aborted error.
  86. ASIO_DECL void cancel_ops(socket_type descriptor,
  87. per_descriptor_data& descriptor_data);
  88. // Cancel any operations that are running against the descriptor and remove
  89. // its registration from the reactor.
  90. ASIO_DECL void close_descriptor(socket_type descriptor,
  91. per_descriptor_data& descriptor_data);
  92. // Add a new timer queue to the reactor.
  93. template <typename Time_Traits>
  94. void add_timer_queue(timer_queue<Time_Traits>& queue);
  95. // Remove a timer queue from the reactor.
  96. template <typename Time_Traits>
  97. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  98. // Schedule a new operation in the given timer queue to expire at the
  99. // specified absolute time.
  100. template <typename Time_Traits>
  101. void schedule_timer(timer_queue<Time_Traits>& queue,
  102. const typename Time_Traits::time_type& time,
  103. typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
  104. // Cancel the timer operations associated with the given token. Returns the
  105. // number of operations that have been posted or dispatched.
  106. template <typename Time_Traits>
  107. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  108. typename timer_queue<Time_Traits>::per_timer_data& timer);
  109. // Run the kqueue loop.
  110. ASIO_DECL void run(bool block, op_queue<operation>& ops);
  111. // Interrupt the kqueue loop.
  112. ASIO_DECL void interrupt();
  113. private:
  114. // Create the kqueue file descriptor. Throws an exception if the descriptor
  115. // cannot be created.
  116. ASIO_DECL static int do_kqueue_create();
  117. // Helper function to add a new timer queue.
  118. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  119. // Helper function to remove a timer queue.
  120. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  121. // Get the timeout value for the kevent call.
  122. ASIO_DECL timespec* get_timeout(timespec& ts);
  123. // The io_service implementation used to post completions.
  124. io_service_impl& io_service_;
  125. // Mutex to protect access to internal data.
  126. mutex mutex_;
  127. // The kqueue file descriptor.
  128. int kqueue_fd_;
  129. // The interrupter is used to break a blocking kevent call.
  130. select_interrupter interrupter_;
  131. // The timer queues.
  132. timer_queue_set timer_queues_;
  133. // Whether the service has been shut down.
  134. bool shutdown_;
  135. // Mutex to protect access to the registered descriptors.
  136. mutex registered_descriptors_mutex_;
  137. // Keep track of all registered descriptors.
  138. object_pool<descriptor_state> registered_descriptors_;
  139. };
  140. } // namespace detail
  141. } // namespace asio
  142. #include "asio/detail/pop_options.hpp"
  143. #include "asio/detail/impl/kqueue_reactor.hpp"
  144. #if defined(ASIO_HEADER_ONLY)
  145. # include "asio/detail/impl/kqueue_reactor.ipp"
  146. #endif // defined(ASIO_HEADER_ONLY)
  147. #endif // defined(ASIO_HAS_KQUEUE)
  148. #endif // ASIO_DETAIL_KQUEUE_REACTOR_HPP