21#include <boost/asio.hpp>
22#include <boost/move/make_unique.hpp>
38 boost::promise<void>* done_promise_else_block,
39 Task&& init_func_or_empty) :
41 m_task_engine(task_engine_ptr),
42 m_own_task_engine(own_task_engine)
45 using boost::asio::post;
46 using boost::movelib::unique_ptr;
47 using boost::movelib::make_unique;
55 using Task_engine_work = Task_engine::work;
59 string nickname(nickname_view);
63 FLOW_LOG_INFO(
"Task_qing_thread [" <<
static_cast<const void*
>(
this) <<
"] with nickname [" << nickname <<
"] "
64 "will " << (
m_own_task_engine ?
"have own Task_engine, probably with concurrency-hint=1"
83 unique_ptr<promise<void>> our_done_promise_or_null;
84 promise<void>& done_promise
85 = done_promise_else_block ? *done_promise_else_block
86 : *(our_done_promise_or_null = make_unique<promise<void>>());
92 const auto sev_override = *(Log_config::this_thread_verbosity_override());
96 nickname = std::move(nickname),
97 init_func_or_empty = std::move(init_func_or_empty),
104 const auto sev_override_auto = Log_config::this_thread_verbosity_override_auto(sev_override);
108 Logger::this_thread_set_logged_nickname(nickname, logger_ptr);
115 if (!init_func_or_empty.empty())
117 FLOW_LOG_TRACE(
"Thread initializer callback executing synchronously.");
118 init_func_or_empty();
120 FLOW_LOG_INFO(
"Thread initializer callback execution finished. Continuing event loop setup in this thread.");
129 FLOW_LOG_TRACE(
"First task from Task_qing_thread [" <<
this <<
"] executing "
130 "in that thread since not sharing Task_engine object with more threads.");
134 FLOW_LOG_TRACE(
"First task from Task_qing_thread [" <<
this <<
"] executing. "
135 "May or may not be in that thread since sharing Task_engine object with more threads.");
139 done_promise.set_value();
156 const auto sev_override_auto = Log_config::this_thread_verbosity_override_auto(sev_override);
162 FLOW_LOG_INFO(
"Event loop finished: Task_qing_thread [" <<
this <<
"] presumably being stopped! "
163 "Thread exit imminent.");
234 if (done_promise_else_block)
236 FLOW_LOG_TRACE(
"Thread started -- confirmation that it is up shall be signalled to caller through a `promise`.");
240 FLOW_LOG_TRACE(
"Thread started -- awaiting confirmation it is up.");
241 done_promise.get_future().wait();
242 FLOW_LOG_TRACE(
"Confirmed thread started -- Task_qing_thread [" <<
this <<
"] ready.");
248 FLOW_LOG_TRACE(
"Task_qing_thread [" <<
this <<
"]: Destroying object.");
253 FLOW_LOG_INFO(
"Task_qing_thread [" <<
this <<
"]: Thread was stopped/joined just now or earlier; "
254 "destructor return imminent; Task_engine destruction *may* therefore also be imminent.");
271 FLOW_LOG_TRACE(
"Task_qing_thread [" <<
this <<
"]: Not waiting for worker thread "
272 "[T" <<
m_worker_thread->get_id() <<
"] to finish, as it was already stop()ped earlier.");
276 FLOW_LOG_INFO(
"Task_qing_thread [" <<
this <<
"]: Waiting for worker thread "
277 "[T" <<
m_worker_thread->get_id() <<
"] to finish, as it was not stop()ped earlier.");
299 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_servi...
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_service 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...