21 #include "events/EventQueue.h" 22 #include "platform/mbed_assert.h" 23 #include "platform/mbed_error.h" 31 static constexpr std::chrono::duration<int, std::milli> non_periodic{-1};
49 template <
typename... ArgTs>
52 using duration = std::chrono::duration<int, std::milli>;
66 _event =
static_cast<struct event *
>(
67 equeue_alloc(&q->_equeue,
sizeof(
struct event) +
sizeof(F)));
70 _event->equeue = &q->_equeue;
72 _event->delay = duration(0);
73 _event->period = non_periodic;
75 _event->post = &Event::event_post<F>;
76 _event->dtor = &Event::event_dtor<F>;
78 new (_event + 1) F(std::move(f));
101 new (
this)
Event(that);
113 if (_event->ref == 0) {
114 _event->dtor(_event);
115 equeue_dealloc(_event->equeue, _event);
137 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
153 if (p > duration(0)) {
157 if (p != non_periodic) {
158 MBED_WARNING(MBED_ERROR_INVALID_ARGUMENT,
159 "Invalid period specified, defaulting to non_periodic.");
161 _event->period = non_periodic;
171 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
196 _event->id = _event->post(_event, args...);
216 return call(args...);
224 static void thunk(
void *func, ArgTs... args)
226 return static_cast<Event *
>(func)->call(args...);
243 equeue_cancel(_event->equeue, _event->id);
256 int (*post)(
struct event *, ArgTs... args);
257 void (*dtor)(
struct event *);
263 template <
typename F>
264 static int event_post(
struct event *e, ArgTs... args)
266 typedef EventQueue::context<F, ArgTs...> C;
267 void *p = equeue_alloc(e->equeue,
sizeof(C));
272 new (p) C(*(F *)(e + 1), args...);
273 equeue_event_delay(p, e->delay.count());
274 equeue_event_period(p, e->period.count());
275 equeue_event_dtor(p, &EventQueue::function_dtor<C>);
276 return equeue_post(e->equeue, &EventQueue::function_call<C>, p);
279 template <
typename F>
280 static void event_dtor(
struct event *e)
282 ((F *)(e + 1))->~F();
294 template <
typename F,
typename... ContextArgTs>
296 Event(q,
EventQueue::context<F, ContextArgTs...>(f, context_args...)) { }
301 template <
typename T,
typename R,
typename B0>
308 template <
typename T,
typename R,
typename B0>
315 template <
typename T,
typename R,
typename B0>
322 template <
typename T,
typename R,
typename B0>
323 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, ArgTs...)
const volatile, B0 b0) :
329 template <
typename T,
typename R,
typename B0,
typename B1>
336 template <
typename T,
typename R,
typename B0,
typename B1>
337 Event(
EventQueue *q,
const T *obj, R(T::*method)(B0, B1, ArgTs...)
const, B0 b0, B1 b1) :
343 template <
typename T,
typename R,
typename B0,
typename B1>
344 Event(
EventQueue *q,
volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
volatile, B0 b0, B1 b1) :
350 template <
typename T,
typename R,
typename B0,
typename B1>
351 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
const volatile, B0 b0, B1 b1) :
357 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
358 Event(
EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, ArgTs...), B0 b0, B1 b1, B2 b2) :
364 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
365 Event(
EventQueue *q,
const T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const, B0 b0, B1 b1, B2 b2) :
371 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
372 Event(
EventQueue *q,
volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
volatile, B0 b0, B1 b1, B2 b2) :
378 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
379 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const volatile, B0 b0, B1 b1, B2 b2) :
385 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
386 Event(
EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3) :
392 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
393 Event(
EventQueue *q,
const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const, B0 b0, B1 b1, B2 b2, B3 b3) :
399 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
400 Event(
EventQueue *q,
volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
volatile, B0 b0, B1 b1, B2 b2, B3 b3) :
406 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
407 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const volatile, B0 b0, B1 b1, B2 b2, B3 b3) :
413 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
414 Event(
EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3, B4 b4) :
420 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
421 Event(
EventQueue *q,
const T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
const, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4) :
427 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
428 Event(
EventQueue *q,
volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
volatile, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4) :
434 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
435 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
const volatile, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4) :
444 template <
typename R,
typename... ArgTs>
447 return Event<void(ArgTs...)>(
this, func);
450 template <
typename T,
typename R,
typename... ArgTs>
456 template <
typename T,
typename R,
typename... ArgTs>
462 template <
typename T,
typename R,
typename... ArgTs>
468 template <
typename T,
typename R,
typename... ArgTs>
474 template <
typename R,
typename... ArgTs>
477 return Event<void(ArgTs...)>(
this, cb);
480 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
483 return Event<void(ArgTs...)>(
this, func, c0);
486 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
492 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
498 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
504 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
505 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, ArgTs...)
const volatile, C0 c0)
510 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
513 return Event<void(ArgTs...)>(
this, cb, c0);
516 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
519 return Event<void(ArgTs...)>(
this, func, c0, c1);
522 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
528 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
529 Event<void(ArgTs...)>
EventQueue::event(
const T *obj, R(T::*method)(B0, B1, ArgTs...)
const, C0 c0, C1 c1)
534 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
535 Event<void(ArgTs...)>
EventQueue::event(
volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
volatile, C0 c0, C1 c1)
540 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
541 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
const volatile, C0 c0, C1 c1)
546 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
549 return Event<void(ArgTs...)>(
this, cb, c0, c1);
552 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
555 return Event<void(ArgTs...)>(
this, func, c0, c1, c2);
558 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
564 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
565 Event<void(ArgTs...)>
EventQueue::event(
const T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const, C0 c0, C1 c1, C2 c2)
570 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
571 Event<void(ArgTs...)>
EventQueue::event(
volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2)
576 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
577 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2)
582 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
585 return Event<void(ArgTs...)>(
this, cb, c0, c1, c2);
588 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
591 return Event<void(ArgTs...)>(
this, func, c0, c1, c2, c3);
594 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
595 Event<void(ArgTs...)>
EventQueue::event(T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3)
600 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
601 Event<void(ArgTs...)>
EventQueue::event(
const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const, C0 c0, C1 c1, C2 c2, C3 c3)
606 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
607 Event<void(ArgTs...)>
EventQueue::event(
volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2, C3 c3)
612 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
613 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2, C3 c3)
618 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
621 return Event<void(ArgTs...)>(
this, cb, c0, c1, c2, c3);
624 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
625 Event<void(ArgTs...)>
EventQueue::event(R(*func)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
627 return Event<void(ArgTs...)>(
this, func, c0, c1, c2, c3, c4);
630 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
631 Event<void(ArgTs...)>
EventQueue::event(T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
636 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
637 Event<void(ArgTs...)>
EventQueue::event(
const T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
const, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
642 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
643 Event<void(ArgTs...)>
EventQueue::event(
volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
648 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
649 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
654 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
655 Event<void(ArgTs...)>
EventQueue::event(
mbed::Callback<R(B0, B1, B2, B3, B4, ArgTs...)> cb, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
657 return Event<void(ArgTs...)>(
this, cb, c0, c1, c2, c3, c4);
Event(EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, ArgTs...) const volatile, B0 b0, B1 b1)
Create an event.
int post(ArgTs...args) const
Posts an event onto the underlying event queue.
Event(EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...) volatile, B0 b0, B1 b1, B2 b2, B3 b3)
Create an event.
Event(EventQueue *q, const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...) const, B0 b0, B1 b1, B2 b2, B3 b3)
Create an event.
void cancel() const
Cancels the most recently posted event.
Event & operator=(const Event &that)
Assignment operator for events.
Event(EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
Create an event.
Event(EventQueue *q, const T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...) const, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
Create an event.
Event(const Event &e)
Copy constructor for events.
Event(EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3)
Create an event.
Event(EventQueue *q, T *obj, R(T::*method)(B0, B1, ArgTs...), B0 b0, B1 b1)
Create an event.
void period(duration p)
Configure the period of an event.
Event(EventQueue *q, const volatile T *obj, R(T::*method)(B0, ArgTs...) const volatile, B0 b0)
Create an event.
static void thunk(void *func, ArgTs...args)
Static thunk for passing as C-style function.
Event(EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...) const volatile, B0 b0, B1 b1, B2 b2)
Create an event.
Event(EventQueue *q, const T *obj, R(T::*method)(B0, ArgTs...) const, B0 b0)
Create an event.
Event(EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...) const volatile, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
Create an event.
Event(EventQueue *q, F f)
Create an event.
void call(ArgTs...args) const
Posts an event onto the underlying event queue, returning void.
Event(EventQueue *q, T *obj, R(T::*method)(B0, ArgTs...), B0 b0)
Create an event.
Event(EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...) volatile, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
Create an event.
Event(EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, ArgTs...), B0 b0, B1 b1, B2 b2)
Create an event.
void delay(duration d)
Configure the delay of an event.
Event(EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, ArgTs...) volatile, B0 b0, B1 b1)
Create an event.
Event(EventQueue *q, const T *obj, R(T::*method)(B0, B1, ArgTs...) const, B0 b0, B1 b1)
Create an event.
Event(EventQueue *q, F f, ContextArgTs...context_args)
Create an event.
Event(EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...) volatile, B0 b0, B1 b1, B2 b2)
Create an event.
void operator()(ArgTs...args) const
Posts an event onto the underlying event queue, returning void.
Event(EventQueue *q, const T *obj, R(T::*method)(B0, B1, B2, ArgTs...) const, B0 b0, B1 b1, B2 b2)
Create an event.
Event(EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...) const volatile, B0 b0, B1 b1, B2 b2, B3 b3)
Create an event.
Event< void(ArgTs...)> event(R(*func)(BoundArgTs..., ArgTs...), ContextArgTs...context_args)
Creates an event bound to the event queue.
Callback class based on template specialization.
Event(EventQueue *q, volatile T *obj, R(T::*method)(B0, ArgTs...) volatile, B0 b0)
Create an event.
~Event()
Destructor for events.