Add Chrono support to Event/EventQueue

pull/12425/head
Kevin Bracey 2020-02-13 13:06:11 +02:00
parent 8f490f6f24
commit 0eff3340d2
3 changed files with 242 additions and 53 deletions

View File

@ -17,6 +17,7 @@
#ifndef EVENT_H #ifndef EVENT_H
#define EVENT_H #define EVENT_H
#include <utility>
#include "events/EventQueue.h" #include "events/EventQueue.h"
#include "platform/mbed_assert.h" #include "platform/mbed_assert.h"
@ -45,6 +46,8 @@ class Event;
template <typename... ArgTs> template <typename... ArgTs>
class Event<void(ArgTs...)> { class Event<void(ArgTs...)> {
public: public:
using duration = std::chrono::duration<int, std::milli>;
/** Create an event /** Create an event
* *
* Constructs an event bound to the specified event queue. The specified * Constructs an event bound to the specified event queue. The specified
@ -63,13 +66,13 @@ public:
if (_event) { if (_event) {
_event->equeue = &q->_equeue; _event->equeue = &q->_equeue;
_event->id = 0; _event->id = 0;
_event->delay = 0; _event->delay = duration(0);
_event->period = -1; _event->period = duration(-1);
_event->post = &Event::event_post<F>; _event->post = &Event::event_post<F>;
_event->dtor = &Event::event_dtor<F>; _event->dtor = &Event::event_dtor<F>;
new (_event + 1) F(f); new (_event + 1) F(std::move(f));
_event->ref = 1; _event->ref = 1;
} }
@ -113,26 +116,48 @@ public:
/** Configure the delay of an event /** Configure the delay of an event
* *
* @param delay Millisecond delay before dispatching the event * @param d Millisecond delay before dispatching the event
*/ */
void delay(int delay) void delay(duration d)
{ {
if (_event) { if (_event) {
_event->delay = delay; _event->delay = d;
} }
} }
/** Configure the delay of an event
* @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
*
* @param d Millisecond delay before dispatching the event
*/
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
void delay(int d)
{
delay(duration(d));
}
/** Configure the period of an event /** Configure the period of an event
* *
* @param period Millisecond period for repeatedly dispatching an event * @param p Millisecond period for repeatedly dispatching an event
*/ */
void period(int period) void period(duration p)
{ {
if (_event) { if (_event) {
_event->period = period; _event->period = p;
} }
} }
/** Configure the period of an event
* @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
*
* @param p Millisecond period for repeatedly dispatching an event
*/
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
void period(int p)
{
period(duration(p));
}
/** Posts an event onto the underlying event queue /** Posts an event onto the underlying event queue
* *
* The event is posted to the underlying queue and is executed in the * The event is posted to the underlying queue and is executed in the
@ -209,8 +234,8 @@ private:
equeue_t *equeue; equeue_t *equeue;
int id; int id;
int delay; duration delay;
int period; duration period;
int (*post)(struct event *, ArgTs... args); int (*post)(struct event *, ArgTs... args);
void (*dtor)(struct event *); void (*dtor)(struct event *);
@ -229,8 +254,8 @@ private:
} }
new (p) C(*(F *)(e + 1), args...); new (p) C(*(F *)(e + 1), args...);
equeue_event_delay(p, e->delay); equeue_event_delay(p, e->delay.count());
equeue_event_period(p, e->period); equeue_event_period(p, e->period.count());
equeue_event_dtor(p, &EventQueue::function_dtor<C>); equeue_event_dtor(p, &EventQueue::function_dtor<C>);
return equeue_post(e->equeue, &EventQueue::function_call<C>, p); return equeue_post(e->equeue, &EventQueue::function_call<C>, p);
} }

View File

@ -22,6 +22,8 @@
#include "platform/Callback.h" #include "platform/Callback.h"
#include "platform/NonCopyable.h" #include "platform/NonCopyable.h"
#include <cstddef> #include <cstddef>
#include <utility>
#include <chrono>
#include <new> #include <new>
namespace events { namespace events {
@ -59,6 +61,8 @@ class UserAllocatedEvent;
*/ */
class EventQueue : private mbed::NonCopyable<EventQueue> { class EventQueue : private mbed::NonCopyable<EventQueue> {
public: public:
using duration = std::chrono::duration<int, std::milli>;
/** Create an EventQueue /** Create an EventQueue
* *
* Create an event queue. The event queue either allocates a buffer of * Create an event queue. The event queue either allocates a buffer of
@ -358,6 +362,7 @@ public:
* events out of IRQ contexts. * events out of IRQ contexts.
* *
* @param ms Time to delay in milliseconds * @param ms Time to delay in milliseconds
* @param f Function to execute in the context of the dispatch loop
* @param args Arguments to pass to the callback * @param args Arguments to pass to the callback
* @return A unique ID that represents the posted event and can * @return A unique ID that represents the posted event and can
* be passed to cancel, or an ID of 0 if there is not * be passed to cancel, or an ID of 0 if there is not
@ -365,21 +370,22 @@ public:
* *
* @code * @code
* #include "mbed.h" * #include "mbed.h"
* using namespace std::chrono_literals;
* *
* int main() { * int main() {
* // creates a queue with the default size * // creates a queue with the default size
* EventQueue queue; * EventQueue queue;
* *
* // events are simple callbacks * // events are simple callbacks
* queue.call_in(2000, printf, "called in 2 seconds\n"); * queue.call_in(2s, printf, "called in 2 seconds\n");
* *
* // the dispatch methods executes events * // the dispatch methods executes events
* queue.dispatch(); * queue.dispatch();
* } * }
* @endcode * @endcode
*/ */
template <typename F, typename ...Args> template <typename F, typename ...ArgTs>
int call_in(int ms, Args ...args); int call_in(duration ms, F f, ArgTs ...args);
/** Calls an event on the queue after a specified delay /** Calls an event on the queue after a specified delay
* *
@ -399,6 +405,7 @@ public:
* *
* @code * @code
* #include "mbed.h" * #include "mbed.h"
* using namespace std::chrono_literals;
* *
* class EventHandler { * class EventHandler {
* int _id; * int _id;
@ -419,7 +426,7 @@ public:
* *
* // events are simple callbacks, call object method in 2 seconds * // events are simple callbacks, call object method in 2 seconds
* // with provided parameter * // with provided parameter
* queue.call_in(2000, &handler_cb, &EventHandler::handler, 4); * queue.call_in(2s, &handler_cb, &EventHandler::handler, 4);
* *
* // the dispatch method executes events * // the dispatch method executes events
* queue.dispatch(); * queue.dispatch();
@ -428,8 +435,8 @@ public:
*/ */
// AStyle ignore, not handling correctly below // AStyle ignore, not handling correctly below
// *INDENT-OFF* // *INDENT-OFF*
template <typename T, typename R, typename ...Args> template <typename T, typename R, typename ...ArgTs>
int call_in(int ms, T *obj, R (T::*method)(Args ...args), Args ...args); int call_in(duration ms, T *obj, R (T::*method)(ArgTs ...args), ArgTs ...args);
// *INDENT-ON* // *INDENT-ON*
/** Calls an event on the queue periodically /** Calls an event on the queue periodically
@ -452,6 +459,7 @@ public:
* *
* @code * @code
* #include "mbed.h" * #include "mbed.h"
* using namespace std::chrono_literals;
* *
* class EventHandler { * class EventHandler {
* int _id; * int _id;
@ -468,15 +476,15 @@ public:
* EventQueue queue; * EventQueue queue;
* *
* // events are simple callbacks, call every 2 seconds * // events are simple callbacks, call every 2 seconds
* queue.call_every(2000, printf, "Calling every 2 seconds\n"); * queue.call_every(2s, printf, "Calling every 2 seconds\n");
* *
* // the dispatch method executes events * // the dispatch method executes events
* queue.dispatch(); * queue.dispatch();
* } * }
* @endcode * @endcode
*/ */
template <typename F, typename ...Args> template <typename F, typename ...ArgTs>
int call_every(int ms, F f, Args ...args); int call_every(duration ms, F f, ArgTs ...args);
/** Calls an event on the queue periodically /** Calls an event on the queue periodically
* *
@ -496,6 +504,7 @@ public:
* *
* @code * @code
* #include "mbed.h" * #include "mbed.h"
* using namespace std::chrono_literals;
* *
* class EventHandler { * class EventHandler {
* int _id; * int _id;
@ -516,7 +525,7 @@ public:
* *
* // events are simple callbacks, call object method every 2 seconds * // events are simple callbacks, call object method every 2 seconds
* // with provided parameter * // with provided parameter
* queue.call_every(2000, &handler_cb, &EventHandler::handler, 6); * queue.call_every(2s, &handler_cb, &EventHandler::handler, 6);
* *
* // the dispatch method executes events * // the dispatch method executes events
* queue.dispatch(); * queue.dispatch();
@ -525,8 +534,8 @@ public:
*/ */
// AStyle ignore, not handling correctly below // AStyle ignore, not handling correctly below
// *INDENT-OFF* // *INDENT-OFF*
template <typename T, typename R, typename ...Args> template <typename T, typename R, typename ...ArgTs>
int call_every(int ms, T *obj, R (T::*method)(Args ...args), Args ...args); int call_every(duration ms, T *obj, R (T::*method)(ArgTs ...args), ArgTs ...args);
// *INDENT-ON* // *INDENT-ON*
/** Creates an event bound to the event queue /** Creates an event bound to the event queue
@ -571,8 +580,8 @@ public:
*/ */
// AStyle ignore, not handling correctly below // AStyle ignore, not handling correctly below
// *INDENT-OFF* // *INDENT-OFF*
template <typename R, typename ...BoundArgs, typename ...ContextArgs, typename ...Args> template <typename R, typename ...BoundArgTs, typename ...ContextArgTs, typename ...ArgTs>
Event<void(Args...)> event(R (*func)(BoundArgs..., Args...), ContextArgs ...context_args); Event<void(ArgTs...)> event(R (*func)(BoundArgTs..., ArgTs...), ContextArgTs ...context_args);
// *INDENT-ON* // *INDENT-ON*
/** Creates an event bound to the event queue /** Creates an event bound to the event queue
@ -619,8 +628,8 @@ public:
*/ */
// AStyle ignore, not handling correctly below // AStyle ignore, not handling correctly below
// *INDENT-OFF* // *INDENT-OFF*
template <typename T, typename R, typename ...BoundArgs, typename ...ContextArgs, typename ...Args> template <typename T, typename R, typename ...BoundArgTs, typename ...ContextArgTs, typename ...ArgTs>
Event<void(Args...)> event(T *obj, R (T::*method)(BoundArgs..., Args...), ContextArgs ...context_args); Event<void(ArgTs...)> event(T *obj, R (T::*method)(BoundArgTs..., ArgTs...), ContextArgTs ...context_args);
// *INDENT-ON* // *INDENT-ON*
/** Creates an event bound to the event queue /** Creates an event bound to the event queue
@ -658,8 +667,8 @@ public:
* } * }
* @endcode * @endcode
*/ */
template <typename R, typename ...BoundArgs, typename ...ContextArgs, typename ...Args> template <typename R, typename ...BoundArgTs, typename ...ContextArgTs, typename ...ArgTs>
Event<void(Args...)> event(mbed::Callback<R(BoundArgs..., Args...)> cb, ContextArgs ...context_args); Event<void(ArgTs...)> event(mbed::Callback<R(BoundArgTs..., ArgTs...)> cb, ContextArgTs ...context_args);
/** Creates an user allocated event bound to the event queue /** Creates an user allocated event bound to the event queue
* *
@ -749,7 +758,7 @@ public:
return 0; return 0;
} }
F *e = new (p) F(f); F *e = new (p) F(std::move(f));
equeue_event_dtor(e, &EventQueue::function_dtor<F>); equeue_event_dtor(e, &EventQueue::function_dtor<F>);
return equeue_post(&_equeue, &EventQueue::function_call<F>, e); return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
} }
@ -763,7 +772,7 @@ public:
template <typename F, typename... ArgTs> template <typename F, typename... ArgTs>
int call(F f, ArgTs... args) int call(F f, ArgTs... args)
{ {
return call(context<F, ArgTs...>(f, args...)); return call(context<F, ArgTs...>(std::move(f), args...));
} }
/** Calls an event on the queue /** Calls an event on the queue
@ -817,15 +826,15 @@ public:
* enough memory to allocate the event. * enough memory to allocate the event.
*/ */
template <typename F> template <typename F>
int call_in(int ms, F f) int call_in(duration ms, F f)
{ {
void *p = equeue_alloc(&_equeue, sizeof(F)); void *p = equeue_alloc(&_equeue, sizeof(F));
if (!p) { if (!p) {
return 0; return 0;
} }
F *e = new (p) F(f); F *e = new (p) F(std::move(f));
equeue_event_delay(e, ms); equeue_event_delay(e, ms.count());
equeue_event_dtor(e, &EventQueue::function_dtor<F>); equeue_event_dtor(e, &EventQueue::function_dtor<F>);
return equeue_post(&_equeue, &EventQueue::function_call<F>, e); return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
} }
@ -837,45 +846,119 @@ public:
* @param args Arguments to pass to the callback * @param args Arguments to pass to the callback
*/ */
template <typename F, typename... ArgTs> template <typename F, typename... ArgTs>
int call_in(duration ms, F f, ArgTs... args)
{
return call_in(ms, context<F, ArgTs...>(std::move(f), args...));
}
/** Calls an event on the queue after a specified delay
* @see EventQueue::call_in
*/
template <typename T, typename R, typename... ArgTs>
int call_in(duration ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
{
return call_in(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue after a specified delay
* @see EventQueue::call_in
*/
template <typename T, typename R, typename... ArgTs>
int call_in(duration ms, const T *obj, R(T::*method)(ArgTs...) const, ArgTs... args)
{
return call_in(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue after a specified delay
* @see EventQueue::call_in
*/
template <typename T, typename R, typename... ArgTs>
int call_in(duration ms, volatile T *obj, R(T::*method)(ArgTs...) volatile, ArgTs... args)
{
return call_in(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue after a specified delay
* @see EventQueue::call_in
*/
template <typename T, typename R, typename... ArgTs>
int call_in(duration ms, const volatile T *obj, R(T::*method)(ArgTs...) const volatile, ArgTs... args)
{
return call_in(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue after a specified delay
*
* The specified callback will be executed in the context of the event
* queue's dispatch loop.
*
* The call_in function is IRQ safe and can act as a mechanism for moving
* events out of IRQ contexts.
*
* @param ms Time to delay in milliseconds
* @param f Function to execute in the context of the dispatch loop
* @return A unique id that represents the posted event and can
* be passed to cancel, or an id of 0 if there is not
* enough memory to allocate the event.
*/
template <typename F>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_in(int ms, F f)
{
return call_in(duration(ms), std::move(f));
}
/** Calls an event on the queue after a specified delay
* @see EventQueue::call_in
* @param ms Time to delay in milliseconds
* @param f Function to execute in the context of the dispatch loop
* @param args Arguments to pass to the callback
*/
template <typename F, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_in(int ms, F f, ArgTs... args) int call_in(int ms, F f, ArgTs... args)
{ {
return call_in(ms, context<F, ArgTs...>(f, args...)); return call_in(duration(ms), std::move(f), args...);
} }
/** Calls an event on the queue after a specified delay /** Calls an event on the queue after a specified delay
* @see EventQueue::call_in * @see EventQueue::call_in
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_in(int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args) int call_in(int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
{ {
return call_in(ms, mbed::callback(obj, method), args...); return call_in(duration(ms), obj, method, args...);
} }
/** Calls an event on the queue after a specified delay /** Calls an event on the queue after a specified delay
* @see EventQueue::call_in * @see EventQueue::call_in
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_in(int ms, const T *obj, R(T::*method)(ArgTs...) const, ArgTs... args) int call_in(int ms, const T *obj, R(T::*method)(ArgTs...) const, ArgTs... args)
{ {
return call_in(ms, mbed::callback(obj, method), args...); return call_in(duration(ms), obj, method, args...);
} }
/** Calls an event on the queue after a specified delay /** Calls an event on the queue after a specified delay
* @see EventQueue::call_in * @see EventQueue::call_in
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_in(int ms, volatile T *obj, R(T::*method)(ArgTs...) volatile, ArgTs... args) int call_in(int ms, volatile T *obj, R(T::*method)(ArgTs...) volatile, ArgTs... args)
{ {
return call_in(ms, mbed::callback(obj, method), args...); return call_in(duration(ms), obj, method, args...);
} }
/** Calls an event on the queue after a specified delay /** Calls an event on the queue after a specified delay
* @see EventQueue::call_in * @see EventQueue::call_in
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_in(int ms, const volatile T *obj, R(T::*method)(ArgTs...) const volatile, ArgTs... args) int call_in(int ms, const volatile T *obj, R(T::*method)(ArgTs...) const volatile, ArgTs... args)
{ {
return call_in(ms, mbed::callback(obj, method), args...); return call_in(duration(ms), obj, method, args...);
} }
/** Calls an event on the queue periodically /** Calls an event on the queue periodically
@ -896,16 +979,16 @@ public:
* enough memory to allocate the event. * enough memory to allocate the event.
*/ */
template <typename F> template <typename F>
int call_every(int ms, F f) int call_every(duration ms, F f)
{ {
void *p = equeue_alloc(&_equeue, sizeof(F)); void *p = equeue_alloc(&_equeue, sizeof(F));
if (!p) { if (!p) {
return 0; return 0;
} }
F *e = new (p) F(f); F *e = new (p) F(std::move(f));
equeue_event_delay(e, ms); equeue_event_delay(e, ms.count());
equeue_event_period(e, ms); equeue_event_period(e, ms.count());
equeue_event_dtor(e, &EventQueue::function_dtor<F>); equeue_event_dtor(e, &EventQueue::function_dtor<F>);
return equeue_post(&_equeue, &EventQueue::function_call<F>, e); return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
} }
@ -917,45 +1000,122 @@ public:
* @param ms Period of the event in milliseconds * @param ms Period of the event in milliseconds
*/ */
template <typename F, typename... ArgTs> template <typename F, typename... ArgTs>
int call_every(duration ms, F f, ArgTs... args)
{
return call_every(ms, context<F, ArgTs...>(std::move(f), args...));
}
/** Calls an event on the queue periodically
* @see EventQueue::call_every
*/
template <typename T, typename R, typename... ArgTs>
int call_every(duration ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
{
return call_every(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue periodically
* @see EventQueue::call_every
*/
template <typename T, typename R, typename... ArgTs>
int call_every(duration ms, const T *obj, R(T::*method)(ArgTs...) const, ArgTs... args)
{
return call_every(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue periodically
* @see EventQueue::call_every
*/
template <typename T, typename R, typename... ArgTs>
int call_every(duration ms, volatile T *obj, R(T::*method)(ArgTs...) volatile, ArgTs... args)
{
return call_every(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue periodically
* @see EventQueue::call_every
*/
template <typename T, typename R, typename... ArgTs>
int call_every(duration ms, const volatile T *obj, R(T::*method)(ArgTs...) const volatile, ArgTs... args)
{
return call_every(ms, mbed::callback(obj, method), args...);
}
/** Calls an event on the queue periodically
*
* @note The first call_every event occurs after the specified delay.
* To create a periodic event that fires immediately, @see Event.
*
* The specified callback will be executed in the context of the event
* queue's dispatch loop.
*
* The call_every function is IRQ safe and can act as a mechanism for
* moving events out of IRQ contexts.
*
* @param f Function to execute in the context of the dispatch loop
* @param ms Period of the event in milliseconds
* @return A unique id that represents the posted event and can
* be passed to cancel, or an id of 0 if there is not
* enough memory to allocate the event.
*/
template <typename F>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_every(int ms, F f)
{
return call_every(duration(ms), std::move(f));
}
/** Calls an event on the queue periodically
* @see EventQueue::call_every
* @param f Function to execute in the context of the dispatch loop
* @param args Arguments to pass to the callback
* @param ms Period of the event in milliseconds
*/
template <typename F, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_every(int ms, F f, ArgTs... args) int call_every(int ms, F f, ArgTs... args)
{ {
return call_every(ms, context<F, ArgTs...>(f, args...)); return call_every(duration(ms), std::move(f), args...);
} }
/** Calls an event on the queue periodically /** Calls an event on the queue periodically
* @see EventQueue::call_every * @see EventQueue::call_every
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_every(int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args) int call_every(int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
{ {
return call_every(ms, mbed::callback(obj, method), args...); return call_every(duration(ms), obj, method, args...);
} }
/** Calls an event on the queue periodically /** Calls an event on the queue periodically
* @see EventQueue::call_every * @see EventQueue::call_every
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_every(int ms, const T *obj, R(T::*method)(ArgTs...) const, ArgTs... args) int call_every(int ms, const T *obj, R(T::*method)(ArgTs...) const, ArgTs... args)
{ {
return call_every(ms, mbed::callback(obj, method), args...); return call_every(duration(ms), obj, method, args...);
} }
/** Calls an event on the queue periodically /** Calls an event on the queue periodically
* @see EventQueue::call_every * @see EventQueue::call_every
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_every(int ms, volatile T *obj, R(T::*method)(ArgTs...) volatile, ArgTs... args) int call_every(int ms, volatile T *obj, R(T::*method)(ArgTs...) volatile, ArgTs... args)
{ {
return call_every(ms, mbed::callback(obj, method), args...); return call_every(duration(ms), obj, method, args...);
} }
/** Calls an event on the queue periodically /** Calls an event on the queue periodically
* @see EventQueue::call_every * @see EventQueue::call_every
*/ */
template <typename T, typename R, typename... ArgTs> template <typename T, typename R, typename... ArgTs>
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
int call_every(int ms, const volatile T *obj, R(T::*method)(ArgTs...) const volatile, ArgTs... args) int call_every(int ms, const volatile T *obj, R(T::*method)(ArgTs...) const volatile, ArgTs... args)
{ {
return call_every(ms, mbed::callback(obj, method), args...); return call_every(duration(ms), obj, method, args...);
} }
/** Creates an event bound to the event queue /** Creates an event bound to the event queue

View File

@ -49,6 +49,9 @@ void equeue_tick_init()
unsigned equeue_tick() unsigned equeue_tick()
{ {
using unsigned_ms_t = std::chrono::duration<unsigned, std::milli>;
unsigned_ms_t d;
#if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT #if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT
// It is not safe to call get_ms_count from ISRs, both // It is not safe to call get_ms_count from ISRs, both
// because documentation says so, and because it will give // because documentation says so, and because it will give
@ -60,9 +63,9 @@ unsigned equeue_tick()
// should not be called from critical sections, for // should not be called from critical sections, for
// performance reasons, but I don't have a good // performance reasons, but I don't have a good
// current alternative! // current alternative!
return mbed::internal::os_timer->get_time() / 1000; d = std::chrono::duration_cast<unsigned_ms_t>(mbed::internal::os_timer->get_time().time_since_epoch());
} else { } else {
return rtos::Kernel::get_ms_count(); d = rtos::Kernel::Clock::now().time_since_epoch();
} }
#else #else
// And this is the legacy behaviour - if running in // And this is the legacy behaviour - if running in
@ -70,8 +73,9 @@ unsigned equeue_tick()
// documentation saying no. (Most recent CMSIS-RTOS // documentation saying no. (Most recent CMSIS-RTOS
// permits `ososKernelGetTickCount` from IRQ, and our // permits `ososKernelGetTickCount` from IRQ, and our
// `rtos::Kernel` wrapper copes too). // `rtos::Kernel` wrapper copes too).
return rtos::Kernel::get_ms_count(); d = rtos::Kernel::Clock::now().time_since_epoch();
#endif #endif
return d.count();
} }
#else #else