8 : m_clock(default_sample_rate)
9 , m_cleanup_threshold(512)
20 std::cerr <<
"Failed to initiate routine\n;\t >> Exiting" << std::endl;
29 if (existing_it !=
m_tasks.end()) {
30 if (existing_it->routine && existing_it->routine->is_active()) {
31 existing_it->routine->set_should_terminate(
true);
36 m_tasks.emplace_back(routine, task_name);
51 if (it->routine && it->routine->is_active()) {
52 it->routine->set_should_terminate(
true);
64 if (routine && routine->is_active()) {
65 routine->set_should_terminate(
true);
77 if (it->routine && it->routine->is_active()) {
78 it->routine->restart();
87 return (it !=
m_tasks.end()) ? it->routine :
nullptr;
92 std::vector<std::shared_ptr<Routine>> result;
93 for (
const auto& entry :
m_tasks) {
94 if (entry.routine && entry.routine->get_processing_token() ==
token) {
95 result.push_back(entry.routine);
106 processor_it->second(tasks, processing_units);
111 static uint64_t cleanup_counter = 0;
123 static uint64_t cleanup_counter = 0;
139 return *clock_it->second;
144 return *audio_clock_it->second;
147 throw std::runtime_error(
"No clocks available in scheduler");
164 return clock.current_position();
171 return clock_it->second->rate();
181 return entry.routine && entry.routine->is_active() && entry.routine->get_processing_token() == token;
198 [&name](
const TaskEntry& entry) { return entry.name == name; });
204 [&name](
const TaskEntry& entry) { return entry.name == name; });
210 [&routine](
const TaskEntry& entry) { return entry.routine == routine; });
263 auto& clock = *clock_it->second;
264 clock.tick(processing_units);
268 auto& clock = *clock_it->second;
270 for (uint64_t i = 0; i < processing_units; i++) {
271 uint64_t current_context = clock.current_position();
273 for (
auto& routine : tasks) {
274 if (routine && routine->is_active()) {
275 if (routine->requires_clock_sync()) {
276 if (current_context >= routine->next_execution()) {
294 return !entry.routine || !entry.routine->is_active();
310 uint64_t current_context = clock_it->second->current_position();
311 return routine->initialize_state(current_context);
317 if (entry.routine && entry.routine->is_active()) {
318 bool current_auto_resume = entry.routine->get_auto_resume();
319 entry.routine->set_state<
bool>(
"was_auto_resume", current_auto_resume);
320 entry.routine->set_auto_resume(
false);
328 if (entry.routine && entry.routine->is_active()) {
329 bool* was_auto_resume = entry.routine->get_state<
bool>(
"was_auto_resume");
330 if (was_auto_resume) {
331 entry.routine->set_auto_resume(*was_auto_resume);
333 entry.routine->set_auto_resume(
true);
342 if (entry.routine && entry.routine->is_active()) {
343 entry.routine->set_should_terminate(
true);
344 entry.routine->set_auto_resume(
true);
348 std::this_thread::sleep_for(std::chrono::milliseconds(10));
358 for (
auto& task : tasks) {
359 if (task && task->is_active()) {
360 if (task->requires_clock_sync()) {
static MayaFlux::Nodes::ProcessingToken token
Abstract base interface for all clock types in the multimodal scheduling system.
std::atomic< uint64_t > m_next_task_id
Task ID counter for unique identification.
bool cancel_task(std::shared_ptr< Routine > task)
Cancels and removes a task from the scheduler.
void register_token_processor(ProcessingToken token, token_processing_func_t processor)
Register a custom processor for a specific token domain.
uint64_t seconds_to_units(double seconds, ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Convert seconds to processing units for a specific domain.
std::vector< TaskEntry >::iterator find_task_by_routine(std::shared_ptr< Routine > routine)
Find task entry by routine pointer.
unsigned int get_default_rate(ProcessingToken token) const
Get the default rate for a processing token.
std::shared_ptr< Routine > get_task(const std::string &name) const
Get a named task.
void cleanup_completed_tasks()
Clean up completed tasks in a domain.
void add_task(std::shared_ptr< Routine > routine, const std::string &name="", bool initialize=false)
Add a routine to the scheduler based on its processing token.
void process_buffer_cycle_tasks()
TaskScheduler(uint32_t default_sample_rate=48000, uint32_t default_frame_rate=60)
Constructs a TaskScheduler with the specified sample rate.
std::vector< TaskEntry > m_tasks
bool initialize_routine_state(std::shared_ptr< Routine > routine, ProcessingToken token)
Initialize a routine's state for a specific domain.
void process_token(ProcessingToken token, uint64_t processing_units=1)
Process all tasks for a specific processing domain.
std::vector< TaskEntry >::iterator find_task_by_name(const std::string &name)
Find task entry by name.
uint64_t m_current_buffer_cycle
void ensure_domain(ProcessingToken token, unsigned int rate=0)
Initialize a processing domain if it doesn't exist.
void pause_all_tasks()
Pause all active tasks.
uint64_t get_next_task_id() const
Generates a unique task ID for new tasks.
uint32_t m_cleanup_threshold
Threshold for task cleanup.
uint64_t seconds_to_samples(double seconds) const
Converts a time in seconds to a number of samples.
std::vector< std::shared_ptr< Routine > > get_tasks_for_token(ProcessingToken token) const
Get all tasks for a specific processing domain.
std::unordered_map< ProcessingToken, token_processing_func_t > m_token_processors
Custom processors for specific domains.
uint64_t current_units(ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Get current processing units for a domain.
void process_default(ProcessingToken token, uint64_t processing_units)
Process tasks in a specific domain with default algorithm.
void process_all_tokens()
Process all active domains.
std::string auto_generate_name(std::shared_ptr< Routine > routine) const
Generate automatic name for a routine based on its type.
unsigned int get_rate(ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Get processing rate for a domain.
bool restart_task(const std::string &name)
Restart a named task.
void terminate_all_tasks()
Terminate and clear all tasks.
void resume_all_tasks()
Resume all previously paused tasks.
std::unordered_map< ProcessingToken, std::unique_ptr< IClock > > m_token_clocks
Clock instances for each processing domain.
bool has_active_tasks(ProcessingToken token) const
Check if a processing domain has any active tasks.
const SampleClock & get_clock() const
Gets the primary clock (audio domain for legacy compatibility)
uint64_t seconds_to_units(double seconds, uint32_t rate)
Convert seconds to processing units for any rate.
uint64_t seconds_to_samples(double seconds, uint32_t sample_rate)
Convert seconds to samples at a given sample rate.
std::function< void(const std::vector< std::shared_ptr< Routine > > &, uint64_t)> token_processing_func_t
Function type for processing tasks in a specific token domain.
@ MULTI_RATE
Coroutine can handle multiple sample rates. Picks the frame-accurate processing token by default.
@ FRAME_ACCURATE
Coroutine is frame-accurate.
@ SAMPLE_ACCURATE
Coroutine is sample-accurate.
@ ON_DEMAND
Coroutine is executed on demand, not scheduled.
@ SAMPLE_BASED
Sample-accurate delay (audio domain)
@ BUFFER_BASED
Buffer-cycle delay (audio hardware boundary)