ReactESP 3.3.1
Asynchronous programming for the ESP microcontrollers
Loading...
Searching...
No Matches
event_loop.h
Go to the documentation of this file.
1#ifndef REACTESP_SRC_EVENT_LOOP_H_
2#define REACTESP_SRC_EVENT_LOOP_H_
3
4#include <set>
5#include <vector>
6
7#include "events.h"
8
9namespace reactesp {
10
15class EventLoop {
16 friend class Event;
17 friend class TimedEvent;
18 friend class RepeatEvent;
19 friend class UntimedEvent;
20 friend class ISREvent;
21
22 public:
28 timed_queue_mutex_ = xSemaphoreCreateRecursiveMutex();
29 untimed_list_mutex_ = xSemaphoreCreateRecursiveMutex();
30 isr_event_list_mutex_ = xSemaphoreCreateRecursiveMutex();
31
32 // Initialize the mutexes
33
34 xSemaphoreGiveRecursive(timed_queue_mutex_);
35 xSemaphoreGiveRecursive(untimed_list_mutex_);
36 xSemaphoreGiveRecursive(isr_event_list_mutex_);
37 }
38
39 // Disabling copy constructors
40 EventLoop(const EventLoop&) = delete;
41 EventLoop(EventLoop&&) = delete;
42
43 int getTimedEventQueueSize() { return timed_events_.size(); }
44 int getUntimedEventQueueSize() { return untimed_list.size(); }
45 int getISREventQueueSize() { return isr_event_list.size(); }
50
53 uint64_t getEventCount() {
55 }
56
57 uint64_t getTickCount() { return tick_counter; }
58
59 void tick();
60
68 DelayEvent* onDelay(uint32_t delay, react_callback callback);
76 DelayEvent* onDelayMicros(uint64_t delay, react_callback callback);
84 RepeatEvent* onRepeat(uint32_t interval, react_callback callback);
92 RepeatEvent* onRepeatMicros(uint64_t interval, react_callback callback);
100 StreamEvent* onAvailable(Stream& stream, react_callback callback);
111 ISREvent* onInterrupt(uint8_t pin_number, int mode, react_callback callback);
119
120 void remove(TimedEvent* event);
121 void remove(UntimedEvent* event);
122 void remove(ISREvent* event);
123
129 void remove(Event* event);
130
131 protected:
132 // Timed events are stored in an ordered set, sorted by trigger time (with
133 // pointer tiebreaker for total ordering). This allows O(log n) removal of
134 // specific events, avoiding the zombie-event accumulation that occurred
135 // with the previous priority_queue + lazy-delete approach.
136 // The hot path uses C++17 extract()/insert() to reuse tree nodes without
137 // heap allocation; under C++14 it falls back to erase()/insert(), which
138 // does one heap alloc + dealloc per RepeatEvent tick.
139 using TimedEventSet = std::set<TimedEvent*, TriggerTimeCompare>;
141 // Untimed events are stored in a vector, which is traversed in order.
142 // Elements are rarely removed from the middle of the list, so a vector is
143 // acceptable.
144 std::vector<UntimedEvent*> untimed_list;
145 // ISR events are stored in a vector. The list is traversed or modified
146 // infrequently.
147 std::vector<ISREvent*> isr_event_list;
148
149 // Semaphores for accessing the above queues and lists
150 SemaphoreHandle_t timed_queue_mutex_;
151 SemaphoreHandle_t untimed_list_mutex_;
152 SemaphoreHandle_t isr_event_list_mutex_;
153
156 uint64_t tick_counter = 0;
157
158 void tickTimed();
159 void tickUntimed();
160};
161
162// Provide compatibility aliases for the old naming scheme
163
172
173} // namespace reactesp
174
175#endif // REACTESP_SRC_EVENT_LOOP_H_
Event that is triggered after a certain time delay.
Definition events.h:155
Events are code to be called when a given condition is fulfilled.
Definition events.h:55
Asynchronous event loop supporting timed (repeating and non-repeating), interrupt and stream events.
Definition event_loop.h:15
friend class TimedEvent
Definition event_loop.h:17
friend class UntimedEvent
Definition event_loop.h:19
void remove(TimedEvent *event)
DelayEvent * onDelayMicros(uint64_t delay, react_callback callback)
Create a new DelayEvent.
friend class ISREvent
Definition event_loop.h:20
EventLoop(EventLoop &&)=delete
EventLoop()
Construct a new EventLoop object.
Definition event_loop.h:26
SemaphoreHandle_t untimed_list_mutex_
Definition event_loop.h:151
StreamEvent * onAvailable(Stream &stream, react_callback callback)
Create a new StreamEvent.
friend class Event
Definition event_loop.h:16
TickEvent * onTick(react_callback callback)
Create a new TickEvent.
DelayEvent * onDelay(uint32_t delay, react_callback callback)
Create a new DelayEvent.
uint64_t untimed_event_counter
Definition event_loop.h:155
std::vector< UntimedEvent * > untimed_list
Definition event_loop.h:144
RepeatEvent * onRepeatMicros(uint64_t interval, react_callback callback)
Create a new RepeatEvent.
int getUntimedEventQueueSize()
Definition event_loop.h:44
uint64_t getTickCount()
Definition event_loop.h:57
SemaphoreHandle_t timed_queue_mutex_
Definition event_loop.h:150
ISREvent * onInterrupt(uint8_t pin_number, int mode, react_callback callback)
Create a new ISREvent (interrupt event).
RepeatEvent * onRepeat(uint32_t interval, react_callback callback)
Create a new RepeatEvent.
TimedEventSet timed_events_
Definition event_loop.h:140
uint64_t getUntimedEventCount()
Definition event_loop.h:52
SemaphoreHandle_t isr_event_list_mutex_
Definition event_loop.h:152
uint64_t getTimedEventCount()
Definition event_loop.h:51
uint64_t getEventCount()
Definition event_loop.h:53
int getTimedEventQueueSize()
Definition event_loop.h:43
uint64_t timed_event_counter
Definition event_loop.h:154
EventLoop(const EventLoop &)=delete
friend class RepeatEvent
Definition event_loop.h:18
std::vector< ISREvent * > isr_event_list
Definition event_loop.h:147
std::set< TimedEvent *, TriggerTimeCompare > TimedEventSet
Definition event_loop.h:139
Event that is triggered on an input pin change.
Definition events.h:254
Event that is triggered repeatedly.
Definition events.h:178
Event that is triggered when there is input available at the given Arduino Stream.
Definition events.h:219
Event that is triggered unconditionally at each execution loop.
Definition events.h:239
TimedEvents are called based on elapsing of time.
Definition events.h:77
Events that are triggered based on something else than time.
Definition events.h:203
RepeatEvent RepeatReaction
Definition event_loop.h:168
TimedEvent TimedReaction
Definition event_loop.h:165
EventLoop ReactESP
Definition event_loop.h:164
StreamEvent StreamReaction
Definition event_loop.h:170
UntimedEvent UntimedReaction
Definition event_loop.h:166
ISREvent ISRReaction
Definition event_loop.h:169
std::function< void()> react_callback
Definition events.h:12
TickEvent TickReaction
Definition event_loop.h:171
DelayEvent DelayReaction
Definition event_loop.h:167