|
9 | 9 |
|
10 | 10 | #include <boost/rts/application.hpp> |
11 | 11 | #include <boost/rts/detail/except.hpp> |
| 12 | +#include <boost/assert.hpp> |
12 | 13 | #include <mutex> |
13 | 14 | #include <vector> |
14 | 15 |
|
|
55 | 56 | application:: |
56 | 57 | start() |
57 | 58 | { |
| 59 | + struct action |
58 | 60 | { |
59 | | - std::lock_guard<std::mutex> lock(impl_->m); |
60 | | - if(impl_->st != state::none) |
| 61 | + action(application& self) |
| 62 | + : self_(self) |
61 | 63 | { |
| 64 | + std::lock_guard< |
| 65 | + std::mutex> lock(self_.impl_->m); |
62 | 66 | // can't call twice |
63 | | - detail::throw_invalid_argument(); |
| 67 | + if(self_.impl_->st != state::none) |
| 68 | + detail::throw_invalid_argument(); |
| 69 | + self_.impl_->st = state::starting; |
64 | 70 | } |
65 | | - impl_->st = state::starting; |
66 | | - } |
67 | | - auto v = get_elements(); |
68 | | - for(std::size_t i = 0; i < v.size(); ++i) |
69 | | - { |
70 | | - try |
71 | | - { |
72 | | - v[i].start(); |
73 | | - } |
74 | | - catch(std::exception const&) |
| 71 | + |
| 72 | + ~action() |
75 | 73 | { |
| 74 | + if(n_ == 0) |
| 75 | + return; |
76 | 76 | { |
77 | | - std::lock_guard<std::mutex> lock(impl_->m); |
78 | | - impl_->st = state::stopping; |
| 77 | + std::lock_guard< |
| 78 | + std::mutex> lock(self_.impl_->m); |
| 79 | + BOOST_ASSERT( |
| 80 | + self_.impl_->st == state::stopping); |
| 81 | + self_.impl_->st = state::stopping; |
79 | 82 | } |
80 | | - do |
| 83 | + // stop what we started |
| 84 | + auto v = self_.get_elements(); |
| 85 | + while(n_-- > 0) |
| 86 | + v[n_].stop(); |
81 | 87 | { |
82 | | - v[i].stop(); |
| 88 | + std::lock_guard<std::mutex> lock( |
| 89 | + self_.impl_->m); |
| 90 | + self_.impl_->st = state::stopped; |
83 | 91 | } |
84 | | - while(i-- != 0); |
| 92 | + } |
| 93 | + |
| 94 | + void apply() |
| 95 | + { |
| 96 | + auto v = self_.get_elements(); |
| 97 | + while(n_ < v.size()) |
85 | 98 | { |
86 | | - std::lock_guard<std::mutex> lock(impl_->m); |
87 | | - impl_->st = state::stopped; |
| 99 | + v[n_].stop(); |
| 100 | + ++n_; |
88 | 101 | } |
89 | | - throw; |
| 102 | + n_ = 0; |
| 103 | + std::lock_guard< |
| 104 | + std::mutex> lock(self_.impl_->m); |
| 105 | + self_.impl_->st = state::running; |
90 | 106 | } |
91 | | - } |
92 | | - { |
93 | | - std::lock_guard<std::mutex> lock(impl_->m); |
94 | | - impl_->st = state::running; |
95 | | - } |
| 107 | + |
| 108 | + private: |
| 109 | + application& self_; |
| 110 | + std::size_t n_ = 0; |
| 111 | + }; |
| 112 | + |
| 113 | + action(*this).apply(); |
96 | 114 | } |
97 | 115 |
|
98 | 116 | void |
|
0 commit comments