Skip to content

Commit f329c0b

Browse files
author
Teppo Järvelin
committed
Fix EventQueue::cancel to return value
1 parent f18e336 commit f329c0b

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

UNITTESTS/stubs/EventQueue_stub.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ unsigned EventQueue::tick()
4747
return EventQueue_stub::unsigned_value;
4848
}
4949

50-
void EventQueue::cancel(int id)
50+
bool EventQueue::cancel(int id)
5151
{
52+
return true;
5253
}
5354

5455
int EventQueue::time_left(int id)
@@ -62,6 +63,7 @@ void EventQueue::background(Callback<void(int)> update)
6263

6364
int EventQueue::chain(EventQueue *target)
6465
{
66+
return 0;
6567
}
6668

6769
} // namespace events

UNITTESTS/stubs/equeue_stub.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ int equeue_post(equeue_t *queue, void (*cb)(void *), void *event)
9292
return 0;
9393
}
9494

95-
void equeue_cancel(equeue_t *queue, int id)
95+
bool equeue_cancel(equeue_t *queue, int id)
9696
{
97-
97+
return true;
9898
}
9999

100100
void equeue_background(equeue_t *queue,

events/EventQueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ unsigned EventQueue::tick()
5050
return equeue_tick();
5151
}
5252

53-
void EventQueue::cancel(int id)
53+
bool EventQueue::cancel(int id)
5454
{
5555
return equeue_cancel(&_equeue, id);
5656
}

events/EventQueue.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,16 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
122122
*
123123
* The cancel function is IRQ safe.
124124
*
125-
* If called while the event queue's dispatch loop is active, the cancel
126-
* function does not guarantee that the event will not execute after it
127-
* returns, as the event may have already begun executing.
125+
* If called while the event queue's dispatch loop is active in another thread,
126+
* the cancel function does not guarantee that the event will not execute after it
127+
* returns, as the event may have already begun executing. A call made from
128+
* the same thread as the dispatch loop will always succeed with a valid id.
128129
*
129130
* @param id Unique id of the event
131+
* @return true if event was successfully cancelled
132+
* false if event was not cancelled (invalid id or executing already begun)
130133
*/
131-
void cancel(int id);
134+
bool cancel(int id);
132135

133136
/** Query how much time is left for delayed event
134137
*

events/equeue/equeue.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,18 @@ int equeue_post(equeue_t *q, void (*cb)(void *), void *p)
373373
return id;
374374
}
375375

376-
void equeue_cancel(equeue_t *q, int id)
376+
bool equeue_cancel(equeue_t *q, int id)
377377
{
378378
if (!id) {
379-
return;
379+
return false;
380380
}
381381

382382
struct equeue_event *e = equeue_unqueue(q, id);
383383
if (e) {
384384
equeue_dealloc(q, e + 1);
385+
return true;
386+
} else {
387+
return false;
385388
}
386389
}
387390

@@ -602,7 +605,7 @@ static void equeue_chain_dispatch(void *p)
602605
static void equeue_chain_update(void *p, int ms)
603606
{
604607
struct equeue_chain_context *c = (struct equeue_chain_context *)p;
605-
equeue_cancel(c->target, c->id);
608+
(void)equeue_cancel(c->target, c->id);
606609

607610
if (ms >= 0) {
608611
c->id = equeue_call_in(c->target, ms, equeue_chain_dispatch, c->q);

events/equeue/equeue.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ int equeue_post(equeue_t *queue, void (*cb)(void *), void *event);
182182
//
183183
// The equeue_cancel function is irq safe.
184184
//
185-
// If called while the event queue's dispatch loop is active, equeue_cancel
186-
// does not guarantee that the event will not not execute after it returns as
185+
// If called while the event queue's dispatch loop is active in another thread,
186+
// equeue_cancel does not guarantee that the event will not execute after it returns as
187187
// the event may have already begun executing.
188-
void equeue_cancel(equeue_t *queue, int id);
188+
// Returning true guarantees that cancel succeeded and event will not execute.
189+
// Returning false if invalid id or already started executing.
190+
bool equeue_cancel(equeue_t *queue, int id);
189191

190192
// Query how much time is left for delayed event
191193
//

0 commit comments

Comments
 (0)