Skip to content

Commit cca1e5d

Browse files
user allocated events: fix event cancelling
skip event canceling when already canceled or dispatched
1 parent f469f71 commit cca1e5d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

events/UserAllocatedEvent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class UserAllocatedEvent<F, void(ArgTs...)> {
243243
*/
244244
bool cancel()
245245
{
246-
return equeue_cancel_user_allocated(_equeue, &_e);
246+
return _post_ref > 0 ? equeue_cancel_user_allocated(_equeue, &_e) : false;
247247
}
248248

249249

events/source/equeue.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ void equeue_dealloc(equeue_t *q, void *p)
229229
e->dtor(e + 1);
230230
}
231231

232-
if (!EQUEUE_IS_USER_ALLOCATED_EVENT(e)) {
232+
if (EQUEUE_IS_USER_ALLOCATED_EVENT(e)) {
233+
e->id = 0;
234+
} else {
233235
equeue_mem_dealloc(q, e);
234236
}
235237
}
@@ -402,6 +404,10 @@ void equeue_post_user_allocated(equeue_t *q, void (*cb)(void *), void *p)
402404
unsigned tick = equeue_tick();
403405
e->cb = cb;
404406
e->target = tick + e->target;
407+
// for user allocated events use event id to track event state
408+
// 1 - posted
409+
// 0 - canceled or dispatching done
410+
e->id = 1;
405411

406412
equeue_enqueue(q, e, tick);
407413
equeue_sema_signal(&q->eventsema);
@@ -424,7 +430,7 @@ bool equeue_cancel(equeue_t *q, int id)
424430

425431
bool equeue_cancel_user_allocated(equeue_t *q, void *e)
426432
{
427-
if (!e) {
433+
if (!e || ((struct equeue_event *)e)->id == 0) {
428434
return false;
429435
}
430436

0 commit comments

Comments
 (0)