Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
sem.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
13#ifndef ZEPHYR_INCLUDE_SYS_SEM_H_
14#define ZEPHYR_INCLUDE_SYS_SEM_H_
15
16/*
17 * sys_sem exists in user memory working as counter semaphore for
18 * user mode thread when user mode enabled. When user mode isn't
19 * enabled, sys_sem behaves like k_sem.
20 */
21
22#include <kernel.h>
23#include <sys/atomic.h>
24#include <zephyr/types.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
33struct sys_sem {
34#ifdef CONFIG_USERSPACE
35 struct k_futex futex;
36 int limit;
37#else
38 struct k_sem kernel_sem;
39#endif
40};
41
61#ifdef CONFIG_USERSPACE
62#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
63 struct sys_sem _name = { \
64 .futex = { _initial_count }, \
65 .limit = _count_limit \
66 }; \
67 BUILD_ASSERT(((_count_limit) != 0) && \
68 ((_initial_count) <= (_count_limit)))
69#else
70/* Stuff this in the section with the rest of the k_sem objects, since they
71 * are identical and can be treated as a k_sem in the boot initialization code
72 */
73#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
74 STRUCT_SECTION_ITERABLE_ALTERNATE(k_sem, sys_sem, _name) = { \
75 .kernel_sem = Z_SEM_INITIALIZER(_name.kernel_sem, \
76 _initial_count, _count_limit) \
77 }; \
78 BUILD_ASSERT(((_count_limit) != 0) && \
79 ((_initial_count) <= (_count_limit)))
80#endif
81
95int sys_sem_init(struct sys_sem *sem, unsigned int initial_count,
96 unsigned int limit);
97
112
128
138unsigned int sys_sem_count_get(struct sys_sem *sem);
139
144#ifdef __cplusplus
145}
146#endif
147
148#endif
ZTEST_BMEM int timeout
Definition: main.c:31
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:1949
Kernel timeout type.
Definition: sys_clock.h:65
Definition: sem.h:33
struct k_futex futex
Definition: sem.h:35
int limit
Definition: sem.h:36
static struct k_sem sem[3]
Definition: timeout_order.c:14