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>;
119 MBED_DEPRECATED_SINCE(
"mbed-os-6.7.0",
"Use dispatch_for() to pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
194 template<
typename F,
typename A>
197 if (event->_equeue != &_equeue) {
200 return equeue_cancel_user_allocated(&_equeue, event);
240 template<
typename F,
typename A>
243 if (event && event->_equeue != &_equeue) {
246 return equeue_timeleft_user_allocated(&_equeue, &event->_e);
289 #if defined(DOXYGEN_ONLY) 321 template <
typename F,
typename ...Args>
322 int call(F f, Args ...args);
372 template <
typename T,
typename R,
typename ...Args>
373 int call(T *obj, R (T::*method)(Args ...args), Args ...args);
407 template <
typename F,
typename ...ArgTs>
408 int call_in(duration ms, F f, ArgTs ...args);
458 template <
typename T,
typename R,
typename ...ArgTs>
459 int call_in(duration ms, T *obj, R (T::*method)(ArgTs ...args), ArgTs ...args);
506 template <
typename F,
typename ...ArgTs>
507 int call_every(duration ms, F f, ArgTs ...args);
557 template <
typename T,
typename R,
typename ...ArgTs>
558 int call_every(duration ms, T *obj, R (T::*method)(ArgTs ...args), ArgTs ...args);
603 template <
typename R,
typename ...BoundArgTs,
typename ...ContextArgTs,
typename ...ArgTs>
604 Event<void(ArgTs...)>
event(R (*func)(BoundArgTs..., ArgTs...), ContextArgTs ...context_args);
651 template <
typename T,
typename R,
typename ...BoundArgTs,
typename ...ContextArgTs,
typename ...ArgTs>
652 Event<void(ArgTs...)>
event(T *obj, R (T::*method)(BoundArgTs..., ArgTs...), ContextArgTs ...context_args);
690 template <
typename R,
typename ...BoundArgTs,
typename ...ContextArgTs,
typename ...ArgTs>
730 template <
typename F,
typename... ArgTs>
736 template <
typename T,
typename R,
typename... ArgTs>
773 template <
typename F>
776 void *p = equeue_alloc(&_equeue,
sizeof(F));
781 F *e =
new (p) F(std::move(f));
782 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
783 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
792 template <
typename F,
typename... ArgTs>
793 int call(F f, ArgTs... args)
795 return call(context<F, ArgTs...>(std::move(f), args...));
801 template <
typename T,
typename R,
typename... ArgTs>
802 int call(T *obj, R(T::*method)(ArgTs...), ArgTs... args)
810 template <
typename T,
typename R,
typename... ArgTs>
811 int call(
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
819 template <
typename T,
typename R,
typename... ArgTs>
820 int call(
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
828 template <
typename T,
typename R,
typename... ArgTs>
829 int call(
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
848 template <
typename F>
851 void *p = equeue_alloc(&_equeue,
sizeof(F));
856 F *e =
new (p) F(std::move(f));
857 equeue_event_delay(e, ms.count());
858 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
859 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
868 template <
typename F,
typename... ArgTs>
869 int call_in(duration ms, F f, ArgTs... args)
871 return call_in(ms, context<F, ArgTs...>(std::move(f), args...));
877 template <
typename T,
typename R,
typename... ArgTs>
878 int call_in(duration ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
886 template <
typename T,
typename R,
typename... ArgTs>
887 int call_in(duration ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
895 template <
typename T,
typename R,
typename... ArgTs>
896 int call_in(duration ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
904 template <
typename T,
typename R,
typename... ArgTs>
905 int call_in(duration ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
924 template <
typename F>
925 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
928 return call_in(duration(ms), std::move(f));
937 template <
typename F,
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, F f, ArgTs... args)
941 return call_in(duration(ms), std::move(f), 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, T *obj, R(T::*method)(ArgTs...), 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 T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
961 return call_in(duration(ms), obj, method, args...);
967 template <
typename T,
typename R,
typename... ArgTs>
968 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
969 int call_in(
int ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
971 return call_in(duration(ms), obj, method, args...);
977 template <
typename T,
typename R,
typename... ArgTs>
978 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
979 int call_in(
int ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
981 return call_in(duration(ms), obj, method, args...);
1001 template <
typename F>
1004 void *p = equeue_alloc(&_equeue,
sizeof(F));
1009 F *e =
new (p) F(std::move(f));
1010 equeue_event_delay(e, ms.count());
1011 equeue_event_period(e, ms.count());
1012 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
1013 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
1022 template <
typename F,
typename... ArgTs>
1023 int call_every(duration ms, F f, ArgTs... args)
1025 return call_every(ms, context<F, ArgTs...>(std::move(f), args...));
1031 template <
typename T,
typename R,
typename... ArgTs>
1032 int call_every(duration ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
1040 template <
typename T,
typename R,
typename... ArgTs>
1041 int call_every(duration ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
1049 template <
typename T,
typename R,
typename... ArgTs>
1050 int call_every(duration ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
1058 template <
typename T,
typename R,
typename... ArgTs>
1059 int call_every(duration ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
1081 template <
typename F>
1082 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1085 return call_every(duration(ms), std::move(f));
1094 template <
typename F,
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`.")
1098 return call_every(duration(ms), std::move(f), 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, T *obj, R(T::*method)(ArgTs...), 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 T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
1118 return call_every(duration(ms), obj, method, args...);
1124 template <
typename T,
typename R,
typename... ArgTs>
1125 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1126 int call_every(
int ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
1128 return call_every(duration(ms), obj, method, args...);
1134 template <
typename T,
typename R,
typename... ArgTs>
1135 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
1136 int call_every(
int ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
1138 return call_every(duration(ms), obj, method, args...);
1150 template <
typename R,
typename... ArgTs>
1151 Event<void(ArgTs...)>
event(R(*func)(ArgTs...));
1156 template <
typename T,
typename R,
typename... ArgTs>
1157 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(ArgTs...));
1162 template <
typename T,
typename R,
typename... ArgTs>
1163 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(ArgTs...)
const);
1168 template <
typename T,
typename R,
typename... ArgTs>
1169 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(ArgTs...)
volatile);
1174 template <
typename T,
typename R,
typename... ArgTs>
1175 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile);
1180 template <
typename R,
typename... ArgTs>
1186 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
1187 Event<void(ArgTs...)>
event(R(*func)(B0, ArgTs...), C0 c0);
1192 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1193 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, ArgTs...), C0 c0);
1198 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1199 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, ArgTs...)
const, C0 c0);
1204 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1205 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, ArgTs...)
volatile, C0 c0);
1210 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1211 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, ArgTs...)
const volatile, C0 c0);
1216 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
1222 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1223 Event<void(ArgTs...)>
event(R(*func)(B0, B1, ArgTs...), C0 c0, C1 c1);
1228 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1229 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, ArgTs...), C0 c0, C1 c1);
1234 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1235 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, ArgTs...)
const, C0 c0, C1 c1);
1240 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1241 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
volatile, C0 c0, C1 c1);
1246 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1247 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
const volatile, C0 c0, C1 c1);
1252 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1258 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1259 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, ArgTs...), C0 c0, C1 c1, C2 c2);
1264 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1265 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, ArgTs...), C0 c0, C1 c1, C2 c2);
1270 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1271 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const, C0 c0, C1 c1, C2 c2);
1276 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1277 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2);
1282 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1283 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2);
1288 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1294 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1295 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3);
1300 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1301 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3);
1306 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1307 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const, C0 c0, C1 c1, C2 c2, C3 c3);
1312 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1313 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2, C3 c3);
1318 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1319 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);
1324 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1330 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>
1331 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1336 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>
1337 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);
1342 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>
1343 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);
1348 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>
1349 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);
1354 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>
1355 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);
1360 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>
1361 Event<void(ArgTs...)>
event(
mbed::Callback<R(B0, B1, B2, B3, B4, ArgTs...)> cb, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1372 template <
typename F,
typename... ArgTs>
1378 template <
typename T,
typename R,
typename... ArgTs>
1384 template <
typename T,
typename R,
typename... ArgTs>
1390 template <
typename T,
typename R,
typename... ArgTs>
1396 template <
typename T,
typename R,
typename... ArgTs>
1401 #if !defined(DOXYGEN_ONLY) 1402 template <
typename F>
1404 template <
typename F,
typename A>
1410 template <
typename F>
1411 static void function_call(
void *p)
1416 template <
typename F>
1417 static void function_dtor(
void *p)
1423 template <
typename F,
typename... ContextArgTs>
1426 template <
typename F>
1430 constexpr context(F f)
1433 template <
typename... ArgTs>
1434 void operator()(ArgTs... args)
1440 template <
typename F,
typename C0>
1441 struct context<F, C0> {
1445 constexpr context(F f, C0 c0)
1448 template <
typename... ArgTs>
1449 void operator()(ArgTs... args)
1455 template <
typename F,
typename C0,
typename C1>
1456 struct context<F, C0, C1> {
1461 constexpr context(F f, C0 c0, C1 c1)
1462 : f(f), c0(c0), c1(c1) {}
1464 template <
typename... ArgTs>
1465 void operator()(ArgTs... args)
1471 template <
typename F,
typename C0,
typename C1,
typename C2>
1472 struct context<F, C0, C1, C2> {
1478 constexpr context(F f, C0 c0, C1 c1, C2 c2)
1479 : f(f), c0(c0), c1(c1), c2(c2) {}
1481 template <
typename... ArgTs>
1482 void operator()(ArgTs... args)
1484 f(c0, c1, c2, args...);
1488 template <
typename F,
typename C0,
typename C1,
typename C2,
typename C3>
1489 struct context<F, C0, C1, C2, C3> {
1496 constexpr context(F f, C0 c0, C1 c1, C2 c2, C3 c3)
1497 : f(f), c0(c0), c1(c1), c2(c2), c3(c3) {}
1499 template <
typename... ArgTs>
1500 void operator()(ArgTs... args)
1502 f(c0, c1, c2, c3, args...);
1506 template <
typename F,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4>
1507 struct context<F, C0, C1, C2, C3, C4> {
1515 constexpr context(F f, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
1516 : f(f), c0(c0), c1(c1), c2(c2), c3(c3), c4(c4) {}
1518 template <
typename... ArgTs>
1519 void operator()(ArgTs... args)
1521 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.
void dispatch_once()
Dispatch currently queued events only and then terminate.
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.
void dispatch_for(duration ms)
Dispatch events.
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.