21 #include "events/EventQueue.h" 22 #include "platform/mbed_assert.h" 46 template <
typename... ArgTs>
49 using duration = std::chrono::duration<int, std::milli>;
63 _event =
static_cast<struct event *
>(
64 equeue_alloc(&q->_equeue,
sizeof(
struct event) +
sizeof(F)));
67 _event->equeue = &q->_equeue;
69 _event->delay = duration(0);
70 _event->period = duration(-1);
72 _event->post = &Event::event_post<F>;
73 _event->dtor = &Event::event_dtor<F>;
75 new (_event + 1) F(std::move(f));
98 new (
this)
Event(that);
110 if (_event->ref == 0) {
111 _event->dtor(_event);
112 equeue_dealloc(_event->equeue, _event);
133 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
155 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
180 _event->id = _event->post(_event, args...);
200 return call(args...);
208 static void thunk(
void *func, ArgTs... args)
210 return static_cast<Event *
>(func)->call(args...);
227 equeue_cancel(_event->equeue, _event->id);
240 int (*post)(
struct event *, ArgTs... args);
241 void (*dtor)(
struct event *);
247 template <
typename F>
248 static int event_post(
struct event *e, ArgTs... args)
250 typedef EventQueue::context<F, ArgTs...> C;
251 void *p = equeue_alloc(e->equeue,
sizeof(C));
256 new (p) C(*(F *)(e + 1), args...);
257 equeue_event_delay(p, e->delay.count());
258 equeue_event_period(p, e->period.count());
259 equeue_event_dtor(p, &EventQueue::function_dtor<C>);
260 return equeue_post(e->equeue, &EventQueue::function_call<C>, p);
263 template <
typename F>
264 static void event_dtor(
struct event *e)
266 ((F *)(e + 1))->~F();
278 template <
typename F,
typename... ContextArgTs>
280 Event(q,
EventQueue::context<F, ContextArgTs...>(f, context_args...)) { }
285 template <
typename T,
typename R,
typename B0>
292 template <
typename T,
typename R,
typename B0>
299 template <
typename T,
typename R,
typename B0>
306 template <
typename T,
typename R,
typename B0>
307 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, ArgTs...)
const volatile, B0 b0) :
313 template <
typename T,
typename R,
typename B0,
typename B1>
320 template <
typename T,
typename R,
typename B0,
typename B1>
321 Event(
EventQueue *q,
const T *obj, R(T::*method)(B0, B1, ArgTs...)
const, B0 b0, B1 b1) :
327 template <
typename T,
typename R,
typename B0,
typename B1>
328 Event(
EventQueue *q,
volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
volatile, B0 b0, B1 b1) :
334 template <
typename T,
typename R,
typename B0,
typename B1>
335 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
const volatile, B0 b0, B1 b1) :
341 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
342 Event(
EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, ArgTs...), B0 b0, B1 b1, B2 b2) :
348 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
349 Event(
EventQueue *q,
const T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const, B0 b0, B1 b1, B2 b2) :
355 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
356 Event(
EventQueue *q,
volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
volatile, B0 b0, B1 b1, B2 b2) :
362 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2>
363 Event(
EventQueue *q,
const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const volatile, B0 b0, B1 b1, B2 b2) :
369 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
370 Event(
EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3) :
376 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
377 Event(
EventQueue *q,
const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const, B0 b0, B1 b1, B2 b2, B3 b3) :
383 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
384 Event(
EventQueue *q,
volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
volatile, B0 b0, B1 b1, B2 b2, B3 b3) :
390 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3>
391 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) :
397 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
398 Event(
EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3, B4 b4) :
404 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
405 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) :
411 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
412 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) :
418 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4>
419 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) :
428 template <
typename R,
typename... ArgTs>
431 return Event<void(ArgTs...)>(
this, func);
434 template <
typename T,
typename R,
typename... ArgTs>
440 template <
typename T,
typename R,
typename... ArgTs>
446 template <
typename T,
typename R,
typename... ArgTs>
452 template <
typename T,
typename R,
typename... ArgTs>
458 template <
typename R,
typename... ArgTs>
461 return Event<void(ArgTs...)>(
this, cb);
464 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
467 return Event<void(ArgTs...)>(
this, func, c0);
470 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
476 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
482 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
488 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
489 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, ArgTs...)
const volatile, C0 c0)
494 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
497 return Event<void(ArgTs...)>(
this, cb, c0);
500 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
503 return Event<void(ArgTs...)>(
this, func, c0, c1);
506 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
512 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
513 Event<void(ArgTs...)>
EventQueue::event(
const T *obj, R(T::*method)(B0, B1, ArgTs...)
const, C0 c0, C1 c1)
518 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
519 Event<void(ArgTs...)>
EventQueue::event(
volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
volatile, C0 c0, C1 c1)
524 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
525 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
const volatile, C0 c0, C1 c1)
530 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
533 return Event<void(ArgTs...)>(
this, cb, c0, c1);
536 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
539 return Event<void(ArgTs...)>(
this, func, c0, c1, c2);
542 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
548 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
549 Event<void(ArgTs...)>
EventQueue::event(
const T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const, C0 c0, C1 c1, C2 c2)
554 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
555 Event<void(ArgTs...)>
EventQueue::event(
volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2)
560 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
561 Event<void(ArgTs...)>
EventQueue::event(
const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2)
566 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
569 return Event<void(ArgTs...)>(
this, cb, c0, c1, c2);
572 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
575 return Event<void(ArgTs...)>(
this, func, c0, c1, c2, c3);
578 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
579 Event<void(ArgTs...)>
EventQueue::event(T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3)
584 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
585 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)
590 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
591 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)
596 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
597 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)
602 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
605 return Event<void(ArgTs...)>(
this, cb, c0, c1, c2, c3);
608 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>
609 Event<void(ArgTs...)>
EventQueue::event(R(*func)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
611 return Event<void(ArgTs...)>(
this, func, c0, c1, c2, c3, c4);
614 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>
615 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)
620 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>
621 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)
626 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>
627 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)
632 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>
633 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)
638 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>
639 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)
641 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.