FastLED 3.9.15
Loading...
Searching...
No Matches
async.h
Go to the documentation of this file.
1#pragma once
2
44
45#include "fl/namespace.h"
46#include "fl/vector.h"
47#include "fl/function.h"
48#include "fl/ptr.h"
49#include "fl/variant.h"
50#include "fl/promise.h"
51#include "fl/promise_result.h"
52#include "fl/singleton.h"
53#include "fl/thread_local.h"
54
55#include "fl/task.h"
56#include "fl/time.h"
57
58namespace fl {
59
60// Forward declarations
61class AsyncManager;
62
65public:
66 virtual ~async_runner() = default;
67
69 virtual void update() = 0;
70
72 virtual bool has_active_tasks() const = 0;
73
75 virtual size_t active_task_count() const = 0;
76};
77
80public:
81 static AsyncManager& instance();
82
84 void register_runner(async_runner* runner);
85
87 void unregister_runner(async_runner* runner);
88
90 void update_all();
91
93 bool has_active_tasks() const;
94
96 size_t total_active_tasks() const;
97
98private:
100};
101
109void async_yield();
110
111
121void async_run();
122
123
124
127size_t async_active_tasks();
128
131bool async_has_tasks();
132
174template<typename T>
176 // Handle invalid promises
177 if (!promise.valid()) {
178 return fl::result<T>(Error("Invalid promise"));
179 }
180
181 // If already completed, return immediately
182 if (promise.is_completed()) {
183 if (promise.is_resolved()) {
184 return fl::result<T>(promise.value());
185 } else {
186 return fl::result<T>(promise.error());
187 }
188 }
189
190 // Track recursion depth to prevent infinite loops
191 static fl::ThreadLocal<int> await_depth(0);
192 if (await_depth.access() > 10) {
193 return fl::result<T>(Error("await_top_level recursion limit exceeded - possible infinite loop"));
194 }
195
196 ++await_depth.access();
197
198 // Wait for promise to complete while pumping async tasks
199 int pump_count = 0;
200 const int max_pump_iterations = 10000; // Safety limit
201
202 while (!promise.is_completed() && pump_count < max_pump_iterations) {
203 // Update the promise first (in case it's not managed by async system)
204 promise.update();
205
206 // Check if completed after update
207 if (promise.is_completed()) {
208 break;
209 }
210
211 // Platform-agnostic async pump and yield
212 async_yield();
213
214 ++pump_count;
215 }
216
217 --await_depth.access();
218
219 // Check for timeout
220 if (pump_count >= max_pump_iterations) {
221 return fl::result<T>(Error("await_top_level timeout - promise did not complete"));
222 }
223
224 // Return the result
225 if (promise.is_resolved()) {
226 return fl::result<T>(promise.value());
227 } else {
228 return fl::result<T>(promise.error());
229 }
230}
231
233public:
234 static Scheduler& instance();
235
236 int add_task(task t);
237 void update();
238
239 // New methods for frame task handling
242
243 // For testing: clear all tasks
244 void clear_all_tasks() { mTasks.clear(); mNextTaskId = 1; }
245
246private:
247 friend class fl::Singleton<Scheduler>;
249
250 void warn_no_then(int task_id, const fl::string& trace_label);
251 void warn_no_catch(int task_id, const fl::string& trace_label, const Error& error);
252
253 // Helper method for running specific task types
254 void update_tasks_of_type(TaskType task_type);
255
257 int mNextTaskId = 1;
258};
259
260} // namespace fl
size_t total_active_tasks() const
Get total number of active tasks across all runners.
Definition async.cpp:51
bool has_active_tasks() const
Check if there are any active async tasks.
Definition async.cpp:42
static AsyncManager & instance()
Definition async.cpp:16
void update_all()
Update all registered async runners.
Definition async.cpp:33
void register_runner(async_runner *runner)
Register an async runner.
Definition async.cpp:20
fl::vector< async_runner * > mRunners
Definition async.h:99
void unregister_runner(async_runner *runner)
Unregister an async runner.
Definition async.cpp:26
Async task manager (singleton)
Definition async.h:79
int mNextTaskId
Definition async.h:257
static Scheduler & instance()
Definition async.cpp:91
void update()
Definition async.cpp:105
int add_task(task t)
Definition async.cpp:95
void clear_all_tasks()
Definition async.h:244
void update_before_frame_tasks()
Definition async.cpp:148
void warn_no_catch(int task_id, const fl::string &trace_label, const Error &error)
Definition async.cpp:203
void update_after_frame_tasks()
Definition async.cpp:152
fl::vector< task > mTasks
Definition async.h:256
void warn_no_then(int task_id, const fl::string &trace_label)
Definition async.cpp:195
void update_tasks_of_type(TaskType task_type)
Definition async.cpp:156
virtual ~async_runner()=default
virtual bool has_active_tasks() const =0
Check if this runner has active tasks.
virtual size_t active_task_count() const =0
Get number of active tasks (for debugging/monitoring)
virtual void update()=0
Update this async runner (called during async pumping)
Generic asynchronous task runner interface.
Definition async.h:64
const Error & error() const
Get the error (only valid if is_rejected() returns true)
Definition promise.h:181
void update()
Update promise state in main loop - should be called periodically This processes pending callbacks wh...
Definition promise.h:148
bool is_resolved() const
Check if promise is resolved (completed successfully)
Definition promise.h:160
bool valid() const
Check if promise is valid.
Definition promise.h:122
const T & value() const
Get the result value (only valid if is_resolved() returns true)
Definition promise.h:172
bool is_completed() const
Check if promise is completed (resolved or rejected)
Definition promise.h:154
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
Definition promise.h:72
Result type for promise operations.
static uint32_t t
Definition Luminova.h:54
Universal timing functions for FastLED.
Implements the FastLED namespace macros.
fl::result< T > await_top_level(fl::promise< T > promise)
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
Definition async.h:175
void async_yield()
Platform-specific async yield function.
Definition async.cpp:68
size_t async_active_tasks()
Get the number of active async tasks across all systems.
Definition async.cpp:82
TaskType
Definition task.h:43
ThreadLocalFake< T > ThreadLocal
void async_run()
Run all registered async tasks once.
Definition async.cpp:63
HeapVector< T, Allocator > vector
Definition vector.h:1214
bool async_has_tasks()
Check if any async systems have active tasks.
Definition async.cpp:86
IMPORTANT!
Definition crgb.h:20
Promise-based fluent API for FastLED - standalone async primitives.
Result type for promise operations with ok() semantics.
Error type for promises.
Definition promise.h:53