2019-07-24 16:04:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/sem.h>
|
|
|
|
#include <syscall_handler.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
2019-11-16 05:54:26 +08:00
|
|
|
#define SYS_SEM_MINIMUM 0
|
|
|
|
#define SYS_SEM_CONTENDED (SYS_SEM_MINIMUM - 1)
|
2019-07-24 16:04:55 +08:00
|
|
|
|
|
|
|
static inline atomic_t bounded_dec(atomic_t *val, atomic_t minimum)
|
|
|
|
{
|
|
|
|
atomic_t old_value, new_value;
|
|
|
|
|
|
|
|
do {
|
|
|
|
old_value = atomic_get(val);
|
|
|
|
if (old_value < minimum) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_value = old_value - 1;
|
|
|
|
} while (atomic_cas(val, old_value, new_value) == 0);
|
|
|
|
|
|
|
|
return old_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline atomic_t bounded_inc(atomic_t *val, atomic_t minimum,
|
2019-11-16 05:54:26 +08:00
|
|
|
atomic_t maximum)
|
2019-07-24 16:04:55 +08:00
|
|
|
{
|
|
|
|
atomic_t old_value, new_value;
|
|
|
|
|
|
|
|
do {
|
|
|
|
old_value = atomic_get(val);
|
|
|
|
if (old_value >= maximum) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_value = old_value < minimum ?
|
2019-11-16 05:54:26 +08:00
|
|
|
minimum + 1 : old_value + 1;
|
2020-08-21 07:47:11 +08:00
|
|
|
} while (atomic_cas(val, old_value, new_value) == 0U);
|
2019-07-24 16:04:55 +08:00
|
|
|
|
|
|
|
return old_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sys_sem_init(struct sys_sem *sem, unsigned int initial_count,
|
2019-11-16 05:54:26 +08:00
|
|
|
unsigned int limit)
|
2019-07-24 16:04:55 +08:00
|
|
|
{
|
2019-11-16 05:54:26 +08:00
|
|
|
if (sem == NULL || limit == SYS_SEM_MINIMUM ||
|
2019-07-24 16:04:55 +08:00
|
|
|
initial_count > limit || limit > INT_MAX) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic_set(&sem->futex.val, initial_count);
|
|
|
|
sem->limit = limit;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sys_sem_give(struct sys_sem *sem)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
atomic_t old_value;
|
|
|
|
|
|
|
|
old_value = bounded_inc(&sem->futex.val,
|
2019-11-16 05:54:26 +08:00
|
|
|
SYS_SEM_MINIMUM, sem->limit);
|
2019-07-24 16:04:55 +08:00
|
|
|
if (old_value < 0) {
|
|
|
|
ret = k_futex_wake(&sem->futex, true);
|
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (old_value >= sem->limit) {
|
|
|
|
return -EAGAIN;
|
2021-04-29 01:56:06 +08:00
|
|
|
} else {
|
|
|
|
;
|
2019-07-24 16:04:55 +08: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 07:18:14 +08:00
|
|
|
int sys_sem_take(struct sys_sem *sem, k_timeout_t timeout)
|
2019-07-24 16:04:55 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
atomic_t old_value;
|
|
|
|
|
|
|
|
do {
|
|
|
|
old_value = bounded_dec(&sem->futex.val,
|
2019-11-16 05:54:26 +08:00
|
|
|
SYS_SEM_MINIMUM);
|
2019-07-24 16:04:55 +08:00
|
|
|
if (old_value > 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = k_futex_wait(&sem->futex,
|
2019-11-16 05:54:26 +08:00
|
|
|
SYS_SEM_CONTENDED, timeout);
|
2019-07-24 16:04:55 +08:00
|
|
|
} while (ret == 0 || ret == -EAGAIN);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int sys_sem_count_get(struct sys_sem *sem)
|
|
|
|
{
|
|
|
|
int value = atomic_get(&sem->futex.val);
|
|
|
|
|
2019-11-16 05:54:26 +08:00
|
|
|
return value > SYS_SEM_MINIMUM ? value : SYS_SEM_MINIMUM;
|
2019-07-24 16:04:55 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
int sys_sem_init(struct sys_sem *sem, unsigned int initial_count,
|
2019-11-16 05:54:26 +08:00
|
|
|
unsigned int limit)
|
2019-07-24 16:04:55 +08:00
|
|
|
{
|
|
|
|
k_sem_init(&sem->kernel_sem, initial_count, limit);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sys_sem_give(struct sys_sem *sem)
|
|
|
|
{
|
|
|
|
k_sem_give(&sem->kernel_sem);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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 07:18:14 +08:00
|
|
|
int sys_sem_take(struct sys_sem *sem, k_timeout_t timeout)
|
2019-07-24 16:04:55 +08:00
|
|
|
{
|
|
|
|
int ret_value = 0;
|
|
|
|
|
|
|
|
ret_value = k_sem_take(&sem->kernel_sem, timeout);
|
|
|
|
if (ret_value == -EAGAIN || ret_value == -EBUSY) {
|
|
|
|
ret_value = -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int sys_sem_count_get(struct sys_sem *sem)
|
|
|
|
{
|
|
|
|
return k_sem_count_get(&sem->kernel_sem);
|
|
|
|
}
|
|
|
|
#endif
|