|
| 1 | +// |
| 2 | +// Copyright (c) 2022 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/cppalliance/beast2 |
| 8 | +// |
| 9 | + |
| 10 | +#include <boost/rts/application.hpp> |
| 11 | +#include <boost/rts/detail/except.hpp> |
| 12 | +#include <mutex> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +namespace boost { |
| 16 | +namespace rts { |
| 17 | + |
| 18 | +enum application::state : char |
| 19 | +{ |
| 20 | + none, |
| 21 | + starting, |
| 22 | + running, |
| 23 | + stopping, |
| 24 | + stopped |
| 25 | +}; |
| 26 | + |
| 27 | +struct application::impl |
| 28 | +{ |
| 29 | + std::mutex m; |
| 30 | + state st = state::none; |
| 31 | +}; |
| 32 | + |
| 33 | +application:: |
| 34 | +~application() |
| 35 | +{ |
| 36 | + { |
| 37 | + std::lock_guard<std::mutex> lock(impl_->m); |
| 38 | + if( impl_->st != state::stopped && |
| 39 | + impl_->st != state::none) |
| 40 | + { |
| 41 | + // stop() hasn't returned yet |
| 42 | + detail::throw_invalid_argument(); |
| 43 | + } |
| 44 | + } |
| 45 | + delete impl_; |
| 46 | +} |
| 47 | + |
| 48 | +application:: |
| 49 | +application() |
| 50 | + : impl_(new impl) |
| 51 | +{ |
| 52 | +} |
| 53 | + |
| 54 | +void |
| 55 | +application:: |
| 56 | +start() |
| 57 | +{ |
| 58 | + { |
| 59 | + std::lock_guard<std::mutex> lock(impl_->m); |
| 60 | + if(impl_->st != state::none) |
| 61 | + { |
| 62 | + // can't call twice |
| 63 | + detail::throw_invalid_argument(); |
| 64 | + } |
| 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&) |
| 75 | + { |
| 76 | + { |
| 77 | + std::lock_guard<std::mutex> lock(impl_->m); |
| 78 | + impl_->st = state::stopping; |
| 79 | + } |
| 80 | + do |
| 81 | + { |
| 82 | + v[i].stop(); |
| 83 | + } |
| 84 | + while(i-- != 0); |
| 85 | + { |
| 86 | + std::lock_guard<std::mutex> lock(impl_->m); |
| 87 | + impl_->st = state::stopped; |
| 88 | + } |
| 89 | + throw; |
| 90 | + } |
| 91 | + } |
| 92 | + { |
| 93 | + std::lock_guard<std::mutex> lock(impl_->m); |
| 94 | + impl_->st = state::running; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void |
| 99 | +application:: |
| 100 | +stop() |
| 101 | +{ |
| 102 | + { |
| 103 | + std::lock_guard<std::mutex> lock(impl_->m); |
| 104 | + if(impl_->st != state::running) |
| 105 | + detail::throw_invalid_argument(); |
| 106 | + impl_->st = state::stopping; |
| 107 | + } |
| 108 | + |
| 109 | + auto v = get_elements(); |
| 110 | + for(std::size_t i = v.size(); i--;) |
| 111 | + v[i].stop(); |
| 112 | + |
| 113 | + { |
| 114 | + std::lock_guard<std::mutex> lock(impl_->m); |
| 115 | + impl_->st = state::stopped; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +} // rts |
| 120 | +} // boost |
0 commit comments