Skip to content

Commit

Permalink
Fix EventQueue::cancel to return value
Browse files Browse the repository at this point in the history
  • Loading branch information
Teppo Järvelin committed Jun 5, 2019
1 parent f18e336 commit de555af
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
4 changes: 3 additions & 1 deletion UNITTESTS/stubs/EventQueue_stub.cpp
Expand Up @@ -47,8 +47,9 @@ unsigned EventQueue::tick()
return EventQueue_stub::unsigned_value;
}

void EventQueue::cancel(int id)
bool EventQueue::cancel(int id)
{
return true;
}

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

int EventQueue::chain(EventQueue *target)
{
return 0;
}

} // namespace events
4 changes: 2 additions & 2 deletions UNITTESTS/stubs/equeue_stub.c
Expand Up @@ -92,9 +92,9 @@ int equeue_post(equeue_t *queue, void (*cb)(void *), void *event)
return 0;
}

void equeue_cancel(equeue_t *queue, int id)
bool equeue_cancel(equeue_t *queue, int id)
{

return true;
}

void equeue_background(equeue_t *queue,
Expand Down
2 changes: 1 addition & 1 deletion events/EventQueue.cpp
Expand Up @@ -50,7 +50,7 @@ unsigned EventQueue::tick()
return equeue_tick();
}

void EventQueue::cancel(int id)
bool EventQueue::cancel(int id)
{
return equeue_cancel(&_equeue, id);
}
Expand Down
11 changes: 7 additions & 4 deletions events/EventQueue.h
Expand Up @@ -122,13 +122,16 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
*
* The cancel function is IRQ safe.
*
* If called while the event queue's dispatch loop is active, the cancel
* function does not guarantee that the event will not execute after it
* returns, as the event may have already begun executing.
* If called while the event queue's dispatch loop is active in another thread,
* the cancel function does not guarantee that the event will not execute after it
* returns, as the event may have already begun executing. A call made from
* the same thread as the dispatch loop will always succeed with a valid id.
*
* @param id Unique id of the event
* @return true if event was successfully cancelled
* false if event was not cancelled (invalid id or executing already begun)
*/
void cancel(int id);
bool cancel(int id);

/** Query how much time is left for delayed event
*
Expand Down
9 changes: 6 additions & 3 deletions events/equeue/equeue.c
Expand Up @@ -373,15 +373,18 @@ int equeue_post(equeue_t *q, void (*cb)(void *), void *p)
return id;
}

void equeue_cancel(equeue_t *q, int id)
bool equeue_cancel(equeue_t *q, int id)
{
if (!id) {
return;
return false;
}

struct equeue_event *e = equeue_unqueue(q, id);
if (e) {
equeue_dealloc(q, e + 1);
return true;
} else {
return false;
}
}

Expand Down Expand Up @@ -602,7 +605,7 @@ static void equeue_chain_dispatch(void *p)
static void equeue_chain_update(void *p, int ms)
{
struct equeue_chain_context *c = (struct equeue_chain_context *)p;
equeue_cancel(c->target, c->id);
(void)equeue_cancel(c->target, c->id);

if (ms >= 0) {
c->id = equeue_call_in(c->target, ms, equeue_chain_dispatch, c->q);
Expand Down
8 changes: 5 additions & 3 deletions events/equeue/equeue.h
Expand Up @@ -182,10 +182,12 @@ int equeue_post(equeue_t *queue, void (*cb)(void *), void *event);
//
// The equeue_cancel function is irq safe.
//
// If called while the event queue's dispatch loop is active, equeue_cancel
// does not guarantee that the event will not not execute after it returns as
// If called while the event queue's dispatch loop is active in another thread,
// equeue_cancel does not guarantee that the event will not execute after it returns as
// the event may have already begun executing.
void equeue_cancel(equeue_t *queue, int id);
// Returning true guarantees that cancel succeeded and event will not execute.
// Returning false if invalid id or already started executing.
bool equeue_cancel(equeue_t *queue, int id);

// Query how much time is left for delayed event
//
Expand Down

0 comments on commit de555af

Please sign in to comment.