123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- #ifndef BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
- #define BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/config.hpp>
- #include <cstddef>
- #include <signal.h>
- #include <boost/asio/associated_cancellation_slot.hpp>
- #include <boost/asio/cancellation_type.hpp>
- #include <boost/asio/error.hpp>
- #include <boost/asio/execution_context.hpp>
- #include <boost/asio/signal_set_base.hpp>
- #include <boost/asio/detail/handler_alloc_helpers.hpp>
- #include <boost/asio/detail/memory.hpp>
- #include <boost/asio/detail/op_queue.hpp>
- #include <boost/asio/detail/signal_handler.hpp>
- #include <boost/asio/detail/signal_op.hpp>
- #include <boost/asio/detail/socket_types.hpp>
- #if defined(BOOST_ASIO_HAS_IOCP)
- # include <boost/asio/detail/win_iocp_io_context.hpp>
- #else
- # include <boost/asio/detail/scheduler.hpp>
- #endif
- #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
- # if defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
- # include <boost/asio/detail/io_uring_service.hpp>
- # else
- # include <boost/asio/detail/reactor.hpp>
- # endif
- #endif
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace detail {
- #if defined(NSIG) && (NSIG > 0)
- enum { max_signal_number = NSIG };
- #else
- enum { max_signal_number = 128 };
- #endif
- extern BOOST_ASIO_DECL struct signal_state* get_signal_state();
- extern "C" BOOST_ASIO_DECL void boost_asio_signal_handler(int signal_number);
- class signal_set_service :
- public execution_context_service_base<signal_set_service>
- {
- public:
-
- class registration
- {
- public:
-
- registration()
- : signal_number_(0),
- queue_(0),
- undelivered_(0),
- next_in_table_(0),
- prev_in_table_(0),
- next_in_set_(0)
- {
- }
- private:
-
- friend class signal_set_service;
-
- int signal_number_;
-
- op_queue<signal_op>* queue_;
-
- std::size_t undelivered_;
-
- registration* next_in_table_;
- registration* prev_in_table_;
-
- registration* next_in_set_;
- };
-
- class implementation_type
- {
- public:
-
- implementation_type()
- : signals_(0)
- {
- }
- private:
-
- friend class signal_set_service;
-
- op_queue<signal_op> queue_;
-
- registration* signals_;
- };
-
- BOOST_ASIO_DECL signal_set_service(execution_context& context);
-
- BOOST_ASIO_DECL ~signal_set_service();
-
- BOOST_ASIO_DECL void shutdown();
-
- BOOST_ASIO_DECL void notify_fork(
- boost::asio::execution_context::fork_event fork_ev);
-
- BOOST_ASIO_DECL void construct(implementation_type& impl);
-
- BOOST_ASIO_DECL void destroy(implementation_type& impl);
-
- boost::system::error_code add(implementation_type& impl,
- int signal_number, boost::system::error_code& ec)
- {
- return add(impl, signal_number, signal_set_base::flags::dont_care, ec);
- }
-
- BOOST_ASIO_DECL boost::system::error_code add(implementation_type& impl,
- int signal_number, signal_set_base::flags_t f,
- boost::system::error_code& ec);
-
- BOOST_ASIO_DECL boost::system::error_code remove(implementation_type& impl,
- int signal_number, boost::system::error_code& ec);
-
- BOOST_ASIO_DECL boost::system::error_code clear(implementation_type& impl,
- boost::system::error_code& ec);
-
- BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
- boost::system::error_code& ec);
-
- BOOST_ASIO_DECL void cancel_ops_by_key(implementation_type& impl,
- void* cancellation_key);
-
- template <typename Handler, typename IoExecutor>
- void async_wait(implementation_type& impl,
- Handler& handler, const IoExecutor& io_ex)
- {
- associated_cancellation_slot_t<Handler> slot
- = boost::asio::get_associated_cancellation_slot(handler);
-
- typedef signal_handler<Handler, IoExecutor> op;
- typename op::ptr p = { boost::asio::detail::addressof(handler),
- op::ptr::allocate(handler), 0 };
- p.p = new (p.v) op(handler, io_ex);
-
- if (slot.is_connected())
- {
- p.p->cancellation_key_ =
- &slot.template emplace<signal_op_cancellation>(this, &impl);
- }
- BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
- *p.p, "signal_set", &impl, 0, "async_wait"));
- start_wait_op(impl, p.p);
- p.v = p.p = 0;
- }
-
- BOOST_ASIO_DECL static void deliver_signal(int signal_number);
- private:
-
- BOOST_ASIO_DECL static void add_service(signal_set_service* service);
-
- BOOST_ASIO_DECL static void remove_service(signal_set_service* service);
-
- BOOST_ASIO_DECL static void open_descriptors();
-
- BOOST_ASIO_DECL static void close_descriptors();
-
- BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, signal_op* op);
-
- class signal_op_cancellation
- {
- public:
- signal_op_cancellation(signal_set_service* s, implementation_type* i)
- : service_(s),
- implementation_(i)
- {
- }
- void operator()(cancellation_type_t type)
- {
- if (!!(type &
- (cancellation_type::terminal
- | cancellation_type::partial
- | cancellation_type::total)))
- {
- service_->cancel_ops_by_key(*implementation_, this);
- }
- }
- private:
- signal_set_service* service_;
- implementation_type* implementation_;
- };
-
- #if defined(BOOST_ASIO_HAS_IOCP)
- typedef class win_iocp_io_context scheduler_impl;
- #else
- typedef class scheduler scheduler_impl;
- #endif
- scheduler_impl& scheduler_;
- #if !defined(BOOST_ASIO_WINDOWS) \
- && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
- && !defined(__CYGWIN__)
-
- class pipe_read_op;
- # if defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
-
- io_uring_service& io_uring_service_;
-
- io_uring_service::per_io_object_data io_object_data_;
- # else
-
- reactor& reactor_;
-
- reactor::per_descriptor_data reactor_data_;
- # endif
- #endif
-
-
-
- registration* registrations_[max_signal_number];
-
- signal_set_service* next_;
- signal_set_service* prev_;
- };
- }
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #if defined(BOOST_ASIO_HEADER_ONLY)
- # include <boost/asio/detail/impl/signal_set_service.ipp>
- #endif
- #endif
|