Skip to content
This repository was archived by the owner on Aug 19, 2021. It is now read-only.

Commit d6585fc

Browse files
committed
Removed the EventLoop class
The EventLoop was an interesting concept: the combination of an EventQueue and a Thread. The idea was that the EventLoop would provide a convenient coupling of these two concepts for use in module boundaries, and the EventLoop could abstract out some of the complexities with running an event queue in a thread. However, with the addition of the chain function, event queues can be easily composed without threads or indirect references to event queues. Threads can still be spawned dynamically in default-constructors, although the overhead is much more explicit and tangible. Additionally, it turned out there weren't that many complexities with running an event queue in a thread. There were, surprisingly, several problems with just passing the EventQueue::dispatch function to mbed's Thread constructor. 1. Split EventQueue::dispatch into overloaded functions Apparently, default parameters don't create another target for infering member-function-pointers. 2. Remove problematic template overloads in Thread constructor Issue is actually here ARMmbed/mbed-os#2395. The indirection in the nested callback templates prevented correct overload resolution and let to template-expansion errors. 3. Exposed break_ Allows threaded event queues to shutdown cleanly. With these changes, this code: EventLoop loop; loop.start(); loop.call(F); loop.stop(); Becomes: EventQueue queue; Thread thread; thread.start(&queue, &EventQueue::dispatch); queue.call(F); queue.break_(); thread.join(); Except now with 182 less lines to maintain
1 parent 242a8ec commit d6585fc

File tree

6 files changed

+14
-196
lines changed

6 files changed

+14
-196
lines changed

EventLoop.cpp

Lines changed: 0 additions & 49 deletions
This file was deleted.

EventLoop.h

Lines changed: 0 additions & 74 deletions
This file was deleted.

EventQueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void EventQueue::break_() {
2424
return equeue_break(&_equeue);
2525
}
2626

27-
unsigned EventQueue::get_tick() {
27+
unsigned EventQueue::tick() {
2828
return equeue_tick();
2929
}
3030

EventQueue.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ namespace events {
3131
#define EVENTS_EVENT_SIZE \
3232
(EQUEUE_EVENT_SIZE - 2*sizeof(void*) + sizeof(mbed::Callback<void()>))
3333

34-
/** DEFAULT_QUEUE_SIZE
35-
* default size of buffer for events
34+
/** EVENTS_QUEUE_SIZE
35+
* Default size of buffer for events
3636
*/
37-
#define DEFAULT_QUEUE_SIZE (32*EVENTS_EVENT_SIZE)
37+
#define EVENTS_QUEUE_SIZE (32*EVENTS_EVENT_SIZE)
3838

3939

4040
/** EventQueue
@@ -46,11 +46,11 @@ class EventQueue {
4646
/** Create an event queue
4747
*
4848
* @param queue_size Size of buffer to use for events
49-
* (default: DEFAULT_QUEUE_SIZE)
49+
* (default: EVENTS_QUEUE_SIZE)
5050
* @param queue_pointer Pointer to memory region to use for events
5151
* (default: NULL)
5252
*/
53-
EventQueue(unsigned queue_size=DEFAULT_QUEUE_SIZE,
53+
EventQueue(unsigned queue_size=EVENTS_QUEUE_SIZE,
5454
unsigned char *queue_pointer=NULL);
5555

5656
/** Destroy an event queue
@@ -65,11 +65,18 @@ class EventQueue {
6565
*/
6666
void dispatch(int ms=-1);
6767

68+
/** Break out of a running event loop
69+
*
70+
* Already pending events may finish executing, but the queue will not
71+
* continue to loop indefinitely.
72+
*/
73+
void break_();
74+
6875
/* Monotonic counter for the event queue
6976
* @return A monotonically incrementing counter in milliseconds
7077
* this count intentionally overflows to 0 after 2^32-1
7178
*/
72-
unsigned get_tick();
79+
unsigned tick();
7380

7481
/** Cancel events that are in flight
7582
*
@@ -241,7 +248,6 @@ class EventQueue {
241248
}
242249

243250
protected:
244-
void break_();
245251

246252
struct equeue _equeue;
247253
mbed::Callback<void(int)> _update;

TESTS/events/queue/main.cpp

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -102,40 +102,6 @@ void call_every_test() {
102102
queue.dispatch(N*100);
103103
}
104104

105-
#ifdef MBED_CONF_RTOS_PRESENT
106-
void event_loop_test1() {
107-
EventLoop loop;
108-
osStatus status = loop.start();
109-
TEST_ASSERT_EQUAL(osOK, status);
110-
111-
touched = false;
112-
loop.call(func0);
113-
Thread::yield();
114-
TEST_ASSERT(touched);
115-
116-
status = loop.stop();
117-
TEST_ASSERT_EQUAL(osOK, status);
118-
}
119-
120-
template <int N>
121-
void event_loop_test2() {
122-
EventLoop loop(osPriorityHigh);
123-
osStatus status = loop.start();
124-
TEST_ASSERT_EQUAL(osOK, status);
125-
126-
Timer tickers[N];
127-
128-
for (int i = 0; i < N; i++) {
129-
tickers[i].start();
130-
loop.call_every((i+1)*100, time_func, &tickers[i], (i+1)*100);
131-
Thread::yield();
132-
wait_ms(75);
133-
}
134-
135-
wait_ms(N*100);
136-
}
137-
#endif
138-
139105
struct big { char data[4096]; } big;
140106

141107
void allocate_failure_test1() {
@@ -176,28 +142,6 @@ void cancel_test1() {
176142
queue.dispatch(0);
177143
}
178144

179-
#ifdef MBED_CONF_RTOS_PRESENT
180-
template <int N>
181-
void cancel_test2() {
182-
EventLoop loop;
183-
osStatus status = loop.start();
184-
TEST_ASSERT_EQUAL(osOK, status);
185-
186-
int ids[N];
187-
188-
for (int i = 0; i < N; i++) {
189-
ids[i] = loop.call_in(1000, no);
190-
}
191-
192-
for (int i = N-1; i >= 0; i--) {
193-
loop.cancel(ids[i]);
194-
}
195-
196-
status = loop.stop();
197-
TEST_ASSERT_EQUAL(osOK, status);
198-
}
199-
#endif
200-
201145

202146
// Test setup
203147
utest::v1::status_t test_setup(const size_t number_of_cases) {
@@ -216,18 +160,10 @@ const Case cases[] = {
216160
Case("Testing call_in", call_in_test<20>),
217161
Case("Testing call_every", call_every_test<20>),
218162

219-
#ifdef MBED_CONF_RTOS_PRESENT
220-
Case("Testing event loop 1", event_loop_test1),
221-
Case("Testing event loop 2", event_loop_test2<20>),
222-
#endif
223-
224163
Case("Testing allocate failure 1", allocate_failure_test1),
225164
Case("Testing allocate failure 2", allocate_failure_test2),
226165

227166
Case("Testing event cancel 1", cancel_test1<20>),
228-
#ifdef MBED_CONF_RTOS_PRESENT
229-
Case("Testing event cancel 2", cancel_test2<20>),
230-
#endif
231167
};
232168

233169
Specification specification(test_setup, cases);

mbed_events.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#ifdef __cplusplus
2424

2525
#include "EventQueue.h"
26-
#include "EventLoop.h"
2726

2827
using namespace events;
2928

0 commit comments

Comments
 (0)