13#ifndef ZEPHYR_INCLUDE_SYS_SEM_H_
14#define ZEPHYR_INCLUDE_SYS_SEM_H_
36#ifdef CONFIG_USERSPACE
40 struct k_sem kernel_sem;
63#ifdef CONFIG_USERSPACE
64#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
65 struct sys_sem _name = { \
66 .futex = { _initial_count }, \
67 .limit = _count_limit \
69 BUILD_ASSERT(((_count_limit) != 0) && \
70 ((_initial_count) <= (_count_limit)))
75#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
76 STRUCT_SECTION_ITERABLE_ALTERNATE(k_sem, sys_sem, _name) = { \
77 .kernel_sem = Z_SEM_INITIALIZER(_name.kernel_sem, \
78 _initial_count, _count_limit) \
80 BUILD_ASSERT(((_count_limit) != 0) && \
81 ((_initial_count) <= (_count_limit)))
147static ALWAYS_INLINE void z_sys_sem_lock_onexit(__maybe_unused
int *rc)
149 __ASSERT(*rc == 1,
"SYS_SEM_LOCK exited with goto, break or return, "
150 "use SYS_SEM_LOCK_BREAK instead.");
152#define SYS_SEM_LOCK_ONEXIT __attribute__((__cleanup__(z_sys_sem_lock_onexit)))
154#define SYS_SEM_LOCK_ONEXIT
167#define SYS_SEM_LOCK_BREAK continue
207#define SYS_SEM_LOCK(sem) \
208 for (int __rc SYS_SEM_LOCK_ONEXIT = sys_sem_take((sem), K_FOREVER); ({ \
209 __ASSERT(__rc >= 0, "Failed to take sem: %d", __rc); \
213 __rc = sys_sem_give((sem)); \
214 __ASSERT(__rc == 0, "Failed to give sem: %d", __rc); \
unsigned int sys_sem_count_get(struct sys_sem *sem)
Get sys_sem's value.
int sys_sem_give(struct sys_sem *sem)
Give a semaphore.
int sys_sem_init(struct sys_sem *sem, unsigned int initial_count, unsigned int limit)
Initialize a semaphore.
int sys_sem_take(struct sys_sem *sem, k_timeout_t timeout)
Take a sys_sem.
futex structure
Definition kernel.h:2228
Kernel timeout type.
Definition sys_clock.h:65
sys_sem structure
Definition sem.h:35
struct k_futex futex
Definition sem.h:37
int limit
Definition sem.h:38