|
7 | 7 | // http://www.boost.org/LICENSE_1_0.txt)
|
8 | 8 |
|
9 | 9 | #include <cstddef>
|
10 |
| -#include <boost/network/tags.hpp> |
11 | 10 | #include <boost/thread/thread.hpp>
|
12 | 11 | #include <boost/shared_ptr.hpp>
|
13 | 12 | #include <boost/function.hpp>
|
|
16 | 15 |
|
17 | 16 | namespace boost { namespace network { namespace utils {
|
18 | 17 |
|
19 |
| - typedef boost::shared_ptr<boost::asio::io_service> io_service_ptr; |
20 |
| - typedef boost::shared_ptr<boost::thread_group> worker_threads_ptr; |
21 |
| - typedef boost::shared_ptr<boost::asio::io_service::work> sentinel_ptr; |
22 |
| - |
23 |
| - template <class Tag> |
24 |
| - struct basic_thread_pool { |
25 |
| - basic_thread_pool( |
26 |
| - std::size_t threads = 1, |
27 |
| - io_service_ptr io_service = io_service_ptr(), |
28 |
| - worker_threads_ptr worker_threads = worker_threads_ptr() |
29 |
| - ) |
30 |
| - : threads_(threads) |
31 |
| - , io_service_(io_service) |
32 |
| - , worker_threads_(worker_threads) |
33 |
| - , sentinel_() |
34 |
| - { |
35 |
| - bool commit = false; |
36 |
| - BOOST_SCOPE_EXIT_TPL((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) { |
37 |
| - if (!commit) { |
38 |
| - sentinel_.reset(); |
39 |
| - io_service_.reset(); |
40 |
| - if (worker_threads_.get()) { |
41 |
| - worker_threads_->interrupt_all(); |
42 |
| - worker_threads_->join_all(); |
43 |
| - } |
44 |
| - worker_threads_.reset(); |
45 |
| - } |
46 |
| - } BOOST_SCOPE_EXIT_END |
| 18 | +typedef boost::shared_ptr<boost::asio::io_service> io_service_ptr; |
| 19 | +typedef boost::shared_ptr<boost::thread_group> worker_threads_ptr; |
| 20 | +typedef boost::shared_ptr<boost::asio::io_service::work> sentinel_ptr; |
47 | 21 |
|
48 |
| - if (!io_service_.get()) { |
49 |
| - io_service_.reset(new boost::asio::io_service); |
50 |
| - } |
| 22 | +struct thread_pool_pimpl; |
51 | 23 |
|
52 |
| - if (!worker_threads_.get()) { |
53 |
| - worker_threads_.reset(new boost::thread_group); |
54 |
| - } |
| 24 | +struct thread_pool { |
| 25 | + thread_pool(std::size_t threads = 1, |
| 26 | + io_service_ptr io_service = io_service_ptr(), |
| 27 | + worker_threads_ptr worker_threads = worker_threads_ptr()); |
| 28 | + std::size_t const thread_count() const; |
| 29 | + void post(function<void()> f); |
| 30 | + ~thread_pool(); |
| 31 | + void swap(thread_pool & other); |
| 32 | + protected: |
| 33 | + thread_pool_pimpl * pimpl; |
| 34 | +}; |
55 | 35 |
|
56 |
| - if (!sentinel_.get()) { |
57 |
| - sentinel_.reset(new boost::asio::io_service::work(*io_service_)); |
58 |
| - } |
| 36 | +inline void swap(thread_pool & l, thread_pool & r) { |
| 37 | + l.swap(r); |
| 38 | +} |
59 | 39 |
|
60 |
| - for (std::size_t counter = 0; counter < threads_; ++counter) |
61 |
| - worker_threads_->create_thread( |
62 |
| - boost::bind( |
63 |
| - &boost::asio::io_service::run, |
64 |
| - io_service_ |
65 |
| - ) |
66 |
| - ); |
| 40 | +} // utils |
67 | 41 |
|
68 |
| - commit = true; |
69 |
| - } |
| 42 | +} // network |
70 | 43 |
|
71 |
| - std::size_t const thread_count() const { |
72 |
| - return threads_; |
73 |
| - } |
74 |
| - |
75 |
| - void post(boost::function<void()> f) { |
76 |
| - io_service_->post(f); |
77 |
| - } |
78 |
| - |
79 |
| - ~basic_thread_pool() throw () { |
80 |
| - sentinel_.reset(); |
81 |
| - try { |
82 |
| - worker_threads_->join_all(); |
83 |
| - } catch (...) { |
84 |
| - BOOST_ASSERT(false && "A handler was not supposed to throw, but one did."); |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - void swap(basic_thread_pool & other) { |
89 |
| - std::swap(other.threads_, threads_); |
90 |
| - std::swap(other.io_service_, io_service_); |
91 |
| - std::swap(other.worker_threads_, worker_threads_); |
92 |
| - std::swap(other.sentinel_, sentinel_); |
93 |
| - } |
94 |
| - protected: |
95 |
| - std::size_t threads_; |
96 |
| - io_service_ptr io_service_; |
97 |
| - worker_threads_ptr worker_threads_; |
98 |
| - sentinel_ptr sentinel_; |
99 |
| - |
100 |
| - private: |
101 |
| - basic_thread_pool(basic_thread_pool const &); // no copies please |
102 |
| - basic_thread_pool & operator=(basic_thread_pool); // no assignment please |
103 |
| - }; |
104 |
| - |
105 |
| - typedef basic_thread_pool<tags::default_> thread_pool; |
106 |
| - |
107 |
| -} /* utils */ |
108 |
| - |
109 |
| -} /* network */ |
110 |
| - |
111 |
| -} /* boost */ |
| 44 | +} // boost |
112 | 45 |
|
113 | 46 | #endif /* BOOST_NETWORK_UTILS_THREAD_POOL_HPP_20101020 */
|
0 commit comments