FastLED 3.10.8
Loading...
Searching...
No Matches
rpc_scheduler.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/stdint.h"
4#include "fl/stl/function.h"
6#include "fl/stl/noexcept.h"
7
8namespace fl {
9namespace net {
10
22template<typename Task = fl::function<void()>>
24public:
27
28 // Non-copyable but movable
30 RpcScheduler& operator=(const RpcScheduler&) FL_NO_EXCEPT = delete;
32 RpcScheduler& operator=(RpcScheduler&&) FL_NO_EXCEPT = default;
33
39 void schedule(u32 timestamp, Task task) FL_NO_EXCEPT {
40 mQueue.push({timestamp, fl::move(task)});
41 }
42
48 size_t tick(u32 currentTime) FL_NO_EXCEPT {
49 size_t executed = 0;
50
51 while (!mQueue.empty() && currentTime >= mQueue.top().executeAt) {
52 // Copy the task before popping (can't execute after pop)
53 ScheduledTask scheduledTask = mQueue.top();
54 mQueue.pop();
55 scheduledTask.task(); // Execute task
56 executed++;
57 }
58
59 return executed;
60 }
61
66 size_t pendingCount() const FL_NO_EXCEPT {
67 return mQueue.size();
68 }
69
74 mQueue.clear();
75 }
76
77private:
79 u32 executeAt; // Timestamp when to execute
80 Task task; // Task to execute
81
82 // Comparison for stable priority queue (earlier times = higher priority)
83 // priority_queue_stable uses fl::less by default, creating a max-heap
84 // Invert comparison so earlier (smaller) timestamps are "greater" = higher priority
85 bool operator<(const ScheduledTask& other) const FL_NO_EXCEPT {
86 return executeAt > other.executeAt; // Inverted: smaller timestamps = higher priority
87 }
88 };
89
91};
92
93} // namespace net
94} // namespace fl
size_t tick(u32 currentTime) FL_NO_EXCEPT
Execute all tasks with timestamp <= currentTime.
void clear() FL_NO_EXCEPT
Clear all scheduled tasks.
fl::priority_queue_stable< ScheduledTask > mQueue
size_t pendingCount() const FL_NO_EXCEPT
Get number of pending scheduled tasks.
void schedule(u32 timestamp, Task task) FL_NO_EXCEPT
Schedule a task for execution at specified timestamp.
RpcScheduler() FL_NO_EXCEPT=default
Stable priority queue that maintains FIFO ordering for equal-priority elements.
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:30
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179
bool operator<(const ScheduledTask &other) const FL_NO_EXCEPT