21#include <boost/asio.hpp>
22#include <boost/move/make_unique.hpp>
23#include <boost/asio/executor_work_guard.hpp>
39 boost::promise<void>* done_promise_else_block,
40 Task&& init_func_or_empty) :
42 m_task_engine(task_engine_ptr),
43 m_own_task_engine(own_task_engine)
46 using boost::asio::post;
47 using boost::asio::make_work_guard;
48 using boost::movelib::unique_ptr;
49 using boost::movelib::make_unique;
57 using Task_engine_work = boost::asio::executor_work_guard<Task_engine::executor_type>;
61 string nickname(nickname_view);
65 FLOW_LOG_INFO(
"Task_qing_thread [" <<
static_cast<const void*
>(
this) <<
"] with nickname [" << nickname <<
"] "
66 "will " << (
m_own_task_engine ?
"have own Task_engine, probably with concurrency-hint=1"
85 unique_ptr<promise<void>> our_done_promise_or_null;
86 promise<void>& done_promise
87 = done_promise_else_block ? *done_promise_else_block
88 : *(our_done_promise_or_null = make_unique<promise<void>>());
94 const auto sev_override = *(Log_config::this_thread_verbosity_override());
98 nickname = std::move(nickname),
99 init_func_or_empty = std::move(init_func_or_empty),
106 const auto sev_override_auto = Log_config::this_thread_verbosity_override_auto(sev_override);
110 Logger::this_thread_set_logged_nickname(nickname, logger_ptr);
117 if (!init_func_or_empty.empty())
119 FLOW_LOG_TRACE(
"Thread initializer callback executing synchronously.");
120 init_func_or_empty();
122 FLOW_LOG_INFO(
"Thread initializer callback execution finished. Continuing event loop setup in this thread.");
131 FLOW_LOG_TRACE(
"First task from Task_qing_thread [" <<
this <<
"] executing "
132 "in that thread since not sharing Task_engine object with more threads.");
136 FLOW_LOG_TRACE(
"First task from Task_qing_thread [" <<
this <<
"] executing. "
137 "May or may not be in that thread since sharing Task_engine object with more threads.");
141 done_promise.set_value();
151 Task_engine_work avoid_task_engine_stop(make_work_guard(*
m_task_engine));
158 const auto sev_override_auto = Log_config::this_thread_verbosity_override_auto(sev_override);
164 FLOW_LOG_INFO(
"Event loop finished: Task_qing_thread [" <<
this <<
"] presumably being stopped! "
165 "Thread exit imminent.");
236 if (done_promise_else_block)
238 FLOW_LOG_TRACE(
"Thread started -- confirmation that it is up shall be signalled to caller through a `promise`.");
242 FLOW_LOG_TRACE(
"Thread started -- awaiting confirmation it is up.");
243 done_promise.get_future().wait();
244 FLOW_LOG_TRACE(
"Confirmed thread started -- Task_qing_thread [" <<
this <<
"] ready.");
250 FLOW_LOG_TRACE(
"Task_qing_thread [" <<
this <<
"]: Destroying object.");
255 FLOW_LOG_INFO(
"Task_qing_thread [" <<
this <<
"]: Thread was stopped/joined just now or earlier; "
256 "destructor return imminent; Task_engine destruction *may* therefore also be imminent.");
273 FLOW_LOG_TRACE(
"Task_qing_thread [" <<
this <<
"]: Not waiting for worker thread "
274 "[T" <<
m_worker_thread->get_id() <<
"] to finish, as it was already stop()ped earlier.");
278 FLOW_LOG_INFO(
"Task_qing_thread [" <<
this <<
"]: Waiting for worker thread "
279 "[T" <<
m_worker_thread->get_id() <<
"] to finish, as it was not stop()ped earlier.");
301 FLOW_LOG_TRACE(
"Task_qing_thread [" <<
this <<
"]: Thread stopped/joined; stop() return imminent.");
bool m_own_task_engine
See constructor.
Task_engine_ptr m_task_engine
See task_engine().
util::Thread * raw_worker_thread()
Returns the util::Thread – a thin wrapper around the native OS thread handle – corresponding to the w...
void stop()
Blocks the calling thread until the constructor-started thread has finished; if the underlying Task_e...
boost::movelib::unique_ptr< util::Thread > m_worker_thread
Thread created in constructor. Not-a-thread after stop(); not not-a-thread before stop().
static const int S_BAD_EXIT
exit() code returned to OS in the event Task_qing_thread chooses to exit() the entire program (as of ...
Task_engine_ptr task_engine()
Returns pointer to util::Task_engine such that post()ing to it will cause the subsequent asynchronous...
Task_qing_thread(flow::log::Logger *logger_ptr, util::String_view nickname, const Task_engine_ptr &task_engine, bool own_task_engine, boost::promise< void > *done_promise_else_block=0, Task &&init_func_or_empty=Task())
Constructs object, immediately spawning new (worker) thread, memorizing a ref-counted reference to th...
~Task_qing_thread()
stop(), followed by forgetting the Task_engine returned by task_engine(); the latter action may destr...
Class used to configure the filtering and logging behavior of Loggers; its use in your custom Loggers...
Logger * get_logger() const
Returns the stored Logger pointer, particularly as many FLOW_LOG_*() macros expect.
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
#define FLOW_LOG_TRACE(ARG_stream_fragment)
Logs a TRACE message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Flow module containing tools enabling multi-threaded event loops operating under the asynchronous-tas...
@ S_ASYNC
Simply post the given task to execute asynchronously in some execution context – as soon as the conte...
boost::shared_ptr< util::Task_engine > Task_engine_ptr
Short-hand for reference-counting pointer to a mutable util::Task_engine (a/k/a boost::asio::io_conte...
void beautify_chrono_logger_this_thread(Logger *logger_ptr)
Sets certain chrono-related formatting on the given Logger in the current thread that results in a co...
boost::asio::io_context Task_engine
Short-hand for boost.asio event service, the central class of boost.asio.
Basic_string_view< char > String_view
Commonly used char-based Basic_string_view. See its doc header.
boost::thread Thread
Short-hand for standard thread class.
Catch-all namespace for the Flow project: A collection of various production-quality modules written ...
Flow_log_component
The flow::log::Component payload enumeration comprising various log components used by Flow's own int...