123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- //
- // detail/kqueue_reactor.hpp
- // ~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef ASIO_DETAIL_KQUEUE_REACTOR_HPP
- #define ASIO_DETAIL_KQUEUE_REACTOR_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include "asio/detail/config.hpp"
- #if defined(ASIO_HAS_KQUEUE)
- #include <cstddef>
- #include <sys/types.h>
- #include <sys/event.h>
- #include <sys/time.h>
- #include "asio/detail/kqueue_reactor_fwd.hpp"
- #include "asio/detail/mutex.hpp"
- #include "asio/detail/object_pool.hpp"
- #include "asio/detail/op_queue.hpp"
- #include "asio/detail/reactor_op.hpp"
- #include "asio/detail/select_interrupter.hpp"
- #include "asio/detail/socket_types.hpp"
- #include "asio/detail/timer_op.hpp"
- #include "asio/detail/timer_queue_base.hpp"
- #include "asio/detail/timer_queue_fwd.hpp"
- #include "asio/detail/timer_queue_set.hpp"
- #include "asio/error.hpp"
- #include "asio/io_service.hpp"
- // Older versions of Mac OS X may not define EV_OOBAND.
- #if !defined(EV_OOBAND)
- # define EV_OOBAND EV_FLAG1
- #endif // !defined(EV_OOBAND)
- #include "asio/detail/push_options.hpp"
- namespace asio {
- namespace detail {
- class kqueue_reactor
- : public asio::detail::service_base<kqueue_reactor>
- {
- public:
- enum op_types { read_op = 0, write_op = 1,
- connect_op = 1, except_op = 2, max_ops = 3 };
- // Per-descriptor queues.
- struct descriptor_state
- {
- friend class kqueue_reactor;
- friend class object_pool_access;
- mutex mutex_;
- op_queue<reactor_op> op_queue_[max_ops];
- bool shutdown_;
- descriptor_state* next_;
- descriptor_state* prev_;
- };
- // Per-descriptor data.
- typedef descriptor_state* per_descriptor_data;
- // Constructor.
- ASIO_DECL kqueue_reactor(asio::io_service& io_service);
- // Destructor.
- ASIO_DECL ~kqueue_reactor();
- // Destroy all user-defined handler objects owned by the service.
- ASIO_DECL void shutdown_service();
- // Initialise the task.
- ASIO_DECL void init_task();
- // Register a socket with the reactor. Returns 0 on success, system error
- // code on failure.
- ASIO_DECL int register_descriptor(socket_type descriptor,
- per_descriptor_data& descriptor_data);
- // Post a reactor operation for immediate completion.
- void post_immediate_completion(reactor_op* op)
- {
- io_service_.post_immediate_completion(op);
- }
- // Start a new operation. The reactor operation will be performed when the
- // given descriptor is flagged as ready, or an error has occurred.
- ASIO_DECL void start_op(int op_type, socket_type descriptor,
- per_descriptor_data& descriptor_data,
- reactor_op* op, bool allow_speculative);
- // Cancel all operations associated with the given descriptor. The
- // handlers associated with the descriptor will be invoked with the
- // operation_aborted error.
- ASIO_DECL void cancel_ops(socket_type descriptor,
- per_descriptor_data& descriptor_data);
- // Cancel any operations that are running against the descriptor and remove
- // its registration from the reactor.
- ASIO_DECL void close_descriptor(socket_type descriptor,
- per_descriptor_data& descriptor_data);
- // Add a new timer queue to the reactor.
- template <typename Time_Traits>
- void add_timer_queue(timer_queue<Time_Traits>& queue);
- // Remove a timer queue from the reactor.
- template <typename Time_Traits>
- void remove_timer_queue(timer_queue<Time_Traits>& queue);
- // Schedule a new operation in the given timer queue to expire at the
- // specified absolute time.
- template <typename Time_Traits>
- void schedule_timer(timer_queue<Time_Traits>& queue,
- const typename Time_Traits::time_type& time,
- typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
- // Cancel the timer operations associated with the given token. Returns the
- // number of operations that have been posted or dispatched.
- template <typename Time_Traits>
- std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
- typename timer_queue<Time_Traits>::per_timer_data& timer);
- // Run the kqueue loop.
- ASIO_DECL void run(bool block, op_queue<operation>& ops);
- // Interrupt the kqueue loop.
- ASIO_DECL void interrupt();
- private:
- // Create the kqueue file descriptor. Throws an exception if the descriptor
- // cannot be created.
- ASIO_DECL static int do_kqueue_create();
- // Helper function to add a new timer queue.
- ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
- // Helper function to remove a timer queue.
- ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
- // Get the timeout value for the kevent call.
- ASIO_DECL timespec* get_timeout(timespec& ts);
- // The io_service implementation used to post completions.
- io_service_impl& io_service_;
- // Mutex to protect access to internal data.
- mutex mutex_;
- // The kqueue file descriptor.
- int kqueue_fd_;
- // The interrupter is used to break a blocking kevent call.
- select_interrupter interrupter_;
- // The timer queues.
- timer_queue_set timer_queues_;
- // Whether the service has been shut down.
- bool shutdown_;
- // Mutex to protect access to the registered descriptors.
- mutex registered_descriptors_mutex_;
- // Keep track of all registered descriptors.
- object_pool<descriptor_state> registered_descriptors_;
- };
- } // namespace detail
- } // namespace asio
- #include "asio/detail/pop_options.hpp"
- #include "asio/detail/impl/kqueue_reactor.hpp"
- #if defined(ASIO_HEADER_ONLY)
- # include "asio/detail/impl/kqueue_reactor.ipp"
- #endif // defined(ASIO_HEADER_ONLY)
- #endif // defined(ASIO_HAS_KQUEUE)
- #endif // ASIO_DETAIL_KQUEUE_REACTOR_HPP
|