Zephyr API Documentation 4.0.0-rc2
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 * 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 <zephyr/sys/atomic.h>
25#include <zephyr/types.h>
26#include <zephyr/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
68static inline void sys_mutex_init(struct sys_mutex *mutex)
69{
70 ARG_UNUSED(mutex);
71
72 /* Nothing to do, kernel-side data structures are initialized at
73 * boot
74 */
75}
76
77__syscall int z_sys_mutex_kernel_lock(struct sys_mutex *mutex,
78 k_timeout_t timeout);
79
80__syscall int z_sys_mutex_kernel_unlock(struct sys_mutex *mutex);
81
102static inline int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
103{
104 /* For now, make the syscall unconditionally */
105 return z_sys_mutex_kernel_lock(mutex, timeout);
106}
107
125static inline int sys_mutex_unlock(struct sys_mutex *mutex)
126{
127 /* For now, make the syscall unconditionally */
128 return z_sys_mutex_kernel_unlock(mutex);
129}
130
131#include <zephyr/syscalls/mutex.h>
132
133#else
134#include <zephyr/kernel.h>
136
137struct sys_mutex {
138 struct k_mutex kernel_mutex;
139};
140
141#define SYS_MUTEX_DEFINE(name) \
142 struct sys_mutex name = { \
143 .kernel_mutex = Z_MUTEX_INITIALIZER(name.kernel_mutex) \
144 }
145
146static inline void sys_mutex_init(struct sys_mutex *mutex)
147{
148 k_mutex_init(&mutex->kernel_mutex);
149}
150
151static inline int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
152{
153 return k_mutex_lock(&mutex->kernel_mutex, timeout);
154}
155
156static inline int sys_mutex_unlock(struct sys_mutex *mutex)
157{
158 return k_mutex_unlock(&mutex->kernel_mutex);
159}
160
161#endif /* CONFIG_USERSPACE */
162
167#ifdef __cplusplus
168}
169#endif
170
171#endif /* ZEPHYR_INCLUDE_SYS_MUTEX_H_ */
long atomic_t
Definition atomic_types.h:15
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:68
static int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
Lock a mutex.
Definition mutex.h:102
static int sys_mutex_unlock(struct sys_mutex *mutex)
Unlock a mutex.
Definition mutex.h:125
Public kernel APIs.
Mutex Structure.
Definition kernel.h:2994
Kernel timeout type.
Definition sys_clock.h:65
Definition mutex.h:28
atomic_t val
Definition mutex.h:33
Variables needed for system clock.