-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapplication.cpp
More file actions
138 lines (123 loc) · 2.85 KB
/
application.cpp
File metadata and controls
138 lines (123 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// Copyright (c) 2022 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http
//
#include <boost/http/application.hpp>
#include <boost/http/detail/except.hpp>
#include <boost/assert.hpp>
#include <mutex>
#include <vector>
namespace boost {
namespace http {
enum application::state : char
{
none,
starting,
running,
stopping,
stopped
};
struct application::impl
{
std::mutex m;
state st = state::none;
};
application::
~application()
{
{
std::lock_guard<std::mutex> lock(impl_->m);
if( impl_->st != state::stopped &&
impl_->st != state::none)
{
// stop() hasn't returned yet
detail::throw_invalid_argument();
}
}
delete impl_;
}
application::
application()
: impl_(new impl)
{
}
void
application::
start()
{
struct action
{
action(application& self)
: self_(self)
{
std::lock_guard<
std::mutex> lock(self_.impl_->m);
// can't call twice
if(self_.impl_->st != state::none)
detail::throw_invalid_argument();
self_.impl_->st = state::starting;
}
~action()
{
if(n_ == 0)
return;
{
std::lock_guard<
std::mutex> lock(self_.impl_->m);
BOOST_ASSERT(
self_.impl_->st == state::stopping);
self_.impl_->st = state::stopping;
}
// stop what we started
auto v = self_.get_elements();
while(n_-- > 0)
v[n_].stop();
{
std::lock_guard<std::mutex> lock(
self_.impl_->m);
self_.impl_->st = state::stopped;
}
}
void apply()
{
auto v = self_.get_elements();
while(n_ < v.size())
{
v[n_].start();
++n_;
}
n_ = 0;
std::lock_guard<
std::mutex> lock(self_.impl_->m);
self_.impl_->st = state::running;
}
private:
application& self_;
std::size_t n_ = 0;
};
action(*this).apply();
}
void
application::
stop()
{
{
std::lock_guard<std::mutex> lock(impl_->m);
if(impl_->st != state::running)
detail::throw_invalid_argument();
impl_->st = state::stopping;
}
auto v = get_elements();
for(std::size_t i = v.size(); i--;)
v[i].stop();
{
std::lock_guard<std::mutex> lock(impl_->m);
impl_->st = state::stopped;
}
}
} // http
} // boost