Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:28:49

0001 //
0002 // detail/kqueue_reactor.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 
0012 #ifndef BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP
0013 #define BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP
0014 
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0018 
0019 #include <boost/asio/detail/config.hpp>
0020 
0021 #if defined(BOOST_ASIO_HAS_KQUEUE)
0022 
0023 #include <cstddef>
0024 #include <sys/types.h>
0025 #include <sys/event.h>
0026 #include <sys/time.h>
0027 #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
0028 #include <boost/asio/detail/limits.hpp>
0029 #include <boost/asio/detail/object_pool.hpp>
0030 #include <boost/asio/detail/op_queue.hpp>
0031 #include <boost/asio/detail/reactor_op.hpp>
0032 #include <boost/asio/detail/scheduler_task.hpp>
0033 #include <boost/asio/detail/select_interrupter.hpp>
0034 #include <boost/asio/detail/socket_types.hpp>
0035 #include <boost/asio/detail/timer_queue_base.hpp>
0036 #include <boost/asio/detail/timer_queue_set.hpp>
0037 #include <boost/asio/detail/wait_op.hpp>
0038 #include <boost/asio/error.hpp>
0039 #include <boost/asio/execution_context.hpp>
0040 
0041 // Older versions of Mac OS X may not define EV_OOBAND.
0042 #if !defined(EV_OOBAND)
0043 # define EV_OOBAND EV_FLAG1
0044 #endif // !defined(EV_OOBAND)
0045 
0046 #include <boost/asio/detail/push_options.hpp>
0047 
0048 namespace boost {
0049 namespace asio {
0050 namespace detail {
0051 
0052 class scheduler;
0053 
0054 class kqueue_reactor
0055   : public execution_context_service_base<kqueue_reactor>,
0056     public scheduler_task
0057 {
0058 private:
0059   // The mutex type used by this reactor.
0060   typedef conditionally_enabled_mutex mutex;
0061 
0062 public:
0063   enum op_types { read_op = 0, write_op = 1,
0064     connect_op = 1, except_op = 2, max_ops = 3 };
0065 
0066   // Per-descriptor queues.
0067   struct descriptor_state
0068   {
0069     descriptor_state(bool locking, int spin_count)
0070       : mutex_(locking, spin_count) {}
0071 
0072     friend class kqueue_reactor;
0073     friend class object_pool_access;
0074 
0075     descriptor_state* next_;
0076     descriptor_state* prev_;
0077 
0078     mutex mutex_;
0079     int descriptor_;
0080     int num_kevents_; // 1 == read only, 2 == read and write
0081     op_queue<reactor_op> op_queue_[max_ops];
0082     bool shutdown_;
0083   };
0084 
0085   // Per-descriptor data.
0086   typedef descriptor_state* per_descriptor_data;
0087 
0088   // Constructor.
0089   BOOST_ASIO_DECL kqueue_reactor(boost::asio::execution_context& ctx);
0090 
0091   // Destructor.
0092   BOOST_ASIO_DECL ~kqueue_reactor();
0093 
0094   // Destroy all user-defined handler objects owned by the service.
0095   BOOST_ASIO_DECL void shutdown();
0096 
0097   // Recreate internal descriptors following a fork.
0098   BOOST_ASIO_DECL void notify_fork(
0099       boost::asio::execution_context::fork_event fork_ev);
0100 
0101   // Initialise the task.
0102   BOOST_ASIO_DECL void init_task();
0103 
0104   // Register a socket with the reactor. Returns 0 on success, system error
0105   // code on failure.
0106   BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
0107       per_descriptor_data& descriptor_data);
0108 
0109   // Register a descriptor with an associated single operation. Returns 0 on
0110   // success, system error code on failure.
0111   BOOST_ASIO_DECL int register_internal_descriptor(
0112       int op_type, socket_type descriptor,
0113       per_descriptor_data& descriptor_data, reactor_op* op);
0114 
0115   // Move descriptor registration from one descriptor_data object to another.
0116   BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
0117       per_descriptor_data& target_descriptor_data,
0118       per_descriptor_data& source_descriptor_data);
0119 
0120   // Post a reactor operation for immediate completion.
0121   void post_immediate_completion(operation* op, bool is_continuation) const;
0122 
0123   // Post a reactor operation for immediate completion.
0124   BOOST_ASIO_DECL static void call_post_immediate_completion(
0125       operation* op, bool is_continuation, const void* self);
0126 
0127   // Start a new operation. The reactor operation will be performed when the
0128   // given descriptor is flagged as ready, or an error has occurred.
0129   BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
0130       per_descriptor_data& descriptor_data, reactor_op* op,
0131       bool is_continuation, bool allow_speculative,
0132       void (*on_immediate)(operation*, bool, const void*),
0133       const void* immediate_arg);
0134 
0135   // Start a new operation. The reactor operation will be performed when the
0136   // given descriptor is flagged as ready, or an error has occurred.
0137   void start_op(int op_type, socket_type descriptor,
0138       per_descriptor_data& descriptor_data, reactor_op* op,
0139       bool is_continuation, bool allow_speculative)
0140   {
0141     start_op(op_type, descriptor, descriptor_data,
0142         op, is_continuation, allow_speculative,
0143         &kqueue_reactor::call_post_immediate_completion, this);
0144   }
0145 
0146   // Cancel all operations associated with the given descriptor. The
0147   // handlers associated with the descriptor will be invoked with the
0148   // operation_aborted error.
0149   BOOST_ASIO_DECL void cancel_ops(socket_type descriptor,
0150       per_descriptor_data& descriptor_data);
0151 
0152   // Cancel all operations associated with the given descriptor and key. The
0153   // handlers associated with the descriptor will be invoked with the
0154   // operation_aborted error.
0155   BOOST_ASIO_DECL void cancel_ops_by_key(socket_type descriptor,
0156       per_descriptor_data& descriptor_data,
0157       int op_type, void* cancellation_key);
0158 
0159   // Cancel any operations that are running against the descriptor and remove
0160   // its registration from the reactor. The reactor resources associated with
0161   // the descriptor must be released by calling cleanup_descriptor_data.
0162   BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
0163       per_descriptor_data& descriptor_data, bool closing);
0164 
0165   // Remove the descriptor's registration from the reactor. The reactor
0166   // resources associated with the descriptor must be released by calling
0167   // cleanup_descriptor_data.
0168   BOOST_ASIO_DECL void deregister_internal_descriptor(
0169       socket_type descriptor, per_descriptor_data& descriptor_data);
0170 
0171   // Perform any post-deregistration cleanup tasks associated with the
0172   // descriptor data.
0173   BOOST_ASIO_DECL void cleanup_descriptor_data(
0174       per_descriptor_data& descriptor_data);
0175 
0176   // Add a new timer queue to the reactor.
0177   template <typename Time_Traits>
0178   void add_timer_queue(timer_queue<Time_Traits>& queue);
0179 
0180   // Remove a timer queue from the reactor.
0181   template <typename Time_Traits>
0182   void remove_timer_queue(timer_queue<Time_Traits>& queue);
0183 
0184   // Schedule a new operation in the given timer queue to expire at the
0185   // specified absolute time.
0186   template <typename Time_Traits>
0187   void schedule_timer(timer_queue<Time_Traits>& queue,
0188       const typename Time_Traits::time_type& time,
0189       typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
0190 
0191   // Cancel the timer operations associated with the given token. Returns the
0192   // number of operations that have been posted or dispatched.
0193   template <typename Time_Traits>
0194   std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
0195       typename timer_queue<Time_Traits>::per_timer_data& timer,
0196       std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
0197 
0198   // Cancel the timer operations associated with the given key.
0199   template <typename Time_Traits>
0200   void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
0201       typename timer_queue<Time_Traits>::per_timer_data* timer,
0202       void* cancellation_key);
0203 
0204   // Move the timer operations associated with the given timer.
0205   template <typename Time_Traits>
0206   void move_timer(timer_queue<Time_Traits>& queue,
0207       typename timer_queue<Time_Traits>::per_timer_data& target,
0208       typename timer_queue<Time_Traits>::per_timer_data& source);
0209 
0210   // Run the kqueue loop.
0211   BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
0212 
0213   // Interrupt the kqueue loop.
0214   BOOST_ASIO_DECL void interrupt();
0215 
0216 private:
0217   // Create the kqueue file descriptor. Throws an exception if the descriptor
0218   // cannot be created.
0219   BOOST_ASIO_DECL static int do_kqueue_create();
0220 
0221   // Allocate a new descriptor state object.
0222   BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
0223 
0224   // Free an existing descriptor state object.
0225   BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
0226 
0227   // Helper function to add a new timer queue.
0228   BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
0229 
0230   // Helper function to remove a timer queue.
0231   BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
0232 
0233   // Get the timeout value for the kevent call.
0234   BOOST_ASIO_DECL timespec* get_timeout(long usec, timespec& ts);
0235 
0236   // The scheduler used to post completions.
0237   scheduler& scheduler_;
0238 
0239   // Mutex to protect access to internal data.
0240   mutex mutex_;
0241 
0242   // The kqueue file descriptor.
0243   int kqueue_fd_;
0244 
0245   // The interrupter is used to break a blocking kevent call.
0246   select_interrupter interrupter_;
0247 
0248   // The timer queues.
0249   timer_queue_set timer_queues_;
0250 
0251   // Whether the service has been shut down.
0252   bool shutdown_;
0253 
0254   // Whether I/O locking is enabled.
0255   const bool io_locking_;
0256 
0257   // How any times to spin waiting for the I/O mutex.
0258   const int io_locking_spin_count_;
0259 
0260   // Mutex to protect access to the registered descriptors.
0261   mutex registered_descriptors_mutex_;
0262 
0263   // Keep track of all registered descriptors.
0264   object_pool<descriptor_state> registered_descriptors_;
0265 };
0266 
0267 } // namespace detail
0268 } // namespace asio
0269 } // namespace boost
0270 
0271 #include <boost/asio/detail/pop_options.hpp>
0272 
0273 #include <boost/asio/detail/impl/kqueue_reactor.hpp>
0274 #if defined(BOOST_ASIO_HEADER_ONLY)
0275 # include <boost/asio/detail/impl/kqueue_reactor.ipp>
0276 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0277 
0278 #endif // defined(BOOST_ASIO_HAS_KQUEUE)
0279 
0280 #endif // BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP