FastLED 3.10.6
Loading...
Searching...
No Matches
task.h
Go to the documentation of this file.
1#pragma once
2
102
103// allow-include-after-namespace
104
105#include "fl/stl/functional.h" // IWYU pragma: keep
106#include "fl/stl/string.h"
107#include "fl/system/trace.h"
108#include "fl/task/promise.h" // IWYU pragma: keep
109#include "fl/stl/shared_ptr.h"
110#include "fl/stl/cstddef.h"
111#include "fl/stl/noexcept.h"
112
113namespace fl {
114namespace task {
115
124
125// Forward declarations
126class ITaskImpl;
127class Coroutine; // IWYU pragma: keep
128
154
156class Handle {
157public:
158
160 Handle() FL_NO_EXCEPT = default;
161
163 Handle(const Handle&) FL_NO_EXCEPT = default;
165 Handle& operator=(const Handle&) FL_NO_EXCEPT = default;
169 Handle& operator=(Handle&&) FL_NO_EXCEPT = default;
170
180 explicit Handle(shared_ptr<ITaskImpl> impl) FL_NO_EXCEPT;
181
185 Handle& then(function<void()> on_then) FL_NO_EXCEPT;
189 Handle& catch_(function<void(const Error&)> on_catch) FL_NO_EXCEPT;
193
195 int id() const FL_NO_EXCEPT;
197 bool has_then() const FL_NO_EXCEPT;
199 bool has_catch() const FL_NO_EXCEPT;
201 string trace_label() const FL_NO_EXCEPT;
203 TaskType type() const FL_NO_EXCEPT;
205 int interval_ms() const FL_NO_EXCEPT;
210 u32 last_run_time() const FL_NO_EXCEPT;
216 bool ready_to_run(u32 current_time) const FL_NO_EXCEPT;
218 bool is_valid() const FL_NO_EXCEPT;
220 bool isCoroutine() const FL_NO_EXCEPT;
221
224 void stop() FL_NO_EXCEPT;
227 bool isRunning() const FL_NO_EXCEPT;
228
229private:
230 friend class Scheduler;
231
232 // Internal methods for Scheduler (friend access only)
233 void _set_id(int id) FL_NO_EXCEPT;
234 int _id() const FL_NO_EXCEPT;
235 bool _is_canceled() const FL_NO_EXCEPT;
236 bool _ready_to_run(u32 current_time) const FL_NO_EXCEPT;
237 bool _ready_to_run_frame_task(u32 current_time) const FL_NO_EXCEPT;
239 bool _has_then() const FL_NO_EXCEPT;
241 void _execute_catch(const Error& error) FL_NO_EXCEPT;
243 string _trace_label() const FL_NO_EXCEPT;
244
246};
247
257
266Handle at_framerate(int fps, const TracePoint& trace) FL_NO_EXCEPT;
267
271
280
293Handle after_frame(function<void()> on_then) FL_NO_EXCEPT;
299Handle after_frame(function<void()> on_then, const TracePoint& trace) FL_NO_EXCEPT;
304
308
311class Coroutine; // IWYU pragma: keep
312
313} // namespace task
314} // namespace fl
int interval_ms() const FL_NO_EXCEPT
Returns the interval between runs, in milliseconds.
Definition task.cpp.hpp:307
void set_last_run_time(u32 time) FL_NO_EXCEPT
Records the timestamp of the most recent run.
u32 last_run_time() const FL_NO_EXCEPT
Returns the timestamp of the most recent run, in milliseconds.
Definition task.cpp.hpp:309
Handle & then(function< void()> on_then) FL_NO_EXCEPT
Sets the callback invoked when the task runs successfully.
Definition task.cpp.hpp:276
bool has_then() const FL_NO_EXCEPT
Returns whether a success callback is configured.
Definition task.cpp.hpp:303
bool _has_then() const FL_NO_EXCEPT
Definition task.cpp.hpp:378
int _id() const FL_NO_EXCEPT
Definition task.cpp.hpp:373
void _execute_catch(const Error &error) FL_NO_EXCEPT
Definition task.cpp.hpp:380
void _execute_then() FL_NO_EXCEPT
Definition task.cpp.hpp:379
Handle & catch_(function< void(const Error &)> on_catch) FL_NO_EXCEPT
Sets the callback invoked when task execution reports an error.
Definition task.cpp.hpp:287
bool _is_canceled() const FL_NO_EXCEPT
Definition task.cpp.hpp:374
void stop() FL_NO_EXCEPT
Stops the coroutine represented by this handle.
Definition task.cpp.hpp:316
bool _ready_to_run(u32 current_time) const FL_NO_EXCEPT
Handle & cancel() FL_NO_EXCEPT
Prevents future scheduled executions of the task.
Definition task.cpp.hpp:294
void set_interval_ms(int interval_ms) FL_NO_EXCEPT
Changes the interval between runs.
Definition task.cpp.hpp:308
bool is_valid() const FL_NO_EXCEPT
Returns whether this handle refers to a task.
Definition task.cpp.hpp:312
bool isRunning() const FL_NO_EXCEPT
Returns whether the represented coroutine is currently running.
Definition task.cpp.hpp:317
bool has_catch() const FL_NO_EXCEPT
Returns whether an error callback is configured.
Definition task.cpp.hpp:304
bool isCoroutine() const FL_NO_EXCEPT
Returns whether this handle refers to an OS-level coroutine.
Definition task.cpp.hpp:313
TaskType _type() const FL_NO_EXCEPT
Definition task.cpp.hpp:381
Handle() FL_NO_EXCEPT=default
Constructs an empty, invalid handle.
string _trace_label() const FL_NO_EXCEPT
Definition task.cpp.hpp:382
bool ready_to_run(u32 current_time) const FL_NO_EXCEPT
Tests whether the task is ready at a given timestamp.
bool _ready_to_run_frame_task(u32 current_time) const FL_NO_EXCEPT
void _set_last_run_time(u32 time) FL_NO_EXCEPT
string trace_label() const FL_NO_EXCEPT
Returns the label of the task's creation trace, if available.
Definition task.cpp.hpp:305
void _set_id(int id) FL_NO_EXCEPT
Definition task.cpp.hpp:372
shared_ptr< ITaskImpl > mImpl
Definition task.h:245
TaskType type() const FL_NO_EXCEPT
Returns the task's scheduling mode.
Definition task.cpp.hpp:306
friend class Scheduler
Definition task.h:230
Handle before_frame()
Frame tasks recur: every overload below schedules a task that runs once per FastLED frame and keeps d...
Definition task.cpp.hpp:336
Handle after_frame()
Creates a task that runs after every FastLED frame until canceled.
Definition task.cpp.hpp:344
Handle coroutine(const CoroutineConfig &config)
Creates and starts an OS-level coroutine task.
Definition task.cpp.hpp:364
Handle at_framerate(int fps)
Creates a task that runs at a fixed frame rate.
Definition task.cpp.hpp:328
Handle every_ms(int interval_ms)
Creates a task that runs at a fixed millisecond interval.
Definition task.cpp.hpp:320
TaskType
Identifies how a scheduled task is triggered.
Definition task.h:117
@ kBeforeFrame
Runs immediately before each FastLED frame is rendered.
Definition task.h:120
@ kAfterFrame
Runs immediately after each FastLED frame is rendered.
Definition task.h:121
@ kAtFramerate
Runs at a fixed number of times per second.
Definition task.h:119
@ kEveryMs
Runs after a fixed time interval.
Definition task.h:118
@ kCoroutine
Runs as an operating-system or RTOS task.
Definition task.h:122
void exit_current()
Terminates the currently executing coroutine.
Definition task.cpp.hpp:369
function< void()> func
Function executed by the coroutine, copied at creation time.
Definition task.h:138
u8 priority
Platform task priority relative to the platform idle priority, default 5.
Definition task.h:148
size_t stack_size
Requested stack size in bytes, default 4096.
Definition task.h:144
optional< TracePoint > trace
Optional location where the task was created, reported by Handle::trace_label().
Definition task.h:135
optional< int > core_id
Optional CPU core affinity, unset by default (no affinity).
Definition task.h:152
string name
Diagnostic name reported by the platform. Defaults to "task".
Definition task.h:140
Function and platform tuning for an OS-level coroutine task.
Definition task.h:132
unsigned char u8
Definition stdint.h:131
fl::u64 time() FL_NO_EXCEPT
Alias for millis64() - returns 64-bit millisecond time.
Definition chrono.h:346
Optional< T > optional
Definition optional.h:16
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
fl::tuple< const char *, int, fl::u32 > TracePoint
A structure to hold source trace information.
Definition trace.h:51
Base definition for an LED controller.
Definition crgb.hpp:179
Promise-based fluent API for FastLED - standalone async primitives.
Error type for promises.
Definition promise.h:39