12 TaskEntry(
const std::shared_ptr<Routine>& r, std::string n)
61 TaskScheduler(uint32_t default_sample_rate = 48000, uint32_t default_frame_rate = 60);
73 void add_task(
const std::shared_ptr<Routine>& routine,
const std::string& name =
"",
bool initialize =
false);
80 std::shared_ptr<Routine> get_task(
const std::string& name)
const;
91 bool cancel_task(
const std::shared_ptr<Routine>& routine);
112 std::vector<std::shared_ptr<Routine>> get_tasks_for_token(
ProcessingToken token)
const;
123 void process_token(
ProcessingToken token, uint64_t processing_units = 1);
132 void process_all_tokens();
156 void register_clock(
ProcessingToken token, std::shared_ptr<IClock> clock);
171 uint64_t current_units(
ProcessingToken token = ProcessingToken::SAMPLE_ACCURATE)
const;
178 unsigned int get_rate(
ProcessingToken token = ProcessingToken::SAMPLE_ACCURATE)
const;
199 return get_typed_clock<SampleClock>(ProcessingToken::SAMPLE_ACCURATE);
227 template <
typename ClockType>
230 return dynamic_cast<const ClockType&
>(get_clock(token));
240 template <
typename... Args>
243 auto it = find_task_by_name(name);
244 if (it != m_tasks.end() && it->routine && it->routine->is_active()) {
245 it->routine->update_params(std::forward<Args>(args)...);
258 template <
typename T>
261 auto it = find_task_by_name(name);
262 if (it != m_tasks.end() && it->routine && it->routine->is_active()) {
263 return it->routine->get_state<T>(state_key);
275 template <
typename T>
278 return [
this, name, state_key]() -> T {
279 if (
auto value = get_task_state<T>(name, state_key)) {
290 uint64_t get_next_task_id()
const;
303 std::vector<std::string> get_task_names()
const;
316 [[nodiscard]]
const std::vector<TaskEntry>& get_all_tasks();
321 void pause_all_tasks();
326 void resume_all_tasks();
331 void terminate_all_tasks();
355 return m_current_buffer_cycle;
364 m_current_buffer_cycle++;
367 void process_buffer_cycle_tasks();
375 std::string auto_generate_name(
const std::shared_ptr<Routine>& routine)
const;
382 std::vector<TaskEntry>::iterator find_task_by_name(
const std::string& name);
389 std::vector<TaskEntry>::const_iterator find_task_by_name(
const std::string& name)
const;
396 std::vector<TaskEntry>::iterator find_task_by_routine(
const std::shared_ptr<Routine>& routine);
417 void process_default(
ProcessingToken token, uint64_t processing_units);
422 void cleanup_completed_tasks();
430 void drain_pending_tasks();
438 void drain_conditional_pending();
445 bool initialize_routine_state(
const std::shared_ptr<Routine>& routine,
ProcessingToken token);
454 void start_conditional_thread();
478 void pump_conditional();
507 mutable std::atomic<uint64_t> m_next_task_id { 1 };
530 uint64_t m_current_buffer_cycle {};
532 uint64_t m_registered_sample_rate { 48000 };
533 uint32_t m_registered_frame_rate { 60 };
535 static constexpr size_t MAX_PENDING_TASKS = 256;
538 std::atomic<bool> active {
false };
540 bool is_addition {
true };
543 std::atomic<uint32_t> m_pending_count { 0 };
546 static constexpr size_t MAX_PENDING_CONDITIONAL = 64;
548 std::atomic<uint32_t> m_conditional_pending_count { 0 };
Abstract base interface for all clock types in the multimodal scheduling system.
Sample-accurate timing system for audio processing domain.
std::function< T()> create_value_accessor(const std::string &name, const std::string &state_key) const
Create value accessor function for named task.
uint64_t get_current_buffer_cycle() const
Get current buffer cycle for task scheduling Updated by AudioSubsystem at each buffer boundary.
SampleClock m_clock
The master sample clock for the processing engine.
uint32_t get_cleanup_threshold() const
Get the task cleanup threshold.
std::unordered_map< ProcessingToken, std::shared_ptr< IClock > > m_token_clocks
Clock instances for each processing domain.
std::unordered_map< ProcessingToken, unsigned int > m_token_rates
Default processing rates for each domain.
std::jthread m_conditional_thread
std::vector< TaskEntry > m_tasks
void tick_buffer_cycle()
Increment buffer cycle counter Called by AudioSubsystem at start of each buffer processing.
const SampleClock & get_sample_clock() const
Get the audio domain's SampleClock (legacy interface)
std::vector< TaskEntry > m_conditional_tasks
uint32_t m_cleanup_threshold
Threshold for task cleanup.
std::unordered_map< ProcessingToken, token_processing_func_t > m_token_processors
Custom processors for specific domains.
bool update_task_params(const std::string &name, Args &&... args)
Update parameters of a named task.
T * get_task_state(const std::string &name, const std::string &state_key) const
Get task state value by name and key.
const ClockType & get_typed_clock(ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Get a typed clock for a specific processing domain.
void set_cleanup_threshold(uint32_t threshold)
Set the task cleanup threshold.
const SampleClock & get_clock() const
Gets the primary clock (audio domain for legacy compatibility)
Token-based multimodal task scheduling system for unified coroutine processing.
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.
uint64_t seconds_to_units(double seconds, uint32_t rate)
Convert seconds to processing units for any rate.
DelayContext
Discriminator for different temporal delay mechanisms.
bool restart_task(const std::string &name)
Restarts a scheduled task.
uint64_t seconds_to_samples(double seconds)
Converts a time duration in seconds to the equivalent number of audio samples.
bool cancel_task(const std::string &name)
Cancels a scheduled task.
std::shared_ptr< Routine > routine
TaskEntry(const std::shared_ptr< Routine > &r, std::string n)