|
13 | 13 | #include <network/concurrency/thread_pool.hpp>
|
14 | 14 | #include <boost/scope_exit.hpp>
|
15 | 15 |
|
16 |
| -namespace network { namespace concurrency { |
17 |
| - |
18 |
| - struct thread_pool_pimpl { |
19 |
| - thread_pool_pimpl(std::size_t threads = 1, |
20 |
| - io_service_ptr io_service = io_service_ptr(), |
21 |
| - std::vector<std::thread> worker_threads = std::vector<std::thread>()) |
22 |
| - : threads_(threads) |
23 |
| - , io_service_(io_service) |
24 |
| - , worker_threads_(std::move(worker_threads)) |
25 |
| - , sentinel_() |
26 |
| - { |
27 |
| - bool commit = false; |
28 |
| - BOOST_SCOPE_EXIT((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) { |
29 |
| - if (!commit) { |
30 |
| - sentinel_.reset(); |
31 |
| - io_service_.reset(); |
32 |
| - for (auto& thread : worker_threads_) |
33 |
| - if (thread.joinable()) thread.join(); |
34 |
| - worker_threads_.clear(); |
35 |
| - } |
36 |
| - } BOOST_SCOPE_EXIT_END |
37 |
| - |
38 |
| - if (!io_service_.get()) io_service_.reset(new boost::asio::io_service); |
39 |
| - if (!sentinel_.get()) |
40 |
| - sentinel_.reset(new boost::asio::io_service::work(*io_service_)); |
41 |
| - auto local_io_service = io_service_; |
42 |
| - for (std::size_t counter = 0; counter < threads_; ++counter) |
43 |
| - worker_threads_.emplace_back([local_io_service](){ |
44 |
| - local_io_service->run();}); |
45 |
| - |
46 |
| - commit = true; |
| 16 | +namespace network { |
| 17 | +namespace concurrency { |
| 18 | + |
| 19 | +struct thread_pool_pimpl { |
| 20 | + thread_pool_pimpl( |
| 21 | + std::size_t threads = 1, |
| 22 | + io_service_ptr io_service = io_service_ptr(), |
| 23 | + std::vector<std::thread> worker_threads = std::vector<std::thread>()) |
| 24 | + : threads_(threads), |
| 25 | + io_service_(io_service), |
| 26 | + worker_threads_(std::move(worker_threads)), |
| 27 | + sentinel_() { |
| 28 | + bool commit = false; |
| 29 | + BOOST_SCOPE_EXIT((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) { |
| 30 | + if (!commit) { |
| 31 | + sentinel_.reset(); |
| 32 | + io_service_.reset(); |
| 33 | + for (auto& thread : worker_threads_) |
| 34 | + if (thread.joinable()) |
| 35 | + thread.join(); |
| 36 | + worker_threads_.clear(); |
| 37 | + } |
47 | 38 | }
|
| 39 | + BOOST_SCOPE_EXIT_END if (!io_service_.get()) |
| 40 | + io_service_.reset(new boost::asio::io_service); |
| 41 | + if (!sentinel_.get()) |
| 42 | + sentinel_.reset(new boost::asio::io_service::work(*io_service_)); |
| 43 | + auto local_io_service = io_service_; |
| 44 | + for (std::size_t counter = 0; counter < threads_; ++counter) |
| 45 | + worker_threads_.emplace_back([local_io_service]() { |
| 46 | + local_io_service->run(); |
| 47 | + }); |
| 48 | + |
| 49 | + commit = true; |
| 50 | + } |
48 | 51 |
|
49 | 52 | #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
|
50 |
| - thread_pool_pimpl(thread_pool_pimpl const &) = delete; |
51 |
| - thread_pool_pimpl & operator=(thread_pool_pimpl const &) = delete; |
52 |
| -#endif // !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) |
| 53 | + thread_pool_pimpl(thread_pool_pimpl const&) = delete; |
| 54 | + thread_pool_pimpl& operator=(thread_pool_pimpl const&) = delete; |
| 55 | +#endif // !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) |
53 | 56 |
|
54 |
| - thread_pool_pimpl(thread_pool_pimpl&& other) { |
55 |
| - other.swap(*this); |
56 |
| - } |
| 57 | + thread_pool_pimpl(thread_pool_pimpl && other) { other.swap(*this); } |
57 | 58 |
|
58 |
| - std::size_t const thread_count() const { |
59 |
| - return threads_; |
60 |
| - } |
| 59 | + std::size_t const thread_count() const { return threads_; } |
61 | 60 |
|
62 |
| - void post(std::function<void()> f) { |
63 |
| - io_service_->post(f); |
64 |
| - } |
| 61 | + void post(std::function<void()> f) { io_service_->post(f); } |
65 | 62 |
|
66 |
| - ~thread_pool_pimpl() { |
67 |
| - sentinel_.reset(); |
68 |
| - try { |
69 |
| - for (auto& thread : worker_threads_) thread.join(); |
70 |
| - } catch (...) { |
71 |
| - BOOST_ASSERT(false && "A handler was not supposed to throw, but one did."); |
72 |
| - std::abort(); |
73 |
| - } |
| 63 | + ~thread_pool_pimpl() { |
| 64 | + sentinel_.reset(); |
| 65 | + try { |
| 66 | + for (auto& thread : worker_threads_) |
| 67 | + thread.join(); |
74 | 68 | }
|
75 |
| - |
76 |
| - void swap(thread_pool_pimpl & other) { |
77 |
| - using std::swap; |
78 |
| - swap(other.threads_, threads_); |
79 |
| - swap(other.io_service_, io_service_); |
80 |
| - swap(other.worker_threads_, worker_threads_); |
81 |
| - swap(other.sentinel_, sentinel_); |
| 69 | + catch (...) { |
| 70 | + BOOST_ASSERT(false && |
| 71 | + "A handler was not supposed to throw, but one did."); |
| 72 | + std::abort(); |
82 | 73 | }
|
83 |
| - |
84 |
| - protected: |
85 |
| - std::size_t threads_; |
86 |
| - io_service_ptr io_service_; |
87 |
| - std::vector<std::thread> worker_threads_; |
88 |
| - sentinel_ptr sentinel_; |
89 |
| - }; |
90 |
| - |
91 |
| - thread_pool::thread_pool(std::size_t threads, |
92 |
| - io_service_ptr io_service, |
93 |
| - std::vector<std::thread> worker_threads) |
94 |
| - : pimpl(new (std::nothrow) thread_pool_pimpl(threads, io_service, std::move(worker_threads))) |
95 |
| - {} |
96 |
| - |
97 |
| - std::size_t const thread_pool::thread_count() const { |
98 |
| - return pimpl->thread_count(); |
99 | 74 | }
|
100 | 75 |
|
101 |
| - void thread_pool::post(std::function<void()> f) { |
102 |
| - pimpl->post(f); |
| 76 | + void swap(thread_pool_pimpl& other) { |
| 77 | + using std::swap; |
| 78 | + swap(other.threads_, threads_); |
| 79 | + swap(other.io_service_, io_service_); |
| 80 | + swap(other.worker_threads_, worker_threads_); |
| 81 | + swap(other.sentinel_, sentinel_); |
103 | 82 | }
|
104 | 83 |
|
105 |
| - void thread_pool::swap(thread_pool & other) { |
106 |
| - std::swap(other.pimpl, this->pimpl); |
107 |
| - } |
| 84 | + protected: |
| 85 | + std::size_t threads_; |
| 86 | + io_service_ptr io_service_; |
| 87 | + std::vector<std::thread> worker_threads_; |
| 88 | + sentinel_ptr sentinel_; |
| 89 | +}; |
108 | 90 |
|
109 |
| - thread_pool::~thread_pool() { |
110 |
| - delete pimpl; |
111 |
| - } |
| 91 | +thread_pool::thread_pool(std::size_t threads, |
| 92 | + io_service_ptr io_service, |
| 93 | + std::vector<std::thread> worker_threads) |
| 94 | + : pimpl( |
| 95 | + new (std::nothrow) |
| 96 | + thread_pool_pimpl(threads, io_service, std::move(worker_threads))) {} |
| 97 | + |
| 98 | +std::size_t const thread_pool::thread_count() const { |
| 99 | + return pimpl->thread_count(); |
| 100 | +} |
| 101 | + |
| 102 | +void thread_pool::post(std::function<void()> f) { pimpl->post(f); } |
| 103 | + |
| 104 | +void thread_pool::swap(thread_pool& other) { |
| 105 | + std::swap(other.pimpl, this->pimpl); |
| 106 | +} |
| 107 | + |
| 108 | +thread_pool::~thread_pool() { delete pimpl; } |
112 | 109 |
|
113 | 110 | } // namespace concurrency
|
114 | 111 | } // namespace network
|
|
0 commit comments