diff options
Diffstat (limited to '3rdparty/asio/include/asio/impl/thread_pool.ipp')
-rw-r--r-- | 3rdparty/asio/include/asio/impl/thread_pool.ipp | 81 |
1 files changed, 73 insertions, 8 deletions
diff --git a/3rdparty/asio/include/asio/impl/thread_pool.ipp b/3rdparty/asio/include/asio/impl/thread_pool.ipp index f56b94acb40..2e04d2afffc 100644 --- a/3rdparty/asio/include/asio/impl/thread_pool.ipp +++ b/3rdparty/asio/include/asio/impl/thread_pool.ipp @@ -2,7 +2,7 @@ // impl/thread_pool.ipp // ~~~~~~~~~~~~~~~~~~~~ // -// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -16,7 +16,9 @@ #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" +#include <stdexcept> #include "asio/thread_pool.hpp" +#include "asio/detail/throw_exception.hpp" #include "asio/detail/push_options.hpp" @@ -28,28 +30,68 @@ struct thread_pool::thread_function void operator()() { - asio::error_code ec; - scheduler_->run(ec); +#if !defined(ASIO_NO_EXCEPTIONS) + try + { +#endif// !defined(ASIO_NO_EXCEPTIONS) + asio::error_code ec; + scheduler_->run(ec); +#if !defined(ASIO_NO_EXCEPTIONS) + } + catch (...) + { + std::terminate(); + } +#endif// !defined(ASIO_NO_EXCEPTIONS) } }; +#if !defined(ASIO_NO_TS_EXECUTORS) +namespace detail { + +inline long default_thread_pool_size() +{ + std::size_t num_threads = thread::hardware_concurrency() * 2; + num_threads = num_threads == 0 ? 2 : num_threads; + return static_cast<long>(num_threads); +} + +} // namespace detail + thread_pool::thread_pool() - : scheduler_(use_service<detail::scheduler>(*this)) + : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false))), + num_threads_(detail::default_thread_pool_size()) { scheduler_.work_started(); thread_function f = { &scheduler_ }; - std::size_t num_threads = detail::thread::hardware_concurrency() * 2; - threads_.create_threads(f, num_threads ? num_threads : 2); + threads_.create_threads(f, static_cast<std::size_t>(num_threads_)); +} +#endif // !defined(ASIO_NO_TS_EXECUTORS) + +namespace detail { + +inline long clamp_thread_pool_size(std::size_t n) +{ + if (n > 0x7FFFFFFF) + { + std::out_of_range ex("thread pool size"); + asio::detail::throw_exception(ex); + } + return static_cast<long>(n & 0x7FFFFFFF); } +} // namespace detail + thread_pool::thread_pool(std::size_t num_threads) - : scheduler_(use_service<detail::scheduler>(*this)) + : scheduler_(add_scheduler(new detail::scheduler( + *this, num_threads == 1 ? 1 : 0, false))), + num_threads_(detail::clamp_thread_pool_size(num_threads)) { scheduler_.work_started(); thread_function f = { &scheduler_ }; - threads_.create_threads(f, num_threads); + threads_.create_threads(f, static_cast<std::size_t>(num_threads_)); } thread_pool::~thread_pool() @@ -63,8 +105,31 @@ void thread_pool::stop() scheduler_.stop(); } +void thread_pool::attach() +{ + ++num_threads_; + thread_function f = { &scheduler_ }; + f(); +} + void thread_pool::join() { + if (!threads_.empty()) + { + scheduler_.work_finished(); + threads_.join(); + } +} + +detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s) +{ + detail::scoped_ptr<detail::scheduler> scoped_impl(s); + asio::add_service<detail::scheduler>(*this, scoped_impl.get()); + return *scoped_impl.release(); +} + +void thread_pool::wait() +{ scheduler_.work_finished(); threads_.join(); } |