Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:41

0001 //
0002 // detail/kqueue_reactor.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 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) : mutex_(locking) {}
0070 
0071     friend class kqueue_reactor;
0072     friend class object_pool_access;
0073 
0074     descriptor_state* next_;
0075     descriptor_state* prev_;
0076 
0077     mutex mutex_;
0078     int descriptor_;
0079     int num_kevents_; // 1 == read only, 2 == read and write
0080     op_queue<reactor_op> op_queue_[max_ops];
0081     bool shutdown_;
0082   };
0083 
0084   // Per-descriptor data.
0085   typedef descriptor_state* per_descriptor_data;
0086 
0087   // Constructor.
0088   BOOST_ASIO_DECL kqueue_reactor(boost::asio::execution_context& ctx);
0089 
0090   // Destructor.
0091   BOOST_ASIO_DECL ~kqueue_reactor();
0092 
0093   // Destroy all user-defined handler objects owned by the service.
0094   BOOST_ASIO_DECL void shutdown();
0095 
0096   // Recreate internal descriptors following a fork.
0097   BOOST_ASIO_DECL void notify_fork(
0098       boost::asio::execution_context::fork_event fork_ev);
0099 
0100   // Initialise the task.
0101   BOOST_ASIO_DECL void init_task();
0102 
0103   // Register a socket with the reactor. Returns 0 on success, system error
0104   // code on failure.
0105   BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
0106       per_descriptor_data& descriptor_data);
0107 
0108   // Register a descriptor with an associated single operation. Returns 0 on
0109   // success, system error code on failure.
0110   BOOST_ASIO_DECL int register_internal_descriptor(
0111       int op_type, socket_type descriptor,
0112       per_descriptor_data& descriptor_data, reactor_op* op);
0113 
0114   // Move descriptor registration from one descriptor_data object to another.
0115   BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
0116       per_descriptor_data& target_descriptor_data,
0117       per_descriptor_data& source_descriptor_data);
0118 
0119   // Post a reactor operation for immediate completion.
0120   void post_immediate_completion(operation* op, bool is_continuation) const;
0121 
0122   // Post a reactor operation for immediate completion.
0123   BOOST_ASIO_DECL static void call_post_immediate_completion(
0124       operation* op, bool is_continuation, const void* self);
0125 
0126   // Start a new operation. The reactor operation will be performed when the
0127   // given descriptor is flagged as ready, or an error has occurred.
0128   BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
0129       per_descriptor_data& descriptor_data, reactor_op* op,
0130       bool is_continuation, bool allow_speculative,
0131       void (*on_immediate)(operation*, bool, const void*),
0132       const void* immediate_arg);
0133 
0134   // Start a new operation. The reactor operation will be performed when the
0135   // given descriptor is flagged as ready, or an error has occurred.
0136   void start_op(int op_type, socket_type descriptor,
0137       per_descriptor_data& descriptor_data, reactor_op* op,
0138       bool is_continuation, bool allow_speculative)
0139   {
0140     start_op(op_type, descriptor, descriptor_data,
0141         op, is_continuation, allow_speculative,
0142         &kqueue_reactor::call_post_immediate_completion, this);
0143   }
0144 
0145   // Cancel all operations associated with the given descriptor. The
0146   // handlers associated with the descriptor will be invoked with the
0147   // operation_aborted error.
0148   BOOST_ASIO_DECL void cancel_ops(socket_type descriptor,
0149       per_descriptor_data& descriptor_data);
0150 
0151   // Cancel all operations associated with the given descriptor and key. The
0152   // handlers associated with the descriptor will be invoked with the
0153   // operation_aborted error.
0154   BOOST_ASIO_DECL void cancel_ops_by_key(socket_type descriptor,
0155       per_descriptor_data& descriptor_data,
0156       int op_type, void* cancellation_key);
0157 
0158   // Cancel any operations that are running against the descriptor and remove
0159   // its registration from the reactor. The reactor resources associated with
0160   // the descriptor must be released by calling cleanup_descriptor_data.
0161   BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
0162       per_descriptor_data& descriptor_data, bool closing);
0163 
0164   // Remove the descriptor's registration from the reactor. The reactor
0165   // resources associated with the descriptor must be released by calling
0166   // cleanup_descriptor_data.
0167   BOOST_ASIO_DECL void deregister_internal_descriptor(
0168       socket_type descriptor, per_descriptor_data& descriptor_data);
0169 
0170   // Perform any post-deregistration cleanup tasks associated with the
0171   // descriptor data.
0172   BOOST_ASIO_DECL void cleanup_descriptor_data(
0173       per_descriptor_data& descriptor_data);
0174 
0175   // Add a new timer queue to the reactor.
0176   template <typename Time_Traits>
0177   void add_timer_queue(timer_queue<Time_Traits>& queue);
0178 
0179   // Remove a timer queue from the reactor.
0180   template <typename Time_Traits>
0181   void remove_timer_queue(timer_queue<Time_Traits>& queue);
0182 
0183   // Schedule a new operation in the given timer queue to expire at the
0184   // specified absolute time.
0185   template <typename Time_Traits>
0186   void schedule_timer(timer_queue<Time_Traits>& queue,
0187       const typename Time_Traits::time_type& time,
0188       typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
0189 
0190   // Cancel the timer operations associated with the given token. Returns the
0191   // number of operations that have been posted or dispatched.
0192   template <typename Time_Traits>
0193   std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
0194       typename timer_queue<Time_Traits>::per_timer_data& timer,
0195       std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
0196 
0197   // Cancel the timer operations associated with the given key.
0198   template <typename Time_Traits>
0199   void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
0200       typename timer_queue<Time_Traits>::per_timer_data* timer,
0201       void* cancellation_key);
0202 
0203   // Move the timer operations associated with the given timer.
0204   template <typename Time_Traits>
0205   void move_timer(timer_queue<Time_Traits>& queue,
0206       typename timer_queue<Time_Traits>::per_timer_data& target,
0207       typename timer_queue<Time_Traits>::per_timer_data& source);
0208 
0209   // Run the kqueue loop.
0210   BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
0211 
0212   // Interrupt the kqueue loop.
0213   BOOST_ASIO_DECL void interrupt();
0214 
0215 private:
0216   // Create the kqueue file descriptor. Throws an exception if the descriptor
0217   // cannot be created.
0218   BOOST_ASIO_DECL static int do_kqueue_create();
0219 
0220   // Allocate a new descriptor state object.
0221   BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
0222 
0223   // Free an existing descriptor state object.
0224   BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
0225 
0226   // Helper function to add a new timer queue.
0227   BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
0228 
0229   // Helper function to remove a timer queue.
0230   BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
0231 
0232   // Get the timeout value for the kevent call.
0233   BOOST_ASIO_DECL timespec* get_timeout(long usec, timespec& ts);
0234 
0235   // The scheduler used to post completions.
0236   scheduler& scheduler_;
0237 
0238   // Mutex to protect access to internal data.
0239   mutex mutex_;
0240 
0241   // The kqueue file descriptor.
0242   int kqueue_fd_;
0243 
0244   // The interrupter is used to break a blocking kevent call.
0245   select_interrupter interrupter_;
0246 
0247   // The timer queues.
0248   timer_queue_set timer_queues_;
0249 
0250   // Whether the service has been shut down.
0251   bool shutdown_;
0252 
0253   // Mutex to protect access to the registered descriptors.
0254   mutex registered_descriptors_mutex_;
0255 
0256   // Keep track of all registered descriptors.
0257   object_pool<descriptor_state> registered_descriptors_;
0258 };
0259 
0260 } // namespace detail
0261 } // namespace asio
0262 } // namespace boost
0263 
0264 #include <boost/asio/detail/pop_options.hpp>
0265 
0266 #include <boost/asio/detail/impl/kqueue_reactor.hpp>
0267 #if defined(BOOST_ASIO_HEADER_ONLY)
0268 # include <boost/asio/detail/impl/kqueue_reactor.ipp>
0269 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0270 
0271 #endif // defined(BOOST_ASIO_HAS_KQUEUE)
0272 
0273 #endif // BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP