18 #include <hicn/transport/config.h>
19 #include <hicn/transport/core/asio_wrapper.h>
20 #include <hicn/transport/errors/runtime_exception.h>
29 EventThread(asio::io_service& io_service,
bool detached =
false)
30 : internal_io_service_(
nullptr),
31 io_service_(std::ref(io_service)),
32 work_(std::make_unique<asio::io_service::work>(io_service_)),
39 : internal_io_service_(std::make_unique<asio::io_service>()),
40 io_service_(std::ref(*internal_io_service_)),
41 work_(std::make_unique<asio::io_service::work>(io_service_)),
51 : internal_io_service_(std::move(other.internal_io_service_)),
52 io_service_(std::move(other.io_service_)),
53 work_(std::move(other.work_)),
54 thread_(std::move(other.thread_)),
55 detached_(std::move(other.detached_)) {}
58 internal_io_service_ = std::move(other.internal_io_service_);
59 io_service_ = std::move(other.io_service_);
60 work_ = std::move(other.work_);
61 thread_ = std::move(other.thread_);
62 detached_ = other.detached_;
71 io_service_.get().stopped();
75 std::make_unique<std::thread>([
this]() { io_service_.get().run(); });
82 std::thread::id getThreadId()
const {
84 return thread_->get_id();
90 template <
typename Func>
92 io_service_.get().post(std::forward<Func&&>(f));
95 template <
typename Func>
96 void tryRunHandlerNow(Func&& f) {
97 io_service_.get().dispatch(std::forward<Func&&>(f));
103 if (thread_ && thread_->joinable()) {
110 bool stopped() {
return io_service_.get().stopped(); }
112 asio::io_service& getIoService() {
return io_service_; }
115 std::unique_ptr<asio::io_service> internal_io_service_;
116 std::reference_wrapper<asio::io_service> io_service_;
117 std::unique_ptr<asio::io_service::work> work_;
118 std::unique_ptr<std::thread> thread_;