EchoMap
2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Toggle main menu visibility
Loading...
Searching...
No Matches
Worker.cpp
Go to the documentation of this file.
1
7
8
#include "
Worker.hpp
"
9
10
#include "../objects/Project.hpp"
11
#include "
../objects/Signal.hpp
"
12
#include "
../utility/Logger.hpp
"
13
14
namespace
echomap
15
{
16
17
Worker::Worker
(
18
ResultCallback
result_callback
19
) :
20
result_callback
(
std
::move(
result_callback
)),
21
worker_thread
{[this](const
std
::stop_token& stop_token) {
22
execute
(stop_token);
23
}}
24
{
25
}
26
27
void
Worker::submit
(
28
std::unique_ptr<ITask>
&& task
29
)
30
{
31
LOG_F_DEBUG
(
"Scheduling {} {}: {}."
, task->get_class_name(), task->get_id(), task->get_name());
32
task_queue.produce(std::move(task));
33
}
34
35
bool
Worker::is_result_available
()
const
36
{
37
return
!result_queue.empty();
38
}
39
40
std::optional<WorkerResult>
Worker::try_get_result
()
41
{
42
return
result_queue.try_consume();
43
}
44
45
void
Worker::clear
()
46
{
47
task_queue.clear();
48
result_queue.clear();
49
}
50
51
void
Worker::execute
(
52
const
std::stop_token
& stop_token
53
)
noexcept
54
{
55
while
(!stop_token.stop_requested())
56
// ThreadSafeQueue::wait_consume will block the computation thread until some work is available.
57
if
(
auto
job = task_queue.wait_consume(stop_token); job.has_value()) {
58
// Likewise, ITask::execute runs the work synchronously on our computation thread.
59
auto
& task = *job;
60
LOG_F_DEBUG
(
"Executing {}."
, task->get_name());
61
62
try
{
63
auto
result = task->execute(stop_token);
64
LOG_F_DEBUG
(
"Finished {}."
, task->get_name());
65
result_queue.produce(std::move(result));
66
}
catch
(
const
std::exception
& exception) {
67
LOG_F_ERROR
(
"{} failed with message: {}"
, task->get_name(), exception.
what
());
68
result_queue.produce(
ErrorResult
(exception.
what
(),
std::source_location::current
(), std::move(task)));
69
}
catch
(...) {
70
LOG_F_ERROR
(
"{} failed with a system error. This is bug."
, task->get_name());
71
result_queue.produce(
ErrorResult
(
"System error"
,
std::source_location::current
(), std::move(task)));
72
}
73
74
if
(
result_callback
)
75
result_callback
();
76
}
77
}
78
79
}
// namespace echomap
Logger.hpp
EchoMap portable logger specification.
LOG_F_DEBUG
#define LOG_F_DEBUG(msg,...)
Conditionally logs a formatted debug-level message using echomap::Logger::log_f.
Definition
Logger.hpp:94
LOG_F_ERROR
#define LOG_F_ERROR(msg,...)
Logs a formatted error-level message using echomap::Logger::log_f.
Definition
Logger.hpp:125
Signal.hpp
Audio signal class specification.
Worker.hpp
Worker specification.
echomap::ErrorResult
Indicates that an ITask did not successfully complete.
Definition
ErrorResult.hpp:28
echomap::Worker::Worker
Worker(ResultCallback result_callback={})
Create a new Worker with an optional callback.
Definition
Worker.cpp:17
echomap::Worker::ResultCallback
std::function< void()> ResultCallback
The type of callback to indicate new results.
Definition
Worker.hpp:47
echomap::Worker::result_callback
ResultCallback result_callback
Callable to inform clients of new results.
Definition
Worker.hpp:95
echomap::Worker::try_get_result
std::optional< WorkerResult > try_get_result()
Attempt to retrieve the latest WorkerResult object from the computation thread.
Definition
Worker.cpp:40
echomap::Worker::is_result_available
bool is_result_available() const
Checks the state of the result queue.
Definition
Worker.cpp:35
echomap::Worker::worker_thread
std::jthread worker_thread
RAII computation thread to handle ITask work pieces.
Definition
Worker.hpp:96
echomap::Worker::clear
void clear()
Clears any scheduled jobs or pending results.
Definition
Worker.cpp:45
echomap::Worker::execute
void execute(const std::stop_token &stop_token) noexcept
Executor running on the computation thread to receive work from the task queue and synchronously exec...
Definition
Worker.cpp:51
echomap::Worker::submit
void submit(std::unique_ptr< ITask > &&task)
Submit some work to the scheduler for execution on the computation thread.
Definition
Worker.cpp:27
std::source_location::current
T current(T... args)
std::exception
echomap
The main EchoMap outermost namespace for all non-exported symbols.
Definition
ActionController.hpp:20
std
STL namespace.
std::optional
std::stop_token
std::unique_ptr
std::exception::what
T what(T... args)
src
async
Worker.cpp
Generated by
1.17.0