FastLED 3.9.15
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_NOEXCEPT = delete;
32 RpcScheduler& operator=(RpcScheduler&&) FL_NOEXCEPT = default;
33
39 void schedule(u32 timestamp, Task task) {
40 mQueue.push({timestamp, fl::move(task)});
41 }
42
48 size_t tick(u32 currentTime) {
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 {
67 return mQueue.size();
68 }
69
73 void clear() {
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 {
86 return executeAt > other.executeAt; // Inverted: smaller timestamps = higher priority
87 }
88 };
89
91};
92
93} // namespace net
94} // namespace fl
fl::priority_queue_stable< ScheduledTask > mQueue
void schedule(u32 timestamp, Task task)
Schedule a task for execution at specified timestamp.
RpcScheduler() FL_NOEXCEPT=default
size_t tick(u32 currentTime)
Execute all tasks with timestamp <= currentTime.
void clear()
Clear all scheduled tasks.
size_t pendingCount() const
Get number of pending scheduled tasks.
Stable priority queue that maintains FIFO ordering for equal-priority elements.
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
bool operator<(const ScheduledTask &other) const