Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/blocking_executor_op.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP
0012 #define BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/event.hpp>
0020 #include <boost/asio/detail/fenced_block.hpp>
0021 #include <boost/asio/detail/mutex.hpp>
0022 #include <boost/asio/detail/scheduler_operation.hpp>
0023 
0024 #include <boost/asio/detail/push_options.hpp>
0025 
0026 namespace boost {
0027 namespace asio {
0028 namespace detail {
0029 
0030 template <typename Operation = scheduler_operation>
0031 class blocking_executor_op_base : public Operation
0032 {
0033 public:
0034   blocking_executor_op_base(typename Operation::func_type complete_func)
0035     : Operation(complete_func),
0036       is_complete_(false)
0037   {
0038   }
0039 
0040   void wait()
0041   {
0042     boost::asio::detail::mutex::scoped_lock lock(mutex_);
0043     while (!is_complete_)
0044       event_.wait(lock);
0045   }
0046 
0047 protected:
0048   struct do_complete_cleanup
0049   {
0050     ~do_complete_cleanup()
0051     {
0052       boost::asio::detail::mutex::scoped_lock lock(op_->mutex_);
0053       op_->is_complete_ = true;
0054       op_->event_.unlock_and_signal_one_for_destruction(lock);
0055     }
0056 
0057     blocking_executor_op_base* op_;
0058   };
0059 
0060 private:
0061   boost::asio::detail::mutex mutex_;
0062   boost::asio::detail::event event_;
0063   bool is_complete_;
0064 };
0065 
0066 template <typename Handler, typename Operation = scheduler_operation>
0067 class blocking_executor_op : public blocking_executor_op_base<Operation>
0068 {
0069 public:
0070   blocking_executor_op(Handler& h)
0071     : blocking_executor_op_base<Operation>(&blocking_executor_op::do_complete),
0072       handler_(h)
0073   {
0074   }
0075 
0076   static void do_complete(void* owner, Operation* base,
0077       const boost::system::error_code& /*ec*/,
0078       std::size_t /*bytes_transferred*/)
0079   {
0080     BOOST_ASIO_ASSUME(base != 0);
0081     blocking_executor_op* o(static_cast<blocking_executor_op*>(base));
0082 
0083     typename blocking_executor_op_base<Operation>::do_complete_cleanup
0084       on_exit = { o };
0085     (void)on_exit;
0086 
0087     BOOST_ASIO_HANDLER_COMPLETION((*o));
0088 
0089     // Make the upcall if required.
0090     if (owner)
0091     {
0092       fenced_block b(fenced_block::half);
0093       BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
0094       static_cast<Handler&&>(o->handler_)();
0095       BOOST_ASIO_HANDLER_INVOCATION_END;
0096     }
0097   }
0098 
0099 private:
0100   Handler& handler_;
0101 };
0102 
0103 } // namespace detail
0104 } // namespace asio
0105 } // namespace boost
0106 
0107 #include <boost/asio/detail/pop_options.hpp>
0108 
0109 #endif // BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP