-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathworker_impl.cc
More file actions
39 lines (31 loc) · 1.16 KB
/
worker_impl.cc
File metadata and controls
39 lines (31 loc) · 1.16 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
#include "source/common/worker_impl.h"
#include "envoy/runtime/runtime.h"
#include "envoy/thread_local/thread_local.h"
namespace Nighthawk {
WorkerImpl::WorkerImpl(Envoy::Api::Api& api, Envoy::ThreadLocal::Instance& tls,
Envoy::Stats::Store& store)
: thread_factory_(api.threadFactory()), dispatcher_(api.allocateDispatcher("worker_thread")),
tls_(tls), store_(store), time_source_(api.timeSource()) {
tls.registerThread(*dispatcher_, false);
}
WorkerImpl::~WorkerImpl() { RELEASE_ASSERT(shutdown_, "Call shutdown() before destruction."); }
void WorkerImpl::shutdown() {
shutdown_ = true;
signal_thread_to_exit_.set_value();
thread_.join();
}
void WorkerImpl::start() {
RELEASE_ASSERT(!started_, "WorkerImpl::start() expected started_ to be false");
started_ = true;
shutdown_ = false;
thread_ = std::thread([this]() {
dispatcher_->run(Envoy::Event::Dispatcher::RunType::NonBlock);
work();
complete_.set_value();
signal_thread_to_exit_.get_future().wait();
shutdownThread();
tls_.shutdownThread();
});
}
void WorkerImpl::waitForCompletion() { complete_.get_future().wait(); }
} // namespace Nighthawk