2018-09-28 01:50:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-10-24 17:08:21 +02:00
|
|
|
|
|
|
|
#include <kernel.h>
|
2018-09-28 01:50:00 +02:00
|
|
|
#include <spinlock.h>
|
|
|
|
#include <ksched.h>
|
2019-10-24 17:08:21 +02:00
|
|
|
#include <timeout_q.h>
|
2018-09-28 01:50:00 +02:00
|
|
|
#include <syscall_handler.h>
|
2019-10-24 17:08:21 +02:00
|
|
|
#include <drivers/timer/system_timer.h>
|
|
|
|
#include <sys_clock.h>
|
2018-09-28 01:50:00 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static uint64_t curr_tick;
|
2018-09-28 01:50:00 +02:00
|
|
|
|
|
|
|
static sys_dlist_t timeout_list = SYS_DLIST_STATIC_INIT(&timeout_list);
|
|
|
|
|
|
|
|
static struct k_spinlock timeout_lock;
|
|
|
|
|
2019-06-25 19:09:45 +02:00
|
|
|
#define MAX_WAIT (IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE) \
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
? K_TICKS_FOREVER : INT_MAX)
|
2018-09-28 01:50:00 +02:00
|
|
|
|
2021-02-25 21:33:15 +01:00
|
|
|
/* Cycles left to process in the currently-executing sys_clock_announce() */
|
2018-10-03 17:50:52 +02:00
|
|
|
static int announce_remaining;
|
2018-09-28 01:50:00 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
|
|
|
|
int z_clock_hw_cycles_per_sec = CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC;
|
2019-05-21 23:02:26 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
2021-03-13 14:16:53 +01:00
|
|
|
static inline int z_vrfy_sys_clock_hw_cycles_per_sec_runtime_get(void)
|
2019-05-21 23:02:26 +02:00
|
|
|
{
|
2021-03-13 14:16:53 +01:00
|
|
|
return z_impl_sys_clock_hw_cycles_per_sec_runtime_get();
|
2019-05-21 23:02:26 +02:00
|
|
|
}
|
2021-03-13 14:16:53 +01:00
|
|
|
#include <syscalls/sys_clock_hw_cycles_per_sec_runtime_get_mrsh.c>
|
2019-05-21 23:02:26 +02:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
|
|
|
#endif /* CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME */
|
2018-09-28 01:50:00 +02:00
|
|
|
|
|
|
|
static struct _timeout *first(void)
|
|
|
|
{
|
|
|
|
sys_dnode_t *t = sys_dlist_peek_head(&timeout_list);
|
|
|
|
|
|
|
|
return t == NULL ? NULL : CONTAINER_OF(t, struct _timeout, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct _timeout *next(struct _timeout *t)
|
|
|
|
{
|
|
|
|
sys_dnode_t *n = sys_dlist_peek_next(&timeout_list, &t->node);
|
|
|
|
|
|
|
|
return n == NULL ? NULL : CONTAINER_OF(n, struct _timeout, node);
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:29:19 +02:00
|
|
|
static void remove_timeout(struct _timeout *t)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2018-12-30 13:05:03 +01:00
|
|
|
if (next(t) != NULL) {
|
|
|
|
next(t)->dticks += t->dticks;
|
2018-12-07 00:39:28 +01:00
|
|
|
}
|
2018-12-30 13:05:03 +01:00
|
|
|
|
|
|
|
sys_dlist_remove(&t->node);
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static int32_t elapsed(void)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2021-02-25 21:33:15 +01:00
|
|
|
return announce_remaining == 0 ? sys_clock_elapsed() : 0U;
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static int32_t next_timeout(void)
|
2019-01-16 17:54:38 +01:00
|
|
|
{
|
|
|
|
struct _timeout *to = first();
|
2020-05-27 18:26:57 +02:00
|
|
|
int32_t ticks_elapsed = elapsed();
|
2020-09-16 03:06:49 +02:00
|
|
|
int32_t ret = to == NULL ? MAX_WAIT
|
2020-10-27 12:27:25 +01:00
|
|
|
: CLAMP(to->dticks - ticks_elapsed, 0, MAX_WAIT);
|
2019-01-16 17:54:38 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_TIMESLICING
|
|
|
|
if (_current_cpu->slice_ticks && _current_cpu->slice_ticks < ret) {
|
|
|
|
ret = _current_cpu->slice_ticks;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
void z_add_timeout(struct _timeout *to, _timeout_func_t fn,
|
|
|
|
k_timeout_t timeout)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2020-04-21 20:07:07 +02:00
|
|
|
if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:15:42 +01:00
|
|
|
#ifdef CONFIG_KERNEL_COHERENCE
|
kernel: Add cache coherence management framework
Zephyr SMP kernels need to be able to run on architectures with
incoherent caches. Naive implementation of synchronization on such
architectures requires extensive cache flushing (e.g. flush+invalidate
everything on every spin lock operation, flush on every unlock!) and
is a performance problem.
Instead, many of these systems will have access to separate "coherent"
(usually uncached) and "incoherent" regions of memory. Where this is
available, place all writable data sections by default into the
coherent region. An "__incoherent" attribute flag is defined for data
regions that are known to be CPU-local and which should use the cache.
By default, this is used for stack memory.
Stack memory will be incoherent by default, as by definition it is
local to its current thread. This requires special cache management
on context switch, so an arch API has been added for that.
Also, when enabled, add assertions to strategic places to ensure that
shared kernel data is indeed coherent. We check thread objects, the
_kernel struct, waitq's, timeouts and spinlocks. In practice almost
all kernel synchronization is built on top of these structures, and
any shared data structs will contain at least one of them.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2020-05-13 17:34:04 +02:00
|
|
|
__ASSERT_NO_MSG(arch_mem_coherent(to));
|
|
|
|
#endif
|
|
|
|
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
k_ticks_t ticks = timeout.ticks + 1;
|
2020-03-09 17:35:35 +01:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_TIMEOUT_64BIT) && Z_TICK_ABS(ticks) >= 0) {
|
2021-02-19 22:07:46 +01:00
|
|
|
ticks = Z_TICK_ABS(timeout.ticks) - (curr_tick + elapsed());
|
2020-03-09 17:35:35 +01:00
|
|
|
}
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
|
2019-01-02 15:29:43 +01:00
|
|
|
__ASSERT(!sys_dnode_is_linked(&to->node), "");
|
2018-09-28 01:50:00 +02:00
|
|
|
to->fn = fn;
|
2019-02-11 18:14:19 +01:00
|
|
|
ticks = MAX(1, ticks);
|
2018-09-28 01:50:00 +02:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
|
|
|
struct _timeout *t;
|
|
|
|
|
2018-10-04 00:02:50 +02:00
|
|
|
to->dticks = ticks + elapsed();
|
2018-09-28 01:50:00 +02:00
|
|
|
for (t = first(); t != NULL; t = next(t)) {
|
|
|
|
if (t->dticks > to->dticks) {
|
|
|
|
t->dticks -= to->dticks;
|
2019-01-28 18:35:27 +01:00
|
|
|
sys_dlist_insert(&t->node, &to->node);
|
2018-09-28 01:50:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
to->dticks -= t->dticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t == NULL) {
|
|
|
|
sys_dlist_append(&timeout_list, &to->node);
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:49:32 +01:00
|
|
|
if (to == first()) {
|
sched: timeout: Do not miss slice timeouts
Time slices don't have a timeout struct associated and stored in
timeout_list. Time slice timeout is direct programmed in the system
clock and tracked in _current_cpu->slice_ticks.
There is one issue where the time slice timeout can be missed because
the system clock is re-programmed to a longer timeout. To this happens,
it is only necessary that the timeout_list is empty (any timeout set)
and a new timeout longer than remaining time slice is set. This is cause
because z_add_timeout does not check for the slice ticks.
The following example spots the issue:
K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
K_THREAD_STACK_ARRAY_DEFINE(tstacks, NUM_THREAD, STACK_SIZE);
K_SEM_DEFINE(sema, 0, NUM_THREAD);
static inline void spin_for_ms(int ms)
{
uint32_t t32 = k_uptime_get_32();
while (k_uptime_get_32() - t32 < ms) {
}
}
static void thread_time_slice(void *p1, void *p2, void *p3)
{
printk("thread[%d] - Before spin\n", (int)(uintptr_t)p1);
/* Spinning for longer than slice */
spin_for_ms(SLICE_SIZE + 20);
/* The following print should not happen before another
* same priority thread starts.
*/
printk("thread[%d] - After spinning\n", (int)(uintptr_t)p1);
k_sem_give(&sema);
}
void main(void)
{
k_tid_t tid[NUM_THREAD];
struct k_thread t[NUM_THREAD];
uint32_t slice_ticks = k_ms_to_ticks_ceil32(SLICE_SIZE);
int old_prio = k_thread_priority_get(k_current_get());
/* disable timeslice */
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
for (int j = 0; j < 2; j++) {
k_sem_reset(&sema);
/* update priority for current thread */
k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(j));
/* synchronize to tick boundary */
k_usleep(1);
/* create delayed threads with equal preemptive priority */
for (int i = 0; i < NUM_THREAD; i++) {
tid[i] = k_thread_create(&t[i], tstacks[i], STACK_SIZE,
thread_time_slice, (void *)i, NULL,
NULL, K_PRIO_PREEMPT(j), 0,
K_NO_WAIT);
}
/* enable time slice (and reset the counter!) */
k_sched_time_slice_set(SLICE_SIZE, K_PRIO_PREEMPT(0));
/* Spins for while to spend this thread time but not longer */
/* than a slice. This is important */
spin_for_ms(100);
printk("before sleep\n");
/* relinquish CPU and wait for each thread to complete */
k_sleep(K_TICKS(slice_ticks * (NUM_THREAD + 1)));
for (int i = 0; i < NUM_THREAD; i++) {
k_sem_take(&sema, K_FOREVER);
}
/* test case teardown */
for (int i = 0; i < NUM_THREAD; i++) {
k_thread_abort(tid[i]);
}
/* disable time slice */
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
}
k_thread_priority_set(k_current_get(), old_prio);
}
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-12-18 09:33:29 +01:00
|
|
|
#if CONFIG_TIMESLICING
|
|
|
|
/*
|
|
|
|
* This is not ideal, since it does not
|
|
|
|
* account the time elapsed since the the
|
|
|
|
* last announcement, and slice_ticks is based
|
|
|
|
* on that. It means the that time remaining for
|
|
|
|
* the next announcement can be lesser than
|
|
|
|
* slice_ticks.
|
|
|
|
*/
|
|
|
|
int32_t next_time = next_timeout();
|
|
|
|
|
2021-02-02 22:19:25 +01:00
|
|
|
if (next_time == 0 ||
|
|
|
|
_current_cpu->slice_ticks != next_time) {
|
2021-02-25 21:33:15 +01:00
|
|
|
sys_clock_set_timeout(next_time, false);
|
sched: timeout: Do not miss slice timeouts
Time slices don't have a timeout struct associated and stored in
timeout_list. Time slice timeout is direct programmed in the system
clock and tracked in _current_cpu->slice_ticks.
There is one issue where the time slice timeout can be missed because
the system clock is re-programmed to a longer timeout. To this happens,
it is only necessary that the timeout_list is empty (any timeout set)
and a new timeout longer than remaining time slice is set. This is cause
because z_add_timeout does not check for the slice ticks.
The following example spots the issue:
K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
K_THREAD_STACK_ARRAY_DEFINE(tstacks, NUM_THREAD, STACK_SIZE);
K_SEM_DEFINE(sema, 0, NUM_THREAD);
static inline void spin_for_ms(int ms)
{
uint32_t t32 = k_uptime_get_32();
while (k_uptime_get_32() - t32 < ms) {
}
}
static void thread_time_slice(void *p1, void *p2, void *p3)
{
printk("thread[%d] - Before spin\n", (int)(uintptr_t)p1);
/* Spinning for longer than slice */
spin_for_ms(SLICE_SIZE + 20);
/* The following print should not happen before another
* same priority thread starts.
*/
printk("thread[%d] - After spinning\n", (int)(uintptr_t)p1);
k_sem_give(&sema);
}
void main(void)
{
k_tid_t tid[NUM_THREAD];
struct k_thread t[NUM_THREAD];
uint32_t slice_ticks = k_ms_to_ticks_ceil32(SLICE_SIZE);
int old_prio = k_thread_priority_get(k_current_get());
/* disable timeslice */
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
for (int j = 0; j < 2; j++) {
k_sem_reset(&sema);
/* update priority for current thread */
k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(j));
/* synchronize to tick boundary */
k_usleep(1);
/* create delayed threads with equal preemptive priority */
for (int i = 0; i < NUM_THREAD; i++) {
tid[i] = k_thread_create(&t[i], tstacks[i], STACK_SIZE,
thread_time_slice, (void *)i, NULL,
NULL, K_PRIO_PREEMPT(j), 0,
K_NO_WAIT);
}
/* enable time slice (and reset the counter!) */
k_sched_time_slice_set(SLICE_SIZE, K_PRIO_PREEMPT(0));
/* Spins for while to spend this thread time but not longer */
/* than a slice. This is important */
spin_for_ms(100);
printk("before sleep\n");
/* relinquish CPU and wait for each thread to complete */
k_sleep(K_TICKS(slice_ticks * (NUM_THREAD + 1)));
for (int i = 0; i < NUM_THREAD; i++) {
k_sem_take(&sema, K_FOREVER);
}
/* test case teardown */
for (int i = 0; i < NUM_THREAD; i++) {
k_thread_abort(tid[i]);
}
/* disable time slice */
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
}
k_thread_priority_set(k_current_get(), old_prio);
}
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-12-18 09:33:29 +01:00
|
|
|
}
|
|
|
|
#else
|
2021-02-25 21:33:15 +01:00
|
|
|
sys_clock_set_timeout(next_timeout(), false);
|
sched: timeout: Do not miss slice timeouts
Time slices don't have a timeout struct associated and stored in
timeout_list. Time slice timeout is direct programmed in the system
clock and tracked in _current_cpu->slice_ticks.
There is one issue where the time slice timeout can be missed because
the system clock is re-programmed to a longer timeout. To this happens,
it is only necessary that the timeout_list is empty (any timeout set)
and a new timeout longer than remaining time slice is set. This is cause
because z_add_timeout does not check for the slice ticks.
The following example spots the issue:
K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
K_THREAD_STACK_ARRAY_DEFINE(tstacks, NUM_THREAD, STACK_SIZE);
K_SEM_DEFINE(sema, 0, NUM_THREAD);
static inline void spin_for_ms(int ms)
{
uint32_t t32 = k_uptime_get_32();
while (k_uptime_get_32() - t32 < ms) {
}
}
static void thread_time_slice(void *p1, void *p2, void *p3)
{
printk("thread[%d] - Before spin\n", (int)(uintptr_t)p1);
/* Spinning for longer than slice */
spin_for_ms(SLICE_SIZE + 20);
/* The following print should not happen before another
* same priority thread starts.
*/
printk("thread[%d] - After spinning\n", (int)(uintptr_t)p1);
k_sem_give(&sema);
}
void main(void)
{
k_tid_t tid[NUM_THREAD];
struct k_thread t[NUM_THREAD];
uint32_t slice_ticks = k_ms_to_ticks_ceil32(SLICE_SIZE);
int old_prio = k_thread_priority_get(k_current_get());
/* disable timeslice */
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
for (int j = 0; j < 2; j++) {
k_sem_reset(&sema);
/* update priority for current thread */
k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(j));
/* synchronize to tick boundary */
k_usleep(1);
/* create delayed threads with equal preemptive priority */
for (int i = 0; i < NUM_THREAD; i++) {
tid[i] = k_thread_create(&t[i], tstacks[i], STACK_SIZE,
thread_time_slice, (void *)i, NULL,
NULL, K_PRIO_PREEMPT(j), 0,
K_NO_WAIT);
}
/* enable time slice (and reset the counter!) */
k_sched_time_slice_set(SLICE_SIZE, K_PRIO_PREEMPT(0));
/* Spins for while to spend this thread time but not longer */
/* than a slice. This is important */
spin_for_ms(100);
printk("before sleep\n");
/* relinquish CPU and wait for each thread to complete */
k_sleep(K_TICKS(slice_ticks * (NUM_THREAD + 1)));
for (int i = 0; i < NUM_THREAD; i++) {
k_sem_take(&sema, K_FOREVER);
}
/* test case teardown */
for (int i = 0; i < NUM_THREAD; i++) {
k_thread_abort(tid[i]);
}
/* disable time slice */
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
}
k_thread_priority_set(k_current_get(), old_prio);
}
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-12-18 09:33:29 +01:00
|
|
|
#endif /* CONFIG_TIMESLICING */
|
2018-11-22 11:49:32 +01:00
|
|
|
}
|
2018-11-20 17:26:34 +01:00
|
|
|
}
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
int z_abort_timeout(struct _timeout *to)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2019-01-02 15:29:43 +01:00
|
|
|
int ret = -EINVAL;
|
2018-09-28 01:50:00 +02:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
2018-12-30 13:05:03 +01:00
|
|
|
if (sys_dnode_is_linked(&to->node)) {
|
2018-10-17 17:29:19 +02:00
|
|
|
remove_timeout(to);
|
2018-09-28 01:50:00 +02:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:59:15 +01:00
|
|
|
/* must be locked */
|
2020-09-18 23:24:57 +02:00
|
|
|
static k_ticks_t timeout_rem(const struct _timeout *timeout)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2020-03-09 21:59:15 +01:00
|
|
|
k_ticks_t ticks = 0;
|
2018-09-28 01:50:00 +02:00
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
if (z_is_inactive_timeout(timeout)) {
|
2018-09-28 01:50:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:59:15 +01:00
|
|
|
for (struct _timeout *t = first(); t != NULL; t = next(t)) {
|
|
|
|
ticks += t->dticks;
|
|
|
|
if (timeout == t) {
|
|
|
|
break;
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 19:51:04 +01:00
|
|
|
return ticks - elapsed();
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 23:24:57 +02:00
|
|
|
k_ticks_t z_timeout_remaining(const struct _timeout *timeout)
|
2020-03-09 21:59:15 +01:00
|
|
|
{
|
|
|
|
k_ticks_t ticks = 0;
|
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
|
|
|
ticks = timeout_rem(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2020-09-18 23:24:57 +02:00
|
|
|
k_ticks_t z_timeout_expires(const struct _timeout *timeout)
|
2020-03-09 21:59:15 +01:00
|
|
|
{
|
|
|
|
k_ticks_t ticks = 0;
|
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
|
|
|
ticks = curr_tick + timeout_rem(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
int32_t z_get_next_timeout_expiry(void)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
int32_t ret = (int32_t) K_TICKS_FOREVER;
|
2018-09-28 01:50:00 +02:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
2019-01-16 17:54:38 +01:00
|
|
|
ret = next_timeout();
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-23 21:55:20 +02:00
|
|
|
void z_set_timeout_expiry(int32_t ticks, bool is_idle)
|
2018-11-22 11:49:32 +01:00
|
|
|
{
|
|
|
|
LOCKED(&timeout_lock) {
|
2020-07-23 21:55:20 +02:00
|
|
|
int next_to = next_timeout();
|
|
|
|
bool sooner = (next_to == K_TICKS_FOREVER)
|
2021-02-01 19:01:18 +01:00
|
|
|
|| (ticks <= next_to);
|
2020-07-23 21:55:20 +02:00
|
|
|
bool imminent = next_to <= 1;
|
2019-01-02 20:34:26 +01:00
|
|
|
|
|
|
|
/* Only set new timeouts when they are sooner than
|
|
|
|
* what we have. Also don't try to set a timeout when
|
|
|
|
* one is about to expire: drivers have internal logic
|
|
|
|
* that will bump the timeout to the "next" tick if
|
|
|
|
* it's not considered to be settable as directed.
|
2019-08-20 06:40:01 +02:00
|
|
|
* SMP can't use this optimization though: we don't
|
|
|
|
* know when context switches happen until interrupt
|
|
|
|
* exit and so can't get the timeslicing clamp folded
|
|
|
|
* in.
|
2019-01-02 20:34:26 +01:00
|
|
|
*/
|
2019-08-20 06:40:01 +02:00
|
|
|
if (!imminent && (sooner || IS_ENABLED(CONFIG_SMP))) {
|
2021-02-25 21:33:15 +01:00
|
|
|
sys_clock_set_timeout(MIN(ticks, next_to), is_idle);
|
2018-11-22 11:49:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 21:33:15 +01:00
|
|
|
void sys_clock_announce(int32_t ticks)
|
2018-12-20 18:23:31 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_TIMESLICING
|
|
|
|
z_time_slice(ticks);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&timeout_lock);
|
|
|
|
|
|
|
|
announce_remaining = ticks;
|
|
|
|
|
|
|
|
while (first() != NULL && first()->dticks <= announce_remaining) {
|
|
|
|
struct _timeout *t = first();
|
|
|
|
int dt = t->dticks;
|
|
|
|
|
|
|
|
curr_tick += dt;
|
|
|
|
announce_remaining -= dt;
|
|
|
|
t->dticks = 0;
|
|
|
|
remove_timeout(t);
|
|
|
|
|
|
|
|
k_spin_unlock(&timeout_lock, key);
|
|
|
|
t->fn(t);
|
|
|
|
key = k_spin_lock(&timeout_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first() != NULL) {
|
|
|
|
first()->dticks -= announce_remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr_tick += announce_remaining;
|
|
|
|
announce_remaining = 0;
|
|
|
|
|
2021-02-25 21:33:15 +01:00
|
|
|
sys_clock_set_timeout(next_timeout(), false);
|
2018-12-20 18:23:31 +01:00
|
|
|
|
|
|
|
k_spin_unlock(&timeout_lock, key);
|
|
|
|
}
|
|
|
|
|
2021-03-13 14:21:21 +01:00
|
|
|
int64_t sys_clock_tick_get(void)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint64_t t = 0U;
|
2018-09-28 01:50:00 +02:00
|
|
|
|
|
|
|
LOCKED(&timeout_lock) {
|
2021-02-25 21:33:15 +01:00
|
|
|
t = curr_tick + sys_clock_elapsed();
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-03-13 14:19:53 +01:00
|
|
|
uint32_t sys_clock_tick_get_32(void)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2018-10-02 20:12:08 +02:00
|
|
|
#ifdef CONFIG_TICKLESS_KERNEL
|
2021-03-13 14:21:21 +01:00
|
|
|
return (uint32_t)sys_clock_tick_get();
|
2018-10-02 20:12:08 +02:00
|
|
|
#else
|
2020-05-27 18:26:57 +02:00
|
|
|
return (uint32_t)curr_tick;
|
2018-10-02 20:12:08 +02:00
|
|
|
#endif
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
int64_t z_impl_k_uptime_ticks(void)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2021-03-13 14:21:21 +01:00
|
|
|
return sys_clock_tick_get();
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int64_t z_vrfy_k_uptime_ticks(void)
|
2018-09-28 01:50:00 +02:00
|
|
|
{
|
2020-03-10 23:26:38 +01:00
|
|
|
return z_impl_k_uptime_ticks();
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
2020-03-10 23:26:38 +01:00
|
|
|
#include <syscalls/k_uptime_ticks_mrsh.c>
|
2018-09-28 01:50:00 +02:00
|
|
|
#endif
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
|
2021-04-14 13:35:29 +02:00
|
|
|
void z_impl_k_busy_wait(uint32_t usec_to_wait)
|
|
|
|
{
|
2021-03-26 10:59:08 +01:00
|
|
|
SYS_PORT_TRACING_FUNC_ENTER(k_thread, busy_wait, usec_to_wait);
|
2021-04-14 13:35:29 +02:00
|
|
|
if (usec_to_wait == 0U) {
|
2021-03-26 10:59:08 +01:00
|
|
|
SYS_PORT_TRACING_FUNC_EXIT(k_thread, busy_wait, usec_to_wait);
|
2021-04-14 13:35:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT)
|
|
|
|
uint32_t start_cycles = k_cycle_get_32();
|
|
|
|
|
|
|
|
/* use 64-bit math to prevent overflow when multiplying */
|
|
|
|
uint32_t cycles_to_wait = (uint32_t)(
|
|
|
|
(uint64_t)usec_to_wait *
|
|
|
|
(uint64_t)sys_clock_hw_cycles_per_sec() /
|
|
|
|
(uint64_t)USEC_PER_SEC
|
|
|
|
);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
uint32_t current_cycles = k_cycle_get_32();
|
|
|
|
|
|
|
|
/* this handles the rollover on an unsigned 32-bit value */
|
|
|
|
if ((current_cycles - start_cycles) >= cycles_to_wait) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
arch_busy_wait(usec_to_wait);
|
|
|
|
#endif /* CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT */
|
2021-03-26 10:59:08 +01:00
|
|
|
SYS_PORT_TRACING_FUNC_EXIT(k_thread, busy_wait, usec_to_wait);
|
2021-04-14 13:35:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
static inline void z_vrfy_k_busy_wait(uint32_t usec_to_wait)
|
|
|
|
{
|
|
|
|
z_impl_k_busy_wait(usec_to_wait);
|
|
|
|
}
|
|
|
|
#include <syscalls/k_busy_wait_mrsh.c>
|
|
|
|
#endif /* CONFIG_USERSPACE */
|
|
|
|
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
/* Returns the uptime expiration (relative to an unlocked "now"!) of a
|
2020-03-09 17:35:35 +01:00
|
|
|
* timeout object. When used correctly, this should be called once,
|
|
|
|
* synchronously with the user passing a new timeout value. It should
|
|
|
|
* not be used iteratively to adjust a timeout.
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
*/
|
2021-03-13 14:22:38 +01:00
|
|
|
uint64_t sys_clock_timeout_end_calc(k_timeout_t timeout)
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
{
|
|
|
|
k_ticks_t dt;
|
|
|
|
|
|
|
|
if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
|
|
|
|
return UINT64_MAX;
|
|
|
|
} else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
|
2021-03-13 14:21:21 +01:00
|
|
|
return sys_clock_tick_get();
|
2021-03-19 23:36:55 +01:00
|
|
|
} else {
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
|
2021-03-19 23:36:55 +01:00
|
|
|
dt = timeout.ticks;
|
2020-03-09 17:35:35 +01:00
|
|
|
|
2021-03-19 23:36:55 +01:00
|
|
|
if (IS_ENABLED(CONFIG_TIMEOUT_64BIT) && Z_TICK_ABS(dt) >= 0) {
|
|
|
|
return Z_TICK_ABS(dt);
|
|
|
|
}
|
|
|
|
return sys_clock_tick_get() + MAX(1, dt);
|
2020-03-09 17:35:35 +01:00
|
|
|
}
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
}
|