|
| 1 | +#ifndef BOOST_NETWORK_UTILS_THREAD_POOL_IPP_20111021 |
| 2 | +#define BOOST_NETWORK_UTILS_THREAD_POOL_IPP_20111021 |
| 3 | + |
| 4 | +// Copyright 2011 Dean Michael Berris <dberris@google.com>. |
| 5 | +// Copyright 2011 Google, Inc. |
| 6 | +// Distributed under the Boost Software License, Version 1.0. |
| 7 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 9 | + |
| 10 | +#include <boost/network/utils/thread_pool.hpp> |
| 11 | + |
| 12 | +namespace boost { namespace network { namespace utils { |
| 13 | + |
| 14 | +struct thread_pool_pimpl { |
| 15 | + thread_pool_pimpl( |
| 16 | + std::size_t threads = 1, |
| 17 | + io_service_ptr io_service = io_service_ptr(), |
| 18 | + worker_threads_ptr worker_threads = worker_threads_ptr() |
| 19 | + ) |
| 20 | + : threads_(threads) |
| 21 | + , io_service_(io_service) |
| 22 | + , worker_threads_(worker_threads) |
| 23 | + , sentinel_() |
| 24 | + { |
| 25 | + bool commit = false; |
| 26 | + BOOST_SCOPE_EXIT((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) { |
| 27 | + if (!commit) { |
| 28 | + sentinel_.reset(); |
| 29 | + io_service_.reset(); |
| 30 | + if (worker_threads_.get()) { |
| 31 | + worker_threads_->interrupt_all(); |
| 32 | + worker_threads_->join_all(); |
| 33 | + } |
| 34 | + worker_threads_.reset(); |
| 35 | + } |
| 36 | + } BOOST_SCOPE_EXIT_END |
| 37 | + |
| 38 | + if (!io_service_.get()) { |
| 39 | + io_service_.reset(new boost::asio::io_service); |
| 40 | + } |
| 41 | + |
| 42 | + if (!worker_threads_.get()) { |
| 43 | + worker_threads_.reset(new boost::thread_group); |
| 44 | + } |
| 45 | + |
| 46 | + if (!sentinel_.get()) { |
| 47 | + sentinel_.reset(new boost::asio::io_service::work(*io_service_)); |
| 48 | + } |
| 49 | + |
| 50 | + for (std::size_t counter = 0; counter < threads_; ++counter) |
| 51 | + worker_threads_->create_thread( |
| 52 | + boost::bind( |
| 53 | + &boost::asio::io_service::run, |
| 54 | + io_service_ |
| 55 | + ) |
| 56 | + ); |
| 57 | + |
| 58 | + commit = true; |
| 59 | + } |
| 60 | + |
| 61 | + std::size_t const thread_count() const { |
| 62 | + return threads_; |
| 63 | + } |
| 64 | + |
| 65 | + void post(boost::function<void()> f) { |
| 66 | + io_service_->post(f); |
| 67 | + } |
| 68 | + |
| 69 | + ~thread_pool_pimpl() { |
| 70 | + sentinel_.reset(); |
| 71 | + try { |
| 72 | + worker_threads_->join_all(); |
| 73 | + } catch (...) { |
| 74 | + BOOST_ASSERT(false && "A handler was not supposed to throw, but one did."); |
| 75 | + std::abort(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + void swap(thread_pool_pimpl & other) { |
| 80 | + std::swap(other.threads_, threads_); |
| 81 | + std::swap(other.io_service_, io_service_); |
| 82 | + std::swap(other.worker_threads_, worker_threads_); |
| 83 | + std::swap(other.sentinel_, sentinel_); |
| 84 | + } |
| 85 | +protected: |
| 86 | + std::size_t threads_; |
| 87 | + io_service_ptr io_service_; |
| 88 | + worker_threads_ptr worker_threads_; |
| 89 | + sentinel_ptr sentinel_; |
| 90 | + |
| 91 | +private: |
| 92 | + thread_pool_pimpl(thread_pool_pimpl const &); // no copies please |
| 93 | + thread_pool_pimpl & operator=(thread_pool_pimpl); // no assignment please |
| 94 | +}; |
| 95 | + |
| 96 | +thread_pool::thread_pool(std::size_t threads, |
| 97 | + io_service_ptr io_service, |
| 98 | + worker_threads_ptr worker_threads) |
| 99 | +: pimpl(new (std::nothrow) thread_pool_pimpl(threads, io_service, worker_threads)) |
| 100 | +{} |
| 101 | + |
| 102 | +std::size_t const thread_pool::thread_count() const { |
| 103 | + return pimpl->thread_count(); |
| 104 | +} |
| 105 | + |
| 106 | +void thread_pool::post(function<void()> f) { |
| 107 | + pimpl->post(f); |
| 108 | +} |
| 109 | + |
| 110 | +void thread_pool::swap(thread_pool & other) { |
| 111 | + std::swap(other.pimpl, this->pimpl); |
| 112 | +} |
| 113 | + |
| 114 | +thread_pool::~thread_pool() { |
| 115 | + delete pimpl; |
| 116 | +} |
| 117 | + |
| 118 | +} /* utils */ |
| 119 | + |
| 120 | +} /* network */ |
| 121 | + |
| 122 | +} /* boost */ |
| 123 | + |
| 124 | +#endif /* BOOST_NETWORK_UTILS_THREAD_POOL_IPP_20111021 */ |
0 commit comments