Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
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 * Further enhancements will support locking/unlocking uncontended sys_mutexes
15 * with simple atomic ops instead of syscalls, similar to Linux's
16 * FUTEX_LOCK_PI and FUTEX_UNLOCK_PI
17 */
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#ifdef CONFIG_USERSPACE
24#include <sys/atomic.h>
25#include <zephyr/types.h>
26#include <sys_clock.h>
27
28struct sys_mutex {
29 /* Currently unused, but will be used to store state for fast mutexes
30 * that can be locked/unlocked with atomic ops if there is no
31 * contention
32 */
34};
35
53#define SYS_MUTEX_DEFINE(name) \
54 struct sys_mutex name
55
70static inline void sys_mutex_init(struct sys_mutex *mutex)
71{
72 ARG_UNUSED(mutex);
73
74 /* Nothing to do, kernel-side data structures are initialized at
75 * boot
76 */
77}
78
79__syscall int z_sys_mutex_kernel_lock(struct sys_mutex *mutex,
81
82__syscall int z_sys_mutex_kernel_unlock(struct sys_mutex *mutex);
83
105{
106 /* For now, make the syscall unconditionally */
107 return z_sys_mutex_kernel_lock(mutex, timeout);
108}
109
127static inline int sys_mutex_unlock(struct sys_mutex *mutex)
128{
129 /* For now, make the syscall unconditionally */
130 return z_sys_mutex_kernel_unlock(mutex);
131}
132
133#include <syscalls/mutex.h>
134
135#else
136#include <kernel.h>
137#include <kernel_structs.h>
138
139struct sys_mutex {
140 struct k_mutex kernel_mutex;
141};
142
143#define SYS_MUTEX_DEFINE(name) \
144 struct sys_mutex name = { \
145 .kernel_mutex = Z_MUTEX_INITIALIZER(name.kernel_mutex) \
146 }
147
148static inline void sys_mutex_init(struct sys_mutex *mutex)
149{
150 k_mutex_init(&mutex->kernel_mutex);
151}
152
153static inline int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
154{
155 return k_mutex_lock(&mutex->kernel_mutex, timeout);
156}
157
158static inline int sys_mutex_unlock(struct sys_mutex *mutex)
159{
160 return k_mutex_unlock(&mutex->kernel_mutex);
161}
162
163#endif /* CONFIG_USERSPACE */
164
169#ifdef __cplusplus
170}
171#endif
172
173#endif /* ZEPHYR_INCLUDE_SYS_MUTEX_H_ */
int atomic_t
Definition: atomic.h:21
ZTEST_BMEM int timeout
Definition: main.c:31
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.
static void sys_mutex_init(struct sys_mutex *mutex)
Initialize a mutex.
Definition: mutex.h:70
static int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
Lock a mutex.
Definition: mutex.h:104
static int sys_mutex_unlock(struct sys_mutex *mutex)
Unlock a mutex.
Definition: mutex.h:127
struct k_mutex mutex
Definition: kobject.c:1310
Definition: kernel.h:2680
Kernel timeout type.
Definition: sys_clock.h:65
Definition: mutex.h:28
atomic_t val
Definition: mutex.h:33