EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
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
14namespace echomap
15{
16
19) :
21 worker_thread{[this](const std::stop_token& stop_token) {
22 execute(stop_token);
23 }}
24{
25}
26
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
36{
37 return !result_queue.empty();
38}
39
41{
42 return result_queue.try_consume();
43}
44
46{
47 task_queue.clear();
48 result_queue.clear();
49}
50
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
76 }
77}
78
79} // namespace echomap
EchoMap portable logger specification.
#define LOG_F_DEBUG(msg,...)
Conditionally logs a formatted debug-level message using echomap::Logger::log_f.
Definition Logger.hpp:94
#define LOG_F_ERROR(msg,...)
Logs a formatted error-level message using echomap::Logger::log_f.
Definition Logger.hpp:125
Audio signal class specification.
Worker specification.
Indicates that an ITask did not successfully complete.
Worker(ResultCallback result_callback={})
Create a new Worker with an optional callback.
Definition Worker.cpp:17
std::function< void()> ResultCallback
The type of callback to indicate new results.
Definition Worker.hpp:47
ResultCallback result_callback
Callable to inform clients of new results.
Definition Worker.hpp:95
std::optional< WorkerResult > try_get_result()
Attempt to retrieve the latest WorkerResult object from the computation thread.
Definition Worker.cpp:40
bool is_result_available() const
Checks the state of the result queue.
Definition Worker.cpp:35
std::jthread worker_thread
RAII computation thread to handle ITask work pieces.
Definition Worker.hpp:96
void clear()
Clears any scheduled jobs or pending results.
Definition Worker.cpp:45
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
void submit(std::unique_ptr< ITask > &&task)
Submit some work to the scheduler for execution on the computation thread.
Definition Worker.cpp:27
T current(T... args)
The main EchoMap outermost namespace for all non-exported symbols.
STL namespace.
T what(T... args)