Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:06

0001 // Copyright (c) 2016 Klemens D. Morgenstern
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_PROCESS_POSIX_IO_CONTEXT_REF_HPP_
0007 #define BOOST_PROCESS_POSIX_IO_CONTEXT_REF_HPP_
0008 
0009 #include <boost/process/detail/posix/handler.hpp>
0010 #include <boost/process/detail/posix/async_handler.hpp>
0011 #include <boost/asio/io_context.hpp>
0012 
0013 #include <boost/fusion/algorithm/iteration/for_each.hpp>
0014 #include <boost/fusion/algorithm/transformation/filter_if.hpp>
0015 #include <boost/fusion/algorithm/transformation/transform.hpp>
0016 #include <boost/fusion/view/transform_view.hpp>
0017 #include <boost/fusion/container/vector/convert.hpp>
0018 
0019 
0020 #include <boost/process/detail/posix/sigchld_service.hpp>
0021 #include <boost/process/detail/posix/is_running.hpp>
0022 
0023 #include <functional>
0024 #include <type_traits>
0025 #include <memory>
0026 #include <vector>
0027 #include <sys/wait.h>
0028 
0029 namespace boost { namespace process { namespace detail { namespace posix {
0030 
0031 template<typename Executor>
0032 struct on_exit_handler_transformer
0033 {
0034     Executor & exec;
0035     on_exit_handler_transformer(Executor & exec) : exec(exec) {}
0036     template<typename Sig>
0037     struct result;
0038 
0039     template<typename T>
0040     struct result<on_exit_handler_transformer<Executor>(T&)>
0041     {
0042         typedef typename T::on_exit_handler_t type;
0043     };
0044 
0045     template<typename T>
0046     auto operator()(T& t) const -> typename T::on_exit_handler_t
0047     {
0048         return t.on_exit_handler(exec);
0049     }
0050 };
0051 
0052 template<typename Executor>
0053 struct async_handler_collector
0054 {
0055     Executor & exec;
0056     std::vector<std::function<void(int, const std::error_code & ec)>> &handlers;
0057 
0058 
0059     async_handler_collector(Executor & exec,
0060             std::vector<std::function<void(int, const std::error_code & ec)>> &handlers)
0061                 : exec(exec), handlers(handlers) {}
0062 
0063     template<typename T>
0064     void operator()(T & t) const
0065     {
0066         handlers.push_back(t.on_exit_handler(exec));
0067     }
0068 };
0069 
0070 //Also set's up waiting for the exit, so it can close async stuff.
0071 struct io_context_ref : handler_base_ext
0072 {
0073     io_context_ref(boost::asio::io_context & ios) : ios(ios)
0074     {
0075 
0076     }
0077     boost::asio::io_context &get() {return ios;};
0078     
0079     template <class Executor>
0080     void on_success(Executor& exec)
0081     {
0082         ios.notify_fork(boost::asio::io_context::fork_parent);
0083         //must be on the heap, so I can move it into the lambda.
0084         auto asyncs = boost::fusion::filter_if<
0085                         is_async_handler<
0086                         typename std::remove_reference< boost::mpl::_ > ::type
0087                         >>(exec.seq);
0088 
0089         //ok, check if there are actually any.
0090         if (boost::fusion::empty(asyncs))
0091             return;
0092 
0093         std::vector<std::function<void(int, const std::error_code & ec)>> funcs;
0094         funcs.reserve(boost::fusion::size(asyncs));
0095         boost::fusion::for_each(asyncs, async_handler_collector<Executor>(exec, funcs));
0096 
0097         auto & es = exec.exit_status;
0098 
0099         auto wh = [funcs, es](int val, const std::error_code & ec)
0100                 {
0101                     es->store(val);
0102                     for (auto & func : funcs)
0103                         func(::boost::process::detail::posix::eval_exit_status(val), ec);
0104                 };
0105 
0106         sigchld_service.async_wait(exec.pid, std::move(wh));
0107     }
0108 
0109     template<typename Executor>
0110     void on_setup (Executor &) const {/*ios.notify_fork(boost::asio::io_context::fork_prepare);*/}
0111 
0112     template<typename Executor>
0113     void on_exec_setup  (Executor &) const {/*ios.notify_fork(boost::asio::io_context::fork_child);*/}
0114 
0115     template <class Executor>
0116     void on_error(Executor&, const std::error_code &) const {/*ios.notify_fork(boost::asio::io_context::fork_parent);*/}
0117 
0118 private:
0119     boost::asio::io_context &ios;
0120     boost::process::detail::posix::sigchld_service &sigchld_service = boost::asio::use_service<boost::process::detail::posix::sigchld_service>(ios);
0121 };
0122 
0123 }}}}
0124 
0125 #endif /* BOOST_PROCESS_WINDOWS_IO_CONTEXT_REF_HPP_ */