File indexing completed on 2025-01-18 09:28:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_IMPL_SYSTEM_CONTEXT_IPP
0012 #define BOOST_ASIO_IMPL_SYSTEM_CONTEXT_IPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/system_context.hpp>
0020
0021 #include <boost/asio/detail/push_options.hpp>
0022
0023 namespace boost {
0024 namespace asio {
0025
0026 struct system_context::thread_function
0027 {
0028 detail::scheduler* scheduler_;
0029
0030 void operator()()
0031 {
0032 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0033 try
0034 {
0035 #endif
0036 boost::system::error_code ec;
0037 scheduler_->run(ec);
0038 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0039 }
0040 catch (...)
0041 {
0042 std::terminate();
0043 }
0044 #endif
0045 }
0046 };
0047
0048 system_context::system_context()
0049 : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false)))
0050 {
0051 scheduler_.work_started();
0052
0053 thread_function f = { &scheduler_ };
0054 num_threads_ = detail::thread::hardware_concurrency() * 2;
0055 num_threads_ = num_threads_ ? num_threads_ : 2;
0056 threads_.create_threads(f, num_threads_);
0057 }
0058
0059 system_context::~system_context()
0060 {
0061 scheduler_.work_finished();
0062 scheduler_.stop();
0063 threads_.join();
0064 }
0065
0066 void system_context::stop()
0067 {
0068 scheduler_.stop();
0069 }
0070
0071 bool system_context::stopped() const noexcept
0072 {
0073 return scheduler_.stopped();
0074 }
0075
0076 void system_context::join()
0077 {
0078 scheduler_.work_finished();
0079 threads_.join();
0080 }
0081
0082 detail::scheduler& system_context::add_scheduler(detail::scheduler* s)
0083 {
0084 detail::scoped_ptr<detail::scheduler> scoped_impl(s);
0085 boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get());
0086 return *scoped_impl.release();
0087 }
0088
0089 }
0090 }
0091
0092 #include <boost/asio/detail/pop_options.hpp>
0093
0094 #endif