Flow 1.0.2
Flow project: Full implementation reference.
single_thread_task_loop.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
20
21namespace flow::async
22{
23
24// Single_thread_task_loop implementations.
25
27 log::Log_context(logger_ptr, Flow_log_component::S_ASYNC),
28 m_underlying_loop(get_logger(), nickname, 1) // Attn: 1 thread. Other (optional) args become irrelevant.
29{
30 FLOW_LOG_INFO("Single_thread_task_loop [" << static_cast<const void*>(this) << "] "
31 "with nickname [" << nickname << "], backed by a 1-thread Cross_thread_task_loop: "
32 "Started. Above Cross_thread_task_loop messages, if any, contain details.");
33}
34
36{
37 FLOW_LOG_INFO("Single_thread_task_loop [" << this << "]: Destroying object: see subsequent Cross_thread_task_loop "
38 "messages if any.");
39}
40
41void Single_thread_task_loop::start(Task&& init_task_or_empty)
42{
43 // There's an optional 2nd arg we omit.
44 m_underlying_loop.start(std::move(init_task_or_empty), [this](size_t)
45 {
46 // This occurs ~first thing in the thread (before init_task_or_empty() if any).
47 m_started_thread_id_or_none = util::this_thread::get_id();
48 });
49}
50
52{
53 return util::this_thread::get_id() == m_started_thread_id_or_none; // Corner case: false before start().
54}
55
57{
59}
60
62{
63 m_underlying_loop.post(std::move(task), synchronicity);
64}
65
67 Scheduled_task&& task)
68{
69 return m_underlying_loop.schedule_from_now(from_now, std::move(task));
70}
71
73 Scheduled_task&& task)
74{
75 return m_underlying_loop.schedule_at(at, std::move(task));
76}
77
79{
81}
82
84{
85 return static_cast<Concurrent_task_loop*>(&m_underlying_loop);
86}
87
88// Timed_single_thread_task_loop implementations.
89
91 perf::Clock_type clock_type) :
92 Single_thread_task_loop(logger_ptr, nickname),
93 m_timed_loop(underlying_loop(), clock_type,
94 // Not thread-safe: but we have only one thread.
95 [](perf::duration_rep_t* time_acc) -> perf::duration_rep_t
96 { const auto ret = *time_acc; *time_acc = 0; return ret; })
97{
98 // That's it.
99}
100
102{
103 m_timed_loop.post(std::move(task), synchronicity);
104}
105
107 (const Fine_duration& from_now, Scheduled_task&& task)
108{
109 return m_timed_loop.schedule_from_now(from_now, std::move(task));
110}
111
113{
114 return m_timed_loop.schedule_at(at, std::move(task));
115}
116
118{
120}
121
122} // namespace flow::async
The core flow::async interface, providing an optionally multi-threaded thread pool onto which runnabl...
util::Scheduled_task_handle schedule_from_now(const Fine_duration &from_now, Scheduled_task &&task) override
Implements superclass API.
void start(Task &&init_task_or_empty=Task(), const Thread_init_func &thread_init_func_or_empty=Thread_init_func()) override
Implements superclass API.
void stop() override
Implements superclass API.
util::Scheduled_task_handle schedule_at(const Fine_time_pt &at, Scheduled_task &&task) override
Implements superclass API.
void post(Task &&task, Synchronicity synchronicity=Synchronicity::S_ASYNC) override
Implements superclass API.
Task_engine_ptr task_engine() override
See superclass API.
A Concurrent_task_loop-related adapter-style class that represents a single-thread task loop; essenti...
void start(Task &&init_task_or_empty=Task())
Starts the 1 thread in the thread pool; any queued post()ed (and similar) tasks may begin executing i...
virtual util::Scheduled_task_handle schedule_at(const Fine_time_pt &at, Scheduled_task &&task)
Equivalent to schedule_from_now() except one specifies an absolute time point instead of wait duratio...
Task_loop_impl m_underlying_loop
See underlying_loop().
virtual util::Scheduled_task_handle schedule_from_now(const Fine_duration &from_now, Scheduled_task &&task)
Equivalent to post() but execution is scheduled for later, after the given time period passes.
Task_engine_ptr task_engine()
Returns a pointer to the internal util::Task_engine (a/k/a boost.asio io_service) for the purpose of ...
util::Thread_id m_started_thread_id_or_none
Before start() it is default-cted (not-a-thread); from start() on it's the started thread's ID.
Concurrent_task_loop * underlying_loop()
Returns the underlying work-horse Concurrent_task_loop.
~Single_thread_task_loop() override
Executes stop() – see its doc header please – and then cleans up any resources.
void stop()
Waits for the ongoing task/completion handler – if one is running – to return; then prevents any furt...
bool in_thread() const
Returns true if and only if the thread executing this call is the thread started by start().
virtual void post(Task &&task, Synchronicity synchronicity=Synchronicity::S_ASYNC)
Cause the given Task (function) to execute within the worker thread as soon as the thread is free of ...
Single_thread_task_loop(log::Logger *logger_ptr, util::String_view nickname)
Constructs object, making it available for post() and similar work, but without starting any thread a...
util::Scheduled_task_handle schedule_at(const Fine_time_pt &at, Scheduled_task &&task) override
Implements superclass API.
perf::Duration accumulated_time()
Returns the accumulated time spent on tasks this thread pool handles since its last invocation; and r...
util::Scheduled_task_handle schedule_from_now(const Fine_duration &from_now, Scheduled_task &&task) override
Implements superclass API.
void post(Task &&task, Synchronicity synchronicity=Synchronicity::S_ASYNC) override
Implements superclass API.
Timed_single_thread_task_loop(log::Logger *logger_ptr, util::String_view nickname, perf::Clock_type clock_type=perf::Clock_type::S_CPU_THREAD_TOTAL_HI_RES)
Constructs object, similarly to Single_thread_task_loop ctor; but with timing capabilities a-la Timed...
Timed_concurrent_task_loop_impl< perf::duration_rep_t > m_timed_loop
The task-timing decorator of our superclass Single_thread_task_loop's protected superclass.
util::Scheduled_task_handle schedule_at(const Fine_time_pt &at, Scheduled_task &&task) override
Implements superclass method but with the addition of timing of task, accessible through accumulated_...
void post(Task &&task, Synchronicity synchronicity=Synchronicity::S_ASYNC) override
Implements superclass method but with the addition of timing of task, accessible through accumulated_...
perf::Duration accumulated_time()
See Timed_concurrent_task_loop_impl::accumulated_time().
util::Scheduled_task_handle schedule_from_now(const Fine_duration &from_now, Scheduled_task &&task) override
Implements superclass method but with the addition of timing of task, accessible through accumulated_...
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1291
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Definition: log.hpp:197
Flow module containing tools enabling multi-threaded event loops operating under the asynchronous-tas...
Definition: async_fwd.hpp:75
Synchronicity
Enumeration indicating the manner in which asio_exec_ctx_post(), and various boost....
Definition: async_fwd.hpp:223
@ 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...
Definition: async_fwd.hpp:198
Fine_duration Duration
Short-hand for a high-precision boost.chrono duration, formally equivalent to flow::Fine_duration.
Duration::rep duration_rep_t
The raw type used in Duration to store its clock ticks.
Clock_type
Clock types supported by flow::perf module facilities, perf::Checkpointing_timer in particular.
boost::shared_ptr< Scheduled_task_handle_state > Scheduled_task_handle
Black-box type that represents a handle to a scheduled task as scheduled by schedule_task_at() or sch...
Basic_string_view< char > String_view
Commonly used char-based Basic_string_view. See its doc header.
Flow_log_component
The flow::log::Component payload enumeration comprising various log components used by Flow's own int...
Definition: common.hpp:633
Fine_clock::duration Fine_duration
A high-res time duration as computed from two Fine_time_pts.
Definition: common.hpp:411
Fine_clock::time_point Fine_time_pt
A high-res time point as returned by Fine_clock::now() and suitable for precise time math in general.
Definition: common.hpp:408