21 #include "events/equeue.h" 22 #include "platform/Callback.h" 23 #include "platform/NonCopyable.h" 39 #define EVENTS_EVENT_SIZE \ 40 (EQUEUE_EVENT_SIZE - 2*sizeof(void*) + sizeof(mbed::Callback<void()>)) 45 #define EVENTS_QUEUE_SIZE (32*EVENTS_EVENT_SIZE) 50 template <
typename F,
typename A>
64 using duration = std::chrono::duration<int, std::milli>;
174 template<
typename F,
typename A>
177 if (event->_equeue != &_equeue) {
180 return equeue_cancel_user_allocated(&_equeue, event);
220 template<
typename F,
typename A>
223 if (event && event->_equeue != &_equeue) {
226 return equeue_timeleft_user_allocated(&_equeue, &event->_e);
269 #if defined(DOXYGEN_ONLY) 301 template <
typename F,
typename ...Args>
302 int call(F f, Args ...args);
352 template <
typename T,
typename R,
typename ...Args>
353 int call(T *obj, R (T::*method)(Args ...args), Args ...args);
387 template <
typename F,
typename ...ArgTs>
388 int call_in(duration ms, F f, ArgTs ...args);
438 template <
typename T,
typename R,
typename ...ArgTs>
439 int call_in(duration ms, T *obj, R (T::*method)(ArgTs ...args), ArgTs ...args);
486 template <
typename F,
typename ...ArgTs>
487 int call_every(duration ms, F f, ArgTs ...args);
537 template <
typename T,
typename R,
typename ...ArgTs>
538 int call_every(duration ms, T *obj, R (T::*method)(ArgTs ...args), ArgTs ...args);
583 template <
typename R,
typename ...BoundArgTs,
typename ...ContextArgTs,
typename ...ArgTs>
584 Event<void(ArgTs...)>
event(R (*func)(BoundArgTs..., ArgTs...), ContextArgTs ...context_args);
631 template <
typename T,
typename R,
typename ...BoundArgTs,
typename ...ContextArgTs,
typename ...ArgTs>
632 Event<void(ArgTs...)>
event(T *obj, R (T::*method)(BoundArgTs..., ArgTs...), ContextArgTs ...context_args);
670 template <
typename R,
typename ...BoundArgTs,
typename ...ContextArgTs,
typename ...ArgTs>
710 template <
typename F,
typename... ArgTs>
716 template <
typename T,
typename R,
typename... ArgTs>
753 template <
typename F>
756 void *p = equeue_alloc(&_equeue,
sizeof(F));
761 F *e =
new (p) F(std::move(f));
762 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
763 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
772 template <
typename F,
typename... ArgTs>
773 int call(F f, ArgTs... args)
775 return call(context<F, ArgTs...>(std::move(f), args...));
781 template <
typename T,
typename R,
typename... ArgTs>
782 int call(T *obj, R(T::*method)(ArgTs...), ArgTs... args)
790 template <
typename T,
typename R,
typename... ArgTs>
791 int call(
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
799 template <
typename T,
typename R,
typename... ArgTs>
800 int call(
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
808 template <
typename T,
typename R,
typename... ArgTs>
809 int call(
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
828 template <
typename F>
831 void *p = equeue_alloc(&_equeue,
sizeof(F));
836 F *e =
new (p) F(std::move(f));
837 equeue_event_delay(e, ms.count());
838 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
839 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
848 template <
typename F,
typename... ArgTs>
849 int call_in(duration ms, F f, ArgTs... args)
851 return call_in(ms, context<F, ArgTs...>(std::move(f), args...));
857 template <
typename T,
typename R,
typename... ArgTs>
858 int call_in(duration ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
866 template <
typename T,
typename R,
typename... ArgTs>
867 int call_in(duration ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
875 template <
typename T,
typename R,
typename... ArgTs>
876 int call_in(duration ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
884 template <
typename T,
typename R,
typename... ArgTs>
885 int call_in(duration ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
904 template <
typename F>
905 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
908 return call_in(duration(ms), std::move(f));
917 template <
typename F,
typename... ArgTs>
918 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
919 int call_in(
int ms, F f, ArgTs... args)
921 return call_in(duration(ms), std::move(f), args...);
927 template <
typename T,
typename R,
typename... ArgTs>
928 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
929 int call_in(
int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
931 return call_in(duration(ms), obj, method, args...);
937 template <
typename T,
typename R,
typename... ArgTs>
938 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
939 int call_in(
int ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
941 return call_in(duration(ms), obj, method, args...);
947 template <
typename T,
typename R,
typename... ArgTs>
948 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
949 int call_in(
int ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
951 return call_in(duration(ms), obj, method, args...);
957 template <
typename T,
typename R,
typename... ArgTs>
958 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
959 int call_in(
int ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
961 return call_in(duration(ms), obj, method, args...);
981 template <
typename F>
984 void *p = equeue_alloc(&_equeue,
sizeof(F));
989 F *e =
new (p) F(std::move(f));
990 equeue_event_delay(e, ms.count());
991 equeue_event_period(e, ms.count());
992 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
993 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
1002 template <
typename F,
typename... ArgTs>
1003 int call_every(duration ms, F f, ArgTs... args)
1005 return call_every(ms, context<F, ArgTs...>(std::move(f), args...));
1011 template <
typename T,
typename R,
typename... ArgTs>
1012 int call_every(duration ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
1020 template <
typename T,
typename R,
typename... ArgTs>
1021 int call_every(duration ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
1029 template <
typename T,
typename R,
typename... ArgTs>
1030 int call_every(duration ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
1038 template <
typename T,
typename R,
typename... ArgTs>
1039 int call_every(duration ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
1061 template <
typename F>
1062 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1065 return call_every(duration(ms), std::move(f));
1074 template <
typename F,
typename... ArgTs>
1075 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1078 return call_every(duration(ms), std::move(f), args...);
1084 template <
typename T,
typename R,
typename... ArgTs>
1085 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1086 int call_every(
int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
1088 return call_every(duration(ms), obj, method, args...);
1094 template <
typename T,
typename R,
typename... ArgTs>
1095 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1096 int call_every(
int ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
1098 return call_every(duration(ms), obj, method, args...);
1104 template <
typename T,
typename R,
typename... ArgTs>
1105 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1106 int call_every(
int ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
1108 return call_every(duration(ms), obj, method, args...);
1114 template <
typename T,
typename R,
typename... ArgTs>
1115 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1116 int call_every(
int ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
1118 return call_every(duration(ms), obj, method, args...);
1130 template <
typename R,
typename... ArgTs>
1131 Event<void(ArgTs...)>
event(R(*func)(ArgTs...));
1136 template <
typename T,
typename R,
typename... ArgTs>
1137 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(ArgTs...));
1142 template <
typename T,
typename R,
typename... ArgTs>
1143 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(ArgTs...)
const);
1148 template <
typename T,
typename R,
typename... ArgTs>
1149 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(ArgTs...)
volatile);
1154 template <
typename T,
typename R,
typename... ArgTs>
1155 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile);
1160 template <
typename R,
typename... ArgTs>
1166 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
1167 Event<void(ArgTs...)>
event(R(*func)(B0, ArgTs...), C0 c0);
1172 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1173 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, ArgTs...), C0 c0);
1178 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1179 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, ArgTs...)
const, C0 c0);
1184 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1185 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, ArgTs...)
volatile, C0 c0);
1190 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1191 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, ArgTs...)
const volatile, C0 c0);
1196 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
1202 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1203 Event<void(ArgTs...)>
event(R(*func)(B0, B1, ArgTs...), C0 c0, C1 c1);
1208 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1209 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, ArgTs...), C0 c0, C1 c1);
1214 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1215 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, ArgTs...)
const, C0 c0, C1 c1);
1220 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1221 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
volatile, C0 c0, C1 c1);
1226 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1227 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
const volatile, C0 c0, C1 c1);
1232 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1238 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1239 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, ArgTs...), C0 c0, C1 c1, C2 c2);
1244 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1245 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, ArgTs...), C0 c0, C1 c1, C2 c2);
1250 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1251 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const, C0 c0, C1 c1, C2 c2);
1256 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1257 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2);
1262 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1263 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2);
1268 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1274 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1275 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3);
1280 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1281 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3);
1286 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1287 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const, C0 c0, C1 c1, C2 c2, C3 c3);
1292 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1293 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2, C3 c3);
1298 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1299 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2, C3 c3);
1304 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1310 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>
1311 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1316 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>
1317 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1322 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>
1323 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
const, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1328 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>
1329 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1334 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>
1335 Event<void(ArgTs...)>
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);
1340 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>
1341 Event<void(ArgTs...)>
event(
mbed::Callback<R(B0, B1, B2, B3, B4, ArgTs...)> cb, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1352 template <
typename F,
typename... ArgTs>
1358 template <
typename T,
typename R,
typename... ArgTs>
1364 template <
typename T,
typename R,
typename... ArgTs>
1370 template <
typename T,
typename R,
typename... ArgTs>
1376 template <
typename T,
typename R,
typename... ArgTs>
1381 #if !defined(DOXYGEN_ONLY) 1382 template <
typename F>
1384 template <
typename F,
typename A>
1390 template <
typename F>
1391 static void function_call(
void *p)
1396 template <
typename F>
1397 static void function_dtor(
void *p)
1403 template <
typename F,
typename... ContextArgTs>
1406 template <
typename F>
1410 constexpr context(F f)
1413 template <
typename... ArgTs>
1414 void operator()(ArgTs... args)
1420 template <
typename F,
typename C0>
1421 struct context<F, C0> {
1425 constexpr context(F f, C0 c0)
1428 template <
typename... ArgTs>
1429 void operator()(ArgTs... args)
1435 template <
typename F,
typename C0,
typename C1>
1436 struct context<F, C0, C1> {
1441 constexpr context(F f, C0 c0, C1 c1)
1442 : f(f), c0(c0), c1(c1) {}
1444 template <
typename... ArgTs>
1445 void operator()(ArgTs... args)
1451 template <
typename F,
typename C0,
typename C1,
typename C2>
1452 struct context<F, C0, C1, C2> {
1458 constexpr context(F f, C0 c0, C1 c1, C2 c2)
1459 : f(f), c0(c0), c1(c1), c2(c2) {}
1461 template <
typename... ArgTs>
1462 void operator()(ArgTs... args)
1464 f(c0, c1, c2, args...);
1468 template <
typename F,
typename C0,
typename C1,
typename C2,
typename C3>
1469 struct context<F, C0, C1, C2, C3> {
1476 constexpr context(F f, C0 c0, C1 c1, C2 c2, C3 c3)
1477 : f(f), c0(c0), c1(c1), c2(c2), c3(c3) {}
1479 template <
typename... ArgTs>
1480 void operator()(ArgTs... args)
1482 f(c0, c1, c2, c3, args...);
1486 template <
typename F,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4>
1487 struct context<F, C0, C1, C2, C3, C4> {
1495 constexpr context(F f, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
1496 : f(f), c0(c0), c1(c1), c2(c2), c3(c3), c4(c4) {}
1498 template <
typename... ArgTs>
1499 void operator()(ArgTs... args)
1501 f(c0, c1, c2, c3, c4, args...);
void dispatch_forever()
Dispatch events without a timeout.
int time_left(int id)
Query how much time is left for delayed event.
void break_dispatch()
Break out of a running event loop.
int call_every(duration ms, F f, ArgTs...args)
Calls an event on the queue periodically.
int chain(EventQueue *target)
Chain an event queue onto another event queue.
bool cancel(UserAllocatedEvent< F, A > *event)
Cancel an in-flight user allocated event.
Prevents generation of copy constructor and copy assignment operator in derived classes.
UserAllocatedEvent< F, void(ArgTs...)> make_user_allocated_event(F f, ArgTs...args)
Creates an user allocated event bound to the event queue.
unsigned tick()
Millisecond counter.
bool cancel(int id)
Cancel an in-flight event.
int call_in(duration ms, F f, ArgTs...args)
Calls an event on the queue after a specified delay.
void background(mbed::Callback< void(int)> update)
Background an event queue onto a single-shot timer-interrupt.
int call(F f, Args...args)
Calls an event on the queue.
Event< void(ArgTs...)> event(R(*func)(BoundArgTs..., ArgTs...), ContextArgTs...context_args)
Creates an event bound to the event queue.
~EventQueue()
Destroy an EventQueue.
EventQueue(unsigned size=(32 *(EQUEUE_EVENT_SIZE-2 *sizeof(void *)+sizeof(mbed::Callback< void()>))), unsigned char *buffer=NULL)
Create an EventQueue.
int time_left(UserAllocatedEvent< F, A > *event)
Query how much time is left for delayed UserAllocatedEvent.
Callback class based on template specialization.
#define EVENTS_QUEUE_SIZE
EVENTS_QUEUE_SIZE Default size of buffer for events.
void dispatch(int ms=-1)
Dispatch events.