Add Chrono support to ThisThread

pull/12425/head
Kevin Bracey 2020-02-12 15:57:42 +02:00
parent fdc697ee85
commit 0223d92f7e
2 changed files with 148 additions and 32 deletions

View File

@ -24,6 +24,8 @@
#define THIS_THREAD_H
#include <stdint.h>
#include "platform/mbed_toolchain.h"
#include "rtos/Kernel.h"
#include "rtos/mbed_rtos_types.h"
namespace rtos {
@ -105,9 +107,21 @@ uint32_t flags_wait_any(uint32_t flags, bool clear = true);
@return actual thread flags before clearing, which may not satisfy the wait
@note You cannot call this function from ISR context.
@see Thread::flags_set
@deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
*/
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
uint32_t flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear = true);
/** Wait for all of the specified Thread Flags to become signaled for the current thread.
@param flags specifies the flags to wait for
@param rel_time timeout value.
@param clear whether to clear the specified flags after waiting for them. (default: true)
@return actual thread flags before clearing, which may not satisfy the wait
@note You cannot call this function from ISR context.
@see Thread::flags_set
*/
uint32_t flags_wait_all_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear = true);
/** Wait for all of the specified Thread Flags to become signaled for the current thread.
@param flags specifies the flags to wait for
@param millisec absolute timeout time, referenced to Kernel::get_ms_count()
@ -119,9 +133,26 @@ uint32_t flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear = true
wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
the wait will time out earlier than specified.
@see Thread::flags_set
@deprecated Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s`
rather than `Kernel::get_ms_count() + 5000`.
*/
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
uint32_t flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear = true);
/** Wait for all of the specified Thread Flags to become signaled for the current thread.
@param flags specifies the flags to wait for
@param abs_time absolute timeout time, referenced to Kernel::Clock
@param clear whether to clear the specified flags after waiting for them. (default: true)
@return actual thread flags before clearing, which may not satisfy the wait
@note You cannot call this function from ISR context.
@note the underlying RTOS may have a limit to the maximum wait time
due to internal 32-bit computations, but this is guaranteed to work if the
wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
the wait will time out earlier than specified.
@see Thread::flags_set
*/
uint32_t flags_wait_all_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear = true);
/** Wait for any of the specified Thread Flags to become signaled for the current thread.
@param flags specifies the flags to wait for
@param millisec timeout value.
@ -129,9 +160,21 @@ uint32_t flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear = tr
@return actual thread flags before clearing, which may not satisfy the wait
@note You cannot call this function from ISR context.
@see Thread::flags_set
@deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
*/
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
uint32_t flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear = true);
/** Wait for any of the specified Thread Flags to become signaled for the current thread.
@param flags specifies the flags to wait for
@param rel_time timeout value.
@param clear whether to clear the specified flags after waiting for them. (default: true)
@return actual thread flags before clearing, which may not satisfy the wait
@note You cannot call this function from ISR context.
@see Thread::flags_set
*/
uint32_t flags_wait_any_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear = true);
/** Wait for any of the specified Thread Flags to become signaled for the current thread.
@param flags specifies the flags to wait for
@param millisec absolute timeout time, referenced to Kernel::get_ms_count()
@ -143,16 +186,43 @@ uint32_t flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear = true
wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
the wait will time out earlier than specified.
@see Thread::flags_set
@deprecated Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s`
rather than `Kernel::get_ms_count() + 5000`.
*/
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
uint32_t flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear = true);
/** Wait for any of the specified Thread Flags to become signaled for the current thread.
@param flags specifies the flags to wait for
@param abs_time absolute timeout time, referenced to Kernel::Clock
@param clear whether to clear the specified flags after waiting for them. (default: true)
@return actual thread flags before clearing, which may not satisfy the wait
@note You cannot call this function from ISR context.
@note the underlying RTOS may have a limit to the maximum wait time
due to internal 32-bit computations, but this is guaranteed to work if the
wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
the wait will time out earlier than specified.
@see Thread::flags_set
*/
uint32_t flags_wait_any_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear = true);
/** Sleep for a specified time period in millisec:
@param millisec time delay value
@note You cannot call this function from ISR context.
@note The equivalent functionality is accessible in C via thread_sleep_for.
@deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
*/
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 sleep_for(uint32_t millisec);
/** Sleep for a specified time period:
@param rel_time time delay value
@note You cannot call this function from ISR context.
@note The equivalent functionality is accessible in C via thread_sleep_for.
*/
void sleep_for(Kernel::Clock::duration_u32 rel_time);
/** Sleep until a specified time in millisec
The specified time is according to Kernel::get_ms_count().
@param millisec absolute time in millisec
@ -160,9 +230,22 @@ void sleep_for(uint32_t millisec);
@note if millisec is equal to or lower than the current tick count, this
returns immediately.
@note The equivalent functionality is accessible in C via thread_sleep_until.
@deprecated Pass a chrono time_point, not an integer millisecond count. For example use
`Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.
*/
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
void sleep_until(uint64_t millisec);
/** Sleep until a specified time in millisec
The specified time is according to Kernel::Clock.
@param abs_time absolute time
@note You cannot call this function from ISR context.
@note if abs_time is equal to or lower than Kernel::Clock::now(), this
returns immediately.
@note The equivalent functionality is accessible in C via thread_sleep_until.
*/
void sleep_until(Kernel::Clock::time_point abs_time);
/** Pass control to next equal-priority thread that is in state READY.
(Higher-priority READY threads would prevent us from running; this
will not enable lower-priority threads to run, as we remain READY).

View File

@ -31,6 +31,12 @@
#include "platform/mbed_critical.h"
#include "platform/source/mbed_os_timer.h"
using std::milli;
using std::chrono::duration;
using rtos::Kernel::Clock;
using rtos::Kernel::wait_for_u32_max;
using rtos::Kernel::wait_for_u32_forever;
#if !MBED_CONF_RTOS_PRESENT
/* If the RTOS is not present, we call mbed_thread.cpp to do the work */
/* If the RTOS is present, mbed_thread.cpp calls us to do the work */
@ -95,16 +101,16 @@ bool non_rtos_check_flags(void *handle)
}
#endif
static uint32_t flags_wait_for(uint32_t flags, uint32_t millisec, bool clear, uint32_t options)
static uint32_t flags_wait_for(uint32_t flags, Clock::duration_u32 rel_time, bool clear, uint32_t options)
{
if (!clear) {
options |= osFlagsNoClear;
}
#if MBED_CONF_RTOS_PRESENT
flags = osThreadFlagsWait(flags, options, millisec);
flags = osThreadFlagsWait(flags, options, rel_time.count());
if (flags & osFlagsError) {
MBED_ASSERT((flags == osFlagsErrorTimeout && millisec != osWaitForever) ||
(flags == osFlagsErrorResource && millisec == 0));
MBED_ASSERT((flags == osFlagsErrorTimeout && rel_time != wait_for_u32_forever) ||
(flags == osFlagsErrorResource && rel_time == rel_time.zero()));
flags = ThisThread::flags_get();
}
#else
@ -113,7 +119,7 @@ static uint32_t flags_wait_for(uint32_t flags, uint32_t millisec, bool clear, ui
check.options = options;
check.flags_wanted = flags;
check.result = 0;
mbed::internal::do_timed_sleep_relative_or_forever(millisec, rtos::internal::non_rtos_check_flags, &check);
mbed::internal::do_timed_sleep_relative_or_forever(rel_time, rtos::internal::non_rtos_check_flags, &check);
flags = check.result;
#endif
@ -123,7 +129,7 @@ static uint32_t flags_wait_for(uint32_t flags, uint32_t millisec, bool clear, ui
static uint32_t flags_wait(uint32_t flags, bool clear, uint32_t options)
{
#if MBED_CONF_RTOS_PRESENT
return flags_wait_for(flags, osWaitForever, clear, options);
return flags_wait_for(flags, wait_for_u32_forever, clear, options);
#else
/* Avoids pulling in timer if not used */
if (!clear) {
@ -141,20 +147,20 @@ static uint32_t flags_wait(uint32_t flags, bool clear, uint32_t options)
#endif
}
static uint32_t flags_wait_until(uint32_t flags, uint64_t millisec, bool clear, uint32_t options)
static uint32_t flags_wait_until(uint32_t flags, Clock::time_point abs_time, bool clear, uint32_t options)
{
uint64_t now = Kernel::get_ms_count();
Clock::time_point now = Clock::now();
uint32_t delay;
if (now >= millisec) {
delay = 0;
} else if (millisec - now >= osWaitForever) {
Clock::duration_u32 rel_time;
if (now >= abs_time) {
rel_time = rel_time.zero();
} else if (abs_time - now > wait_for_u32_max) {
// Documentation permits early return for big offsets
delay = osWaitForever - 1;
rel_time = wait_for_u32_max;
} else {
delay = millisec - now;
rel_time = abs_time - now;
}
return flags_wait_for(flags, delay, clear, options);
return flags_wait_for(flags, rel_time, clear, options);
}
uint32_t ThisThread::flags_wait_all(uint32_t flags, bool clear)
@ -164,12 +170,22 @@ uint32_t ThisThread::flags_wait_all(uint32_t flags, bool clear)
uint32_t ThisThread::flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear)
{
return flags_wait_for(flags, millisec, clear, osFlagsWaitAll);
return flags_wait_all_for(flags, duration<uint32_t, milli>(millisec), clear);
}
uint32_t ThisThread::flags_wait_all_for(uint32_t flags, Clock::duration_u32 rel_time, bool clear)
{
return flags_wait_for(flags, rel_time, clear, osFlagsWaitAll);
}
uint32_t ThisThread::flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear)
{
return flags_wait_until(flags, millisec, clear, osFlagsWaitAll);
return flags_wait_all_until(flags, Clock::time_point(duration<uint64_t, milli>(millisec)), clear);
}
uint32_t ThisThread::flags_wait_all_until(uint32_t flags, Clock::time_point abs_time, bool clear)
{
return flags_wait_until(flags, abs_time, clear, osFlagsWaitAll);
}
uint32_t ThisThread::flags_wait_any(uint32_t flags, bool clear)
@ -179,45 +195,62 @@ uint32_t ThisThread::flags_wait_any(uint32_t flags, bool clear)
uint32_t ThisThread::flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear)
{
return flags_wait_for(flags, millisec, clear, osFlagsWaitAny);
return flags_wait_any_for(flags, duration<uint32_t, milli>(millisec), clear);
}
uint32_t ThisThread::flags_wait_any_for(uint32_t flags, Clock::duration_u32 rel_time, bool clear)
{
return flags_wait_for(flags, rel_time, clear, osFlagsWaitAll);
}
uint32_t ThisThread::flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear)
{
return flags_wait_until(flags, millisec, clear, osFlagsWaitAny);
return flags_wait_any_until(flags, Clock::time_point(duration<uint64_t, milli>(millisec)), clear);
}
uint32_t ThisThread::flags_wait_any_until(uint32_t flags, Clock::time_point abs_time, bool clear)
{
return flags_wait_until(flags, abs_time, clear, osFlagsWaitAny);
}
void ThisThread::sleep_for(uint32_t millisec)
{
ThisThread::sleep_for(duration<uint32_t, std::milli>(millisec));
}
void ThisThread::sleep_for(Clock::duration_u32 rel_time)
{
#if MBED_CONF_RTOS_PRESENT
osStatus_t status = osDelay(millisec);
osStatus_t status = osDelay(rel_time.count());
MBED_ASSERT(status == osOK);
#else
thread_sleep_for(millisec);
thread_sleep_for(rel_time.count());
#endif
}
void ThisThread::sleep_until(uint64_t millisec)
{
ThisThread::sleep_until(Clock::time_point(duration<uint64_t, milli>(millisec)));
}
void ThisThread::sleep_until(Clock::time_point abs_time)
{
#if MBED_CONF_RTOS_PRESENT
// CMSIS-RTOS 2.1.0 had 64-bit time and osDelayUntil, but that's been revoked.
// Limit ourselves to manual implementation assuming a >=32-bit osDelay.
Clock::time_point now;
// 64-bit time doesn't wrap (for half a billion years, at last)
// make the effort to loop for unlimited sleep, as it doesn't cost much
uint64_t now;
while ((now = Kernel::get_ms_count()) < millisec) {
if (millisec - now > UINT32_MAX) {
sleep_for(UINT32_MAX);
while ((now = Clock::now()) < abs_time) {
if (abs_time - now > wait_for_u32_max) {
osStatus_t status = osDelay(wait_for_u32_max.count());
MBED_ASSERT(status == osOK);
continue;
} else {
sleep_for(millisec - now);
osStatus_t status = osDelay((abs_time - now).count());
MBED_ASSERT(status == osOK);
break;
}
}
#else
thread_sleep_until(millisec);
thread_sleep_until(abs_time.time_since_epoch().count());
#endif
}