FastLED 3.9.15
Loading...
Searching...
No Matches
scheduler.cpp.hpp
Go to the documentation of this file.
1#include "fl/task/scheduler.h"
2#include "fl/stl/singleton.h"
3#include "fl/stl/chrono.h"
4#include "fl/log/log.h"
5
6namespace fl {
7namespace task {
8
9// Scheduler implementation
13
15 if (t.is_valid()) {
16 int task_id = mNextTaskId.fetch_add(1);
17 t._set_id(task_id);
18 mTasks.push_back(fl::move(t));
19 return task_id;
20 }
21 return 0; // Invalid task
22}
23
25 u32 current_time = fl::millis();
26
27 // Use index-based iteration to avoid iterator invalidation issues
28 for (fl::size i = 0; i < mTasks.size();) {
29 Handle& t = mTasks[i];
30
31 if (!t.is_valid() || t._is_canceled()) {
32 // erase() returns bool in fl::vector, not iterator
33 mTasks.erase(mTasks.begin() + i);
34 // Don't increment i since we just removed an element
35 } else {
36 // Check if task is ready to run (frame tasks will return false here)
37 bool should_run = t._ready_to_run(current_time);
38
39 if (should_run) {
40 // Update last run time for recurring tasks
41 t._set_last_run_time(current_time);
42
43 // Execute the task
44 if (t._has_then()) {
45 t._execute_then();
46 } else {
47 warn_no_then(t._id(), t._trace_label());
48 }
49
50 // Remove one-shot tasks, keep recurring ones
51 bool is_recurring = (t._type() == TaskType::kEveryMs || t._type() == TaskType::kAtFramerate);
52 if (is_recurring) {
53 ++i; // Keep recurring tasks
54 } else {
55 // erase() returns bool in fl::vector, not iterator
56 mTasks.erase(mTasks.begin() + i);
57 // Don't increment i since we just removed an element
58 }
59 } else {
60 ++i;
61 }
62 }
63 }
64}
65
69
73
75 u32 current_time = fl::millis();
76
77 // Use index-based iteration to avoid iterator invalidation issues
78 for (fl::size i = 0; i < mTasks.size();) {
79 Handle& t = mTasks[i];
80
81 if (!t.is_valid() || t._is_canceled()) {
82 // erase() returns bool in fl::vector, not iterator
83 mTasks.erase(mTasks.begin() + i);
84 // Don't increment i since we just removed an element
85 } else if (t._type() == task_type) {
86 // This is a frame task of the type we're looking for
87 bool should_run = t._ready_to_run_frame_task(current_time);
88
89 if (should_run) {
90 // Update last run time for frame tasks (though they don't use it)
91 t._set_last_run_time(current_time);
92
93 // Execute the task
94 if (t._has_then()) {
95 t._execute_then();
96 } else {
97 warn_no_then(t._id(), t._trace_label());
98 }
99
100 // Frame tasks are always one-shot, so remove them after execution
101 mTasks.erase(mTasks.begin() + i);
102 // Don't increment i since we just removed an element
103 } else {
104 ++i;
105 }
106 } else {
107 ++i; // Not the task type we're looking for
108 }
109 }
110}
111
112void Scheduler::warn_no_then(int task_id, const fl::string& trace_label) {
113 if (!trace_label.empty()) {
114 FL_WARN(fl::string("[fl::task] Warning: no then() callback set for Task#") << task_id << " launched at " << trace_label);
115 } else {
116 FL_WARN(fl::string("[fl::task] Warning: no then() callback set for Task#") << task_id);
117 }
118}
119
120void Scheduler::warn_no_catch(int task_id, const fl::string& trace_label, const Error& error) {
121 if (!trace_label.empty()) {
122 FL_WARN(fl::string("[fl::task] Warning: no catch_() callback set for Task#") << task_id << " launched at " << trace_label << ". Error: " << error.message);
123 } else {
124 FL_WARN(fl::string("[fl/task] Warning: no catch_() callback set for Task#") << task_id << ". Error: " << error.message);
125 }
126}
127
128} // namespace task
129} // namespace fl
FastLED chrono implementation - duration types for time measurements.
static T & instance() FL_NOEXCEPT
Definition singleton.h:41
bool empty() const FL_NOEXCEPT
Task Handle with fluent API (was class fl::task, renamed to avoid namespace collision)
Definition task.h:139
void warn_no_then(int task_id, const fl::string &trace_label)
fl::vector< Handle > mTasks
Definition scheduler.h:39
fl::atomic< int > mNextTaskId
Definition scheduler.h:40
static Scheduler & instance()
void warn_no_catch(int task_id, const fl::string &trace_label, const Error &error)
void update_tasks_of_type(TaskType task_type)
Scheduler() FL_NOEXCEPT
Definition scheduler.h:31
int add_task(Handle t)
#define FL_WARN(X)
Definition log.h:276
Centralized logging categories for FastLED hardware interfaces and subsystems.
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
TaskType
Definition task.h:116
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
Base definition for an LED controller.
Definition crgb.hpp:179
Task scheduler — manages timer and frame-based tasks.
fl::string message
Definition promise.h:40
Error type for promises.
Definition promise.h:39