Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
mutex.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
7#ifndef ZEPHYR_INCLUDE_SYS_MUTEX_H_
8#define ZEPHYR_INCLUDE_SYS_MUTEX_H_
9
10/*
11 * sys_mutex behaves almost exactly like k_mutex, with the added advantage
12 * that a sys_mutex instance can reside in user memory.
13 *
14 * Depending on CONFIG_SYS_MUTEX_IMPL, sys_mutex is implemented either via
15 * a backing k_mutex (preserving priority inheritance) or via atomics on top
16 * of k_futex (allowing uncontended lock/unlock without syscalls on archs
17 * where reading the current thread and atomic ops are both syscall-free).
18 *
19 * Further enhancements will support priority inheritance with atomics once
20 * a corresponding futex is available.
21 */
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#ifdef CONFIG_USERSPACE
28#include <zephyr/kernel.h>
29#include <zephyr/sys/atomic.h>
30#include <zephyr/types.h>
31#include <zephyr/sys/clock.h>
32
36struct sys_mutex {
38#ifdef CONFIG_SYS_MUTEX_IMPL_K_MUTEX
39 atomic_t val;
40#elif defined(CONFIG_SYS_MUTEX_IMPL_FUTEX)
41 struct k_futex futex;
42 uint32_t counter;
43#endif /* CONFIG_SYS_MUTEX_IMPL_FUTEX */
45};
46
47#ifdef CONFIG_SYS_MUTEX_IMPL_K_MUTEX
48__syscall int z_sys_mutex_kernel_lock(struct sys_mutex *mutex, k_timeout_t timeout);
49
50__syscall int z_sys_mutex_kernel_unlock(struct sys_mutex *mutex);
51
52#include <zephyr/syscalls/mutex.h>
53#endif /* CONFIG_SYS_MUTEX_IMPL_K_MUTEX */
54
60
72#if defined(CONFIG_SYS_MUTEX_IMPL_FUTEX)
73#define SYS_MUTEX_DEFINE(name) struct sys_mutex name = {.futex = {0}, .counter = 0}
74#else
75#define SYS_MUTEX_DEFINE(name) struct sys_mutex name
76#endif
77
90void sys_mutex_init(struct sys_mutex *mutex);
91
117int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout);
118
140int sys_mutex_unlock(struct sys_mutex *mutex);
141
142#else
143#include <zephyr/kernel.h>
144
145struct sys_mutex {
146 struct k_mutex kernel_mutex;
147};
148
149#define SYS_MUTEX_DEFINE(name) \
150 struct sys_mutex name = { \
151 .kernel_mutex = Z_MUTEX_INITIALIZER(name.kernel_mutex) \
152 }
153
154static inline void sys_mutex_init(struct sys_mutex *mutex)
155{
156 k_mutex_init(&mutex->kernel_mutex);
157}
158
159static inline int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
160{
161 return k_mutex_lock(&mutex->kernel_mutex, timeout);
162}
163
164static inline int sys_mutex_unlock(struct sys_mutex *mutex)
165{
166 return k_mutex_unlock(&mutex->kernel_mutex);
167}
168
169#endif /* CONFIG_USERSPACE */
170
174
175#ifdef __cplusplus
176}
177#endif
178
179#endif /* ZEPHYR_INCLUDE_SYS_MUTEX_H_ */
Header file for the Atomic operations API.
System clock APIs.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:34
int k_mutex_unlock(struct k_mutex *mutex)
Unlock a mutex.
int k_mutex_init(struct k_mutex *mutex)
Initialize a mutex.
int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout)
Lock a mutex.
void sys_mutex_init(struct sys_mutex *mutex)
Initialize a mutex.
int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
Lock a mutex.
int sys_mutex_unlock(struct sys_mutex *mutex)
Unlock a mutex.
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
futex structure
Definition kernel.h:2638
Kernel mutex structure.
Definition kernel.h:3514
Kernel timeout type.
Definition clock.h:65
Userspace mutex.
Definition mutex.h:36