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
|
|
|
|
2022-05-06 11:04:23 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/spinlock.h>
|
2018-09-28 01:50:00 +02:00
|
|
|
#include <ksched.h>
|
2023-08-29 21:32:46 +02:00
|
|
|
#include <timeout_q.h>
|
2023-09-27 00:46:01 +02:00
|
|
|
#include <zephyr/internal/syscall_handler.h>
|
2022-05-06 11:04:23 +02:00
|
|
|
#include <zephyr/drivers/timer/system_timer.h>
|
|
|
|
#include <zephyr/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
|
|
|
|
2023-06-28 23:17:45 +02:00
|
|
|
/* Ticks 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
|
|
|
{
|
2023-06-28 23:17:45 +02:00
|
|
|
/* While sys_clock_announce() is executing, new relative timeouts will be
|
|
|
|
* scheduled relatively to the currently firing timeout's original tick
|
|
|
|
* value (=curr_tick) rather than relative to the current
|
|
|
|
* sys_clock_elapsed().
|
|
|
|
*
|
|
|
|
* This means that timeouts being scheduled from within timeout callbacks
|
|
|
|
* will be scheduled at well-defined offsets from the currently firing
|
|
|
|
* timeout.
|
|
|
|
*
|
|
|
|
* As a side effect, the same will happen if an ISR with higher priority
|
|
|
|
* preempts a timeout callback and schedules a timeout.
|
|
|
|
*
|
|
|
|
* The distinction is implemented by looking at announce_remaining which
|
|
|
|
* will be non-zero while sys_clock_announce() is executing and zero
|
|
|
|
* otherwise.
|
|
|
|
*/
|
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();
|
2022-02-09 00:28:09 +01:00
|
|
|
int32_t ret;
|
|
|
|
|
|
|
|
if ((to == NULL) ||
|
|
|
|
((int64_t)(to->dticks - ticks_elapsed) > (int64_t)INT_MAX)) {
|
|
|
|
ret = MAX_WAIT;
|
|
|
|
} else {
|
|
|
|
ret = MAX(0, to->dticks - ticks_elapsed);
|
|
|
|
}
|
2019-01-16 17:54:38 +01:00
|
|
|
|
|
|
|
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));
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* 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
|
|
|
|
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;
|
|
|
|
|
2023-07-07 09:12:38 +02:00
|
|
|
K_SPINLOCK(&timeout_lock) {
|
2018-09-28 01:50:00 +02:00
|
|
|
struct _timeout *t;
|
|
|
|
|
2021-05-24 11:24:13 +02:00
|
|
|
if (IS_ENABLED(CONFIG_TIMEOUT_64BIT) &&
|
|
|
|
Z_TICK_ABS(timeout.ticks) >= 0) {
|
|
|
|
k_ticks_t ticks = Z_TICK_ABS(timeout.ticks) - curr_tick;
|
|
|
|
|
|
|
|
to->dticks = MAX(1, ticks);
|
|
|
|
} else {
|
|
|
|
to->dticks = timeout.ticks + 1 + 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()) {
|
2021-02-25 21:33:15 +01:00
|
|
|
sys_clock_set_timeout(next_timeout(), false);
|
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
|
|
|
|
2023-07-07 09:12:38 +02:00
|
|
|
K_SPINLOCK(&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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 17:12:42 +01:00
|
|
|
return ticks;
|
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;
|
|
|
|
|
2023-07-07 09:12:38 +02:00
|
|
|
K_SPINLOCK(&timeout_lock) {
|
2024-03-06 17:12:42 +01:00
|
|
|
if (!z_is_inactive_timeout(timeout)) {
|
|
|
|
ticks = timeout_rem(timeout) - elapsed();
|
|
|
|
}
|
2020-03-09 21:59:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-07-07 09:12:38 +02:00
|
|
|
K_SPINLOCK(&timeout_lock) {
|
2024-03-06 17:12:42 +01:00
|
|
|
ticks = curr_tick;
|
|
|
|
if (!z_is_inactive_timeout(timeout)) {
|
|
|
|
ticks += timeout_rem(timeout);
|
|
|
|
}
|
2020-03-09 21:59:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-07-07 09:12:38 +02:00
|
|
|
K_SPINLOCK(&timeout_lock) {
|
2019-01-16 17:54:38 +01:00
|
|
|
ret = next_timeout();
|
2018-09-28 01:50:00 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-02-25 21:33:15 +01:00
|
|
|
void sys_clock_announce(int32_t ticks)
|
2018-12-20 18:23:31 +01:00
|
|
|
{
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&timeout_lock);
|
|
|
|
|
2022-04-12 18:52:39 +02:00
|
|
|
/* We release the lock around the callbacks below, so on SMP
|
|
|
|
* systems someone might be already running the loop. Don't
|
|
|
|
* race (which will cause paralllel execution of "sequential"
|
|
|
|
* timeouts and confuse apps), just increment the tick count
|
|
|
|
* and return.
|
|
|
|
*/
|
2022-07-29 15:29:32 +02:00
|
|
|
if (IS_ENABLED(CONFIG_SMP) && (announce_remaining != 0)) {
|
2022-04-12 18:52:39 +02:00
|
|
|
announce_remaining += ticks;
|
|
|
|
k_spin_unlock(&timeout_lock, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:23:31 +01:00
|
|
|
announce_remaining = ticks;
|
|
|
|
|
2023-08-21 15:00:22 +02:00
|
|
|
struct _timeout *t;
|
2022-12-14 17:53:58 +01:00
|
|
|
|
|
|
|
for (t = first();
|
|
|
|
(t != NULL) && (t->dticks <= announce_remaining);
|
|
|
|
t = first()) {
|
2018-12-20 18:23:31 +01:00
|
|
|
int dt = t->dticks;
|
|
|
|
|
|
|
|
curr_tick += dt;
|
|
|
|
t->dticks = 0;
|
|
|
|
remove_timeout(t);
|
|
|
|
|
|
|
|
k_spin_unlock(&timeout_lock, key);
|
|
|
|
t->fn(t);
|
|
|
|
key = k_spin_lock(&timeout_lock);
|
2022-07-29 15:29:32 +02:00
|
|
|
announce_remaining -= dt;
|
2018-12-20 18:23:31 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 17:53:58 +01:00
|
|
|
if (t != NULL) {
|
|
|
|
t->dticks -= announce_remaining;
|
2018-12-20 18:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-03-06 23:31:35 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_TIMESLICING
|
|
|
|
z_time_slice();
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* CONFIG_TIMESLICING */
|
2018-12-20 18:23:31 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-07-07 09:12:38 +02:00
|
|
|
K_SPINLOCK(&timeout_lock) {
|
2022-08-03 22:11:32 +02:00
|
|
|
t = curr_tick + 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;
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* CONFIG_TICKLESS_KERNEL */
|
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>
|
2024-03-08 12:00:10 +01:00
|
|
|
#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
|
|
|
|
2023-07-06 19:56:01 +02:00
|
|
|
k_timepoint_t sys_timepoint_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
|
|
|
{
|
2023-07-06 19:56:01 +02:00
|
|
|
k_timepoint_t timepoint;
|
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
|
|
|
|
|
|
|
if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
|
2023-07-06 19:56:01 +02:00
|
|
|
timepoint.tick = UINT64_MAX;
|
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
|
|
|
} else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
|
2023-07-06 19:56:01 +02:00
|
|
|
timepoint.tick = 0;
|
2021-03-19 23:36:55 +01:00
|
|
|
} else {
|
2023-07-06 19:56:01 +02:00
|
|
|
k_ticks_t 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) {
|
2023-07-06 19:56:01 +02:00
|
|
|
timepoint.tick = Z_TICK_ABS(dt);
|
|
|
|
} else {
|
|
|
|
timepoint.tick = sys_clock_tick_get() + MAX(1, dt);
|
2021-03-19 23:36:55 +01:00
|
|
|
}
|
2020-03-09 17:35:35 +01:00
|
|
|
}
|
2023-07-06 19:56:01 +02:00
|
|
|
|
|
|
|
return timepoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_timeout_t sys_timepoint_timeout(k_timepoint_t timepoint)
|
|
|
|
{
|
|
|
|
uint64_t now, remaining;
|
|
|
|
|
|
|
|
if (timepoint.tick == UINT64_MAX) {
|
|
|
|
return K_FOREVER;
|
|
|
|
}
|
|
|
|
if (timepoint.tick == 0) {
|
|
|
|
return K_NO_WAIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
now = sys_clock_tick_get();
|
|
|
|
remaining = (timepoint.tick > now) ? (timepoint.tick - now) : 0;
|
|
|
|
return K_TICKS(remaining);
|
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
|
|
|
}
|
2022-12-15 19:14:43 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_ZTEST
|
|
|
|
void z_impl_sys_clock_tick_set(uint64_t tick)
|
|
|
|
{
|
|
|
|
curr_tick = tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
void z_vrfy_sys_clock_tick_set(uint64_t tick)
|
|
|
|
{
|
|
|
|
z_impl_sys_clock_tick_set(tick);
|
|
|
|
}
|
2024-03-08 12:00:10 +01:00
|
|
|
#endif /* CONFIG_ZTEST */
|