Skip to content

Commit 9607cea

Browse files
authored
Merge pull request #13975 from adbridge/eventq
Update EventQueue API to use chrono times
2 parents dd33463 + c7c30fe commit 9607cea

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

events/include/events/EventQueue.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
8686
*/
8787
~EventQueue();
8888

89+
/** Dispatch events
90+
*
91+
* Executes events for the specified number of milliseconds.
92+
*
93+
* The dispatch_for() function is guaranteed to terminate after the elapsed wait.
94+
*
95+
* @param ms Time to wait for events in milliseconds, expressed as a
96+
* Chrono duration.
97+
*/
98+
void dispatch_for(duration ms);
99+
89100
/** Dispatch events
90101
*
91102
* Executes events until the specified milliseconds have passed.
@@ -96,23 +107,32 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
96107
* to terminate. When called with a timeout of 0, the dispatch function
97108
* does not wait and is IRQ safe.
98109
*
110+
* NOTE: Since the majority of the event library was updated to use
111+
* Chrono types (as part of the Mbed 6 release), this function will not
112+
* function as expected. Please update to use the new dispatch functions
113+
* to ensure correct functionality.
114+
*
99115
* @param ms Time to wait for events in milliseconds, a negative
100116
* value will dispatch events indefinitely
101117
* (default to -1)
102118
*/
119+
MBED_DEPRECATED_SINCE("mbed-os-6.7.0", "Use dispatch_for() to pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
103120
void dispatch(int ms = -1);
104121

105122
/** Dispatch events without a timeout
106123
*
107-
* This is equivalent to EventQueue::dispatch with no arguments, but
108-
* avoids overload ambiguities when passed as a callback.
124+
* Executes events indefinitely unless the dispatch loop is forcibly broken.
125+
* @See break_dispatch()
109126
*
110-
* @see EventQueue::dispatch
111127
*/
112-
void dispatch_forever()
113-
{
114-
dispatch();
115-
}
128+
void dispatch_forever();
129+
130+
/** Dispatch currently queued events only and then terminate
131+
*
132+
* In this case the dispatch function does not wait.
133+
*
134+
*/
135+
void dispatch_once();
116136

117137
/** Break out of a running event loop
118138
*

events/source/EventQueue.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,26 @@ EventQueue::~EventQueue()
4141
equeue_destroy(&_equeue);
4242
}
4343

44+
void EventQueue::dispatch_for(duration ms)
45+
{
46+
return equeue_dispatch(&_equeue, ms.count());
47+
}
48+
4449
void EventQueue::dispatch(int ms)
4550
{
4651
return equeue_dispatch(&_equeue, ms);
4752
}
4853

54+
void EventQueue::dispatch_forever()
55+
{
56+
return equeue_dispatch(&_equeue, -1);
57+
}
58+
59+
void EventQueue::dispatch_once()
60+
{
61+
return equeue_dispatch(&_equeue, 0);
62+
}
63+
4964
void EventQueue::break_dispatch()
5065
{
5166
return equeue_break(&_equeue);

events/tests/TESTS/events/queue/main.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ void simple_posts_test##i() { \
8989
\
9090
touched = false; \
9191
queue.call(func##i,##__VA_ARGS__); \
92-
queue.dispatch(0); \
92+
queue.dispatch_once(); \
9393
TEST_ASSERT(touched); \
9494
\
9595
touched = false; \
96-
queue.call_in(1ms, func##i,##__VA_ARGS__); \
97-
queue.dispatch(2); \
96+
queue.call_in(1ms, func##i,##__VA_ARGS__); \
97+
queue.dispatch_for(2ms); \
9898
TEST_ASSERT(touched); \
9999
\
100100
touched = false; \
101-
queue.call_every(1ms, func##i,##__VA_ARGS__); \
102-
queue.dispatch(2); \
101+
queue.call_every(1ms, func##i,##__VA_ARGS__); \
102+
queue.dispatch_for(2ms); \
103103
TEST_ASSERT(touched); \
104104
}
105105

@@ -129,7 +129,7 @@ void call_in_test()
129129
queue.call_in((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100);
130130
}
131131

132-
queue.dispatch(N * 100);
132+
queue.dispatch_for(N * 100ms);
133133
}
134134

135135
template <int N>
@@ -144,7 +144,7 @@ void call_every_test()
144144
queue.call_every((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100);
145145
}
146146

147-
queue.dispatch(N * 100);
147+
queue.dispatch_for(N * 100ms);
148148
}
149149

150150
void allocate_failure_test()
@@ -179,7 +179,7 @@ void cancel_test1()
179179
queue.cancel(ids[i]);
180180
}
181181

182-
queue.dispatch(0);
182+
queue.dispatch_once();
183183
}
184184

185185

@@ -235,7 +235,7 @@ void event_class_test()
235235
e1.post(1);
236236
e0.post();
237237

238-
queue.dispatch(0);
238+
queue.dispatch_once();
239239

240240
TEST_ASSERT_EQUAL(counter, 30);
241241
}
@@ -259,7 +259,7 @@ void event_class_helper_test()
259259
e1.post();
260260
e0.post();
261261

262-
queue.dispatch(0);
262+
queue.dispatch_once();
263263

264264
TEST_ASSERT_EQUAL(counter, 15);
265265
}
@@ -283,7 +283,7 @@ void event_inference_test()
283283
queue.event(callback(count5), 1).post(1, 1, 1, 1);
284284
queue.event(callback(count5)).post(1, 1, 1, 1, 1);
285285

286-
queue.dispatch(0);
286+
queue.dispatch_once();
287287

288288
TEST_ASSERT_EQUAL(counter, 60);
289289
}
@@ -317,7 +317,7 @@ void time_left_test()
317317
TEST_ASSERT(timeleft_events[0]);
318318
TEST_ASSERT(timeleft_events[1]);
319319

320-
queue.dispatch(300);
320+
queue.dispatch_for(300ms);
321321

322322
// Ensure check was called
323323
TEST_ASSERT(touched);
@@ -326,7 +326,7 @@ void time_left_test()
326326
int id = queue.call(func0);
327327
TEST_ASSERT(id);
328328
TEST_ASSERT_EQUAL(0, queue.time_left(id));
329-
queue.dispatch(10);
329+
queue.dispatch_for(10ms);
330330

331331
// Test invalid event id
332332
TEST_ASSERT_EQUAL(-1, queue.time_left(0));
@@ -418,7 +418,7 @@ void mixed_dynamic_static_events_queue_test()
418418
ue4.cancel();
419419
e2.cancel();
420420

421-
queue.dispatch(101);
421+
queue.dispatch_for(101ms);
422422

423423
TEST_ASSERT_EQUAL(true, touched);
424424
TEST_ASSERT_EQUAL(1, ue1_test.counter);
@@ -491,7 +491,7 @@ void static_events_queue_test()
491491
g_queue.cancel(&ue4);
492492
g_queue.cancel(&ue4);
493493

494-
g_queue.dispatch(400);
494+
g_queue.dispatch_for(400ms);
495495

496496
TEST_ASSERT_EQUAL(2, test1.counter);
497497
TEST_ASSERT_EQUAL(6, test2.counter);
@@ -500,7 +500,7 @@ void static_events_queue_test()
500500

501501
ue4.delay(1);
502502
TEST_ASSERT_EQUAL(true, ue4.try_call());
503-
g_queue.dispatch(1);
503+
g_queue.dispatch_for(1ms);
504504

505505
TEST_ASSERT_EQUAL(2, test1.counter);
506506
TEST_ASSERT_EQUAL(6, test2.counter);
@@ -525,7 +525,7 @@ void event_period_tests()
525525
event1.delay(10ms);
526526
event1.period(events::non_periodic);
527527
event1.post();
528-
period_tests_queue.dispatch(80);
528+
period_tests_queue.dispatch_for(80ms);
529529

530530
// Wait 100ms and check the event execution status
531531
wait_us(100 * 1000);
@@ -543,7 +543,7 @@ void event_period_tests()
543543
event2.delay(10ms);
544544
event2.period(-10ms);
545545
event2.post();
546-
period_tests_queue.dispatch(80);
546+
period_tests_queue.dispatch_for(80ms);
547547

548548
// Wait 100ms and check the event execution status
549549
wait_us(100 * 1000);
@@ -561,7 +561,7 @@ void event_period_tests()
561561
event3.delay(10ms);
562562
event3.period(0ms);
563563
event3.post();
564-
period_tests_queue.dispatch(80);
564+
period_tests_queue.dispatch_for(80ms);
565565

566566
// Wait 100ms and check the event execution status
567567
wait_us(100 * 1000);
@@ -578,7 +578,7 @@ void event_period_tests()
578578
event4.delay(10ms);
579579
event4.period(20ms);
580580
event4.post();
581-
period_tests_queue.dispatch(80);
581+
period_tests_queue.dispatch_for(80ms);
582582

583583
// Wait 100ms and check the event execution status
584584
wait_us(100 * 1000);

0 commit comments

Comments
 (0)