Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // impl/thread_pool.ipp
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_IMPL_THREAD_POOL_IPP
0012 #define BOOST_ASIO_IMPL_THREAD_POOL_IPP
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 <stdexcept>
0020 #include <boost/asio/thread_pool.hpp>
0021 #include <boost/asio/detail/throw_exception.hpp>
0022 
0023 #include <boost/asio/detail/push_options.hpp>
0024 
0025 namespace boost {
0026 namespace asio {
0027 
0028 struct thread_pool::thread_function
0029 {
0030   detail::scheduler* scheduler_;
0031 
0032   void operator()()
0033   {
0034 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0035     try
0036     {
0037 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
0038       boost::system::error_code ec;
0039       scheduler_->run(ec);
0040 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0041     }
0042     catch (...)
0043     {
0044       std::terminate();
0045     }
0046 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
0047   }
0048 };
0049 
0050 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
0051 namespace detail {
0052 
0053 inline long default_thread_pool_size()
0054 {
0055   std::size_t num_threads = thread::hardware_concurrency() * 2;
0056   num_threads = num_threads == 0 ? 2 : num_threads;
0057   return static_cast<long>(num_threads);
0058 }
0059 
0060 } // namespace detail
0061 
0062 thread_pool::thread_pool()
0063   : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false))),
0064     num_threads_(detail::default_thread_pool_size())
0065 {
0066   scheduler_.work_started();
0067 
0068   thread_function f = { &scheduler_ };
0069   threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
0070 }
0071 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
0072 
0073 namespace detail {
0074 
0075 inline long clamp_thread_pool_size(std::size_t n)
0076 {
0077   if (n > 0x7FFFFFFF)
0078   {
0079     std::out_of_range ex("thread pool size");
0080     boost::asio::detail::throw_exception(ex);
0081   }
0082   return static_cast<long>(n & 0x7FFFFFFF);
0083 }
0084 
0085 } // namespace detail
0086 
0087 thread_pool::thread_pool(std::size_t num_threads)
0088   : scheduler_(add_scheduler(new detail::scheduler(
0089           *this, num_threads == 1 ? 1 : 0, false))),
0090     num_threads_(detail::clamp_thread_pool_size(num_threads))
0091 {
0092   scheduler_.work_started();
0093 
0094   thread_function f = { &scheduler_ };
0095   threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
0096 }
0097 
0098 thread_pool::~thread_pool()
0099 {
0100   stop();
0101   join();
0102   shutdown();
0103 }
0104 
0105 void thread_pool::stop()
0106 {
0107   scheduler_.stop();
0108 }
0109 
0110 void thread_pool::attach()
0111 {
0112   ++num_threads_;
0113   thread_function f = { &scheduler_ };
0114   f();
0115 }
0116 
0117 void thread_pool::join()
0118 {
0119   if (num_threads_)
0120     scheduler_.work_finished();
0121 
0122   if (!threads_.empty())
0123     threads_.join();
0124 }
0125 
0126 detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s)
0127 {
0128   detail::scoped_ptr<detail::scheduler> scoped_impl(s);
0129   boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get());
0130   return *scoped_impl.release();
0131 }
0132 
0133 void thread_pool::wait()
0134 {
0135   scheduler_.work_finished();
0136   threads_.join();
0137 }
0138 
0139 } // namespace asio
0140 } // namespace boost
0141 
0142 #include <boost/asio/detail/pop_options.hpp>
0143 
0144 #endif // BOOST_ASIO_IMPL_THREAD_POOL_IPP