Line data Source code
1 : #include "pool_server.hpp"
2 : #include "../../asio/detail/listener_access.hpp"
3 : #include "../../asio/io_context_of.hpp"
4 : #include "../../util/logger.hpp"
5 :
6 : namespace thinger::http {
7 :
8 1734 : pool_server::pool_server(asio::workers& workers)
9 1734 : : workers_(workers) {
10 1734 : LOG_DEBUG("Created HTTP pool server");
11 : // The end of a shutdown releases wait(), without calling stop() (see set_shutdown_stop())
12 4692 : set_shutdown_stop([this] { release_wait(); });
13 1734 : }
14 :
15 2466 : pool_server::~pool_server() {
16 1734 : LOG_DEBUG("Destroying HTTP pool server");
17 1734 : stop();
18 2466 : }
19 :
20 14279 : std::unique_ptr<asio::listener> pool_server::create_listener(asio::listener_options options) {
21 : auto listener = std::make_unique<asio::listener>(
22 14279 : std::move(options),
23 14279 : [this]() -> boost::asio::any_io_executor { return workers_.get_thread_io_context().get_executor(); },
24 28558 : workers_.provider());
25 : // Accepting does not wait behind the connections a worker serves
26 14279 : asio::detail::listener_access::use_acceptor_thread(*listener);
27 14279 : return listener;
28 0 : }
29 :
30 668237 : boost::asio::io_context* pool_server::shutdown_context() {
31 : // One read of the workers, which may stop meanwhile on another thread: none once stopped
32 668237 : return asio::io_context_of(workers_.next_executor());
33 : }
34 :
35 2754 : bool pool_server::in_server_thread() {
36 2754 : return workers_.in_worker_thread();
37 : }
38 :
39 3033 : void pool_server::stop() {
40 3033 : server_base::stop();
41 3033 : release_wait();
42 3033 : }
43 :
44 5991 : void pool_server::release_wait() {
45 : {
46 5991 : std::lock_guard<std::mutex> lock(wait_mutex_);
47 5991 : stopped_ = true;
48 5991 : }
49 5991 : wait_cv_.notify_all();
50 5991 : }
51 :
52 36 : void pool_server::wait() {
53 36 : std::unique_lock<std::mutex> lock(wait_mutex_);
54 108 : wait_cv_.wait(lock, [this] { return stopped_; });
55 36 : }
56 :
57 : } // namespace thinger::http
|