Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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 <zephyr/kernel.h>
23#include <zephyr/sys/atomic.h>
24#include <zephyr/types.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
34struct sys_sem {
35#ifdef CONFIG_USERSPACE
36 struct k_futex futex;
37 int limit;
38#else
39 struct k_sem kernel_sem;
40#endif
41};
42
62#ifdef CONFIG_USERSPACE
63#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
64 struct sys_sem _name = { \
65 .futex = { _initial_count }, \
66 .limit = _count_limit \
67 }; \
68 BUILD_ASSERT(((_count_limit) != 0) && \
69 ((_initial_count) <= (_count_limit)))
70#else
71/* Stuff this in the section with the rest of the k_sem objects, since they
72 * are identical and can be treated as a k_sem in the boot initialization code
73 */
74#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
75 STRUCT_SECTION_ITERABLE_ALTERNATE(k_sem, sys_sem, _name) = { \
76 .kernel_sem = Z_SEM_INITIALIZER(_name.kernel_sem, \
77 _initial_count, _count_limit) \
78 }; \
79 BUILD_ASSERT(((_count_limit) != 0) && \
80 ((_initial_count) <= (_count_limit)))
81#endif
82
96int sys_sem_init(struct sys_sem *sem, unsigned int initial_count,
97 unsigned int limit);
98
112int sys_sem_give(struct sys_sem *sem);
113
128int sys_sem_take(struct sys_sem *sem, k_timeout_t timeout);
129
139unsigned int sys_sem_count_get(struct sys_sem *sem);
140
145#ifdef __cplusplus
146}
147#endif
148
149#endif
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.
Public kernel APIs.
futex structure
Definition: kernel.h:2142
Kernel timeout type.
Definition: sys_clock.h:65
sys_sem structure
Definition: sem.h:34
struct k_futex futex
Definition: sem.h:36
int limit
Definition: sem.h:37