From 0c9e280547c30c8b640aa4c93116d5e9d48d6fff Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Wed, 26 Jun 2019 10:33:48 -0400 Subject: [PATCH] cleanup: include/: move misc/mutex.h to sys/mutex.h move misc/mutex.h to sys/mutex.h and create a shim for backward-compatibility. No functional changes to the headers. A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES. Related to #16539 Signed-off-by: Anas Nashif --- include/misc/mutex.h | 142 +----------------- include/net/mqtt.h | 2 +- include/sys/mempool.h | 2 +- include/sys/mutex.h | 149 +++++++++++++++++++ kernel/userspace.c | 2 +- lib/os/mutex.c | 2 +- subsys/net/lib/mqtt/mqtt_os.h | 2 +- tests/kernel/mutex/sys_mutex/src/main.c | 2 +- tests/kernel/mutex/sys_mutex/src/thread_12.c | 2 +- tests/net/socket/getaddrinfo/src/main.c | 2 +- 10 files changed, 161 insertions(+), 146 deletions(-) create mode 100644 include/sys/mutex.h diff --git a/include/misc/mutex.h b/include/misc/mutex.h index f2c26c6642e..85a9223a20f 100644 --- a/include/misc/mutex.h +++ b/include/misc/mutex.h @@ -3,147 +3,13 @@ * * SPDX-License-Identifier: Apache-2.0 */ - #ifndef ZEPHYR_INCLUDE_MISC_MUTEX_H_ #define ZEPHYR_INCLUDE_MISC_MUTEX_H_ -/* - * sys_mutex behaves almost exactly like k_mutex, with the added advantage - * that a sys_mutex instance can reside in user memory. - * - * Further enhancements will support locking/unlocking uncontended sys_mutexes - * with simple atomic ops instead of syscalls, similar to Linux's - * FUTEX_LOCK_PI and FUTEX_UNLOCK_PI - */ +#ifndef CONFIG_COMPAT_INCLUDES +#warning "This header file has moved, include instead." +#endif -#ifdef CONFIG_USERSPACE -#include -#include +#include -struct sys_mutex { - /* Currently unused, but will be used to store state for fast mutexes - * that can be locked/unlocked with atomic ops if there is no - * contention - */ - atomic_t val; -}; - -#define SYS_MUTEX_DEFINE(name) \ - struct sys_mutex name - -/** - * @brief Initialize a mutex. - * - * This routine initializes a mutex object, prior to its first use. - * - * Upon completion, the mutex is available and does not have an owner. - * - * This routine is only necessary to call when userspace is disabled - * and the mutex was not created with SYS_MUTEX_DEFINE(). - * - * @param mutex Address of the mutex. - * - * @return N/A - */ -static inline void sys_mutex_init(struct sys_mutex *mutex) -{ - ARG_UNUSED(mutex); - - /* Nothing to do, kernel-side data structures are initialized at - * boot - */ -} - -__syscall int z_sys_mutex_kernel_lock(struct sys_mutex *mutex, s32_t timeout); - -__syscall int z_sys_mutex_kernel_unlock(struct sys_mutex *mutex); - -/** - * @brief Lock a mutex. - * - * This routine locks @a mutex. If the mutex is locked by another thread, - * the calling thread waits until the mutex becomes available or until - * a timeout occurs. - * - * A thread is permitted to lock a mutex it has already locked. The operation - * completes immediately and the lock count is increased by 1. - * - * @param mutex Address of the mutex, which may reside in user memory - * @param timeout Waiting period to lock the mutex (in milliseconds), - * or one of the special values K_NO_WAIT and K_FOREVER. - * - * @retval 0 Mutex locked. - * @retval -EBUSY Returned without waiting. - * @retval -EAGAIN Waiting period timed out. - * @retval -EACCESS Caller has no access to provided mutex address - * @retval -EINVAL Provided mutex not recognized by the kernel - */ -static inline int sys_mutex_lock(struct sys_mutex *mutex, s32_t timeout) -{ - /* For now, make the syscall unconditionally */ - return z_sys_mutex_kernel_lock(mutex, timeout); -} - -/** - * @brief Unlock a mutex. - * - * This routine unlocks @a mutex. The mutex must already be locked by the - * calling thread. - * - * The mutex cannot be claimed by another thread until it has been unlocked by - * the calling thread as many times as it was previously locked by that - * thread. - * - * @param mutex Address of the mutex, which may reside in user memory - * @retval -EACCESS Caller has no access to provided mutex address - * @retval -EINVAL Provided mutex not recognized by the kernel or mutex wasn't - * locked - * @retval -EPERM Caller does not own the mutex - */ -static inline int sys_mutex_unlock(struct sys_mutex *mutex) -{ - /* For now, make the syscall unconditionally */ - return z_sys_mutex_kernel_unlock(mutex); -} - -#include - -#else -#include -#include - -struct sys_mutex { - struct k_mutex kernel_mutex; -}; - -#define SYS_MUTEX_DEFINE(name) \ - struct sys_mutex name = { \ - .kernel_mutex = _K_MUTEX_INITIALIZER(name.kernel_mutex) \ - } - -static inline void sys_mutex_init(struct sys_mutex *mutex) -{ - k_mutex_init(&mutex->kernel_mutex); -} - -static inline int sys_mutex_lock(struct sys_mutex *mutex, s32_t timeout) -{ - return k_mutex_lock(&mutex->kernel_mutex, timeout); -} - -static inline int sys_mutex_unlock(struct sys_mutex *mutex) -{ - if (mutex->kernel_mutex.lock_count == 0) { - return -EINVAL; - } - - if (mutex->kernel_mutex.owner != _current) { - return -EPERM; - } - - k_mutex_unlock(&mutex->kernel_mutex); - return 0; -} - -#endif /* CONFIG_USERSPACE */ #endif /* ZEPHYR_INCLUDE_MISC_MUTEX_H_ */ diff --git a/include/net/mqtt.h b/include/net/mqtt.h index d7d0d75c9a6..883765cc391 100644 --- a/include/net/mqtt.h +++ b/include/net/mqtt.h @@ -27,7 +27,7 @@ #include #include #include -#include +#include #ifdef __cplusplus extern "C" { diff --git a/include/sys/mempool.h b/include/sys/mempool.h index 7df0b40833f..d863a3f7174 100644 --- a/include/sys/mempool.h +++ b/include/sys/mempool.h @@ -9,7 +9,7 @@ #include #include -#include +#include struct sys_mem_pool { struct sys_mem_pool_base base; diff --git a/include/sys/mutex.h b/include/sys/mutex.h new file mode 100644 index 00000000000..9d97a419903 --- /dev/null +++ b/include/sys/mutex.h @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2019 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_SYS_MUTEX_H_ +#define ZEPHYR_INCLUDE_SYS_MUTEX_H_ + +/* + * sys_mutex behaves almost exactly like k_mutex, with the added advantage + * that a sys_mutex instance can reside in user memory. + * + * Further enhancements will support locking/unlocking uncontended sys_mutexes + * with simple atomic ops instead of syscalls, similar to Linux's + * FUTEX_LOCK_PI and FUTEX_UNLOCK_PI + */ + +#ifdef CONFIG_USERSPACE +#include +#include + +struct sys_mutex { + /* Currently unused, but will be used to store state for fast mutexes + * that can be locked/unlocked with atomic ops if there is no + * contention + */ + atomic_t val; +}; + +#define SYS_MUTEX_DEFINE(name) \ + struct sys_mutex name + +/** + * @brief Initialize a mutex. + * + * This routine initializes a mutex object, prior to its first use. + * + * Upon completion, the mutex is available and does not have an owner. + * + * This routine is only necessary to call when userspace is disabled + * and the mutex was not created with SYS_MUTEX_DEFINE(). + * + * @param mutex Address of the mutex. + * + * @return N/A + */ +static inline void sys_mutex_init(struct sys_mutex *mutex) +{ + ARG_UNUSED(mutex); + + /* Nothing to do, kernel-side data structures are initialized at + * boot + */ +} + +__syscall int z_sys_mutex_kernel_lock(struct sys_mutex *mutex, s32_t timeout); + +__syscall int z_sys_mutex_kernel_unlock(struct sys_mutex *mutex); + +/** + * @brief Lock a mutex. + * + * This routine locks @a mutex. If the mutex is locked by another thread, + * the calling thread waits until the mutex becomes available or until + * a timeout occurs. + * + * A thread is permitted to lock a mutex it has already locked. The operation + * completes immediately and the lock count is increased by 1. + * + * @param mutex Address of the mutex, which may reside in user memory + * @param timeout Waiting period to lock the mutex (in milliseconds), + * or one of the special values K_NO_WAIT and K_FOREVER. + * + * @retval 0 Mutex locked. + * @retval -EBUSY Returned without waiting. + * @retval -EAGAIN Waiting period timed out. + * @retval -EACCESS Caller has no access to provided mutex address + * @retval -EINVAL Provided mutex not recognized by the kernel + */ +static inline int sys_mutex_lock(struct sys_mutex *mutex, s32_t timeout) +{ + /* For now, make the syscall unconditionally */ + return z_sys_mutex_kernel_lock(mutex, timeout); +} + +/** + * @brief Unlock a mutex. + * + * This routine unlocks @a mutex. The mutex must already be locked by the + * calling thread. + * + * The mutex cannot be claimed by another thread until it has been unlocked by + * the calling thread as many times as it was previously locked by that + * thread. + * + * @param mutex Address of the mutex, which may reside in user memory + * @retval -EACCESS Caller has no access to provided mutex address + * @retval -EINVAL Provided mutex not recognized by the kernel or mutex wasn't + * locked + * @retval -EPERM Caller does not own the mutex + */ +static inline int sys_mutex_unlock(struct sys_mutex *mutex) +{ + /* For now, make the syscall unconditionally */ + return z_sys_mutex_kernel_unlock(mutex); +} + +#include + +#else +#include +#include + +struct sys_mutex { + struct k_mutex kernel_mutex; +}; + +#define SYS_MUTEX_DEFINE(name) \ + struct sys_mutex name = { \ + .kernel_mutex = _K_MUTEX_INITIALIZER(name.kernel_mutex) \ + } + +static inline void sys_mutex_init(struct sys_mutex *mutex) +{ + k_mutex_init(&mutex->kernel_mutex); +} + +static inline int sys_mutex_lock(struct sys_mutex *mutex, s32_t timeout) +{ + return k_mutex_lock(&mutex->kernel_mutex, timeout); +} + +static inline int sys_mutex_unlock(struct sys_mutex *mutex) +{ + if (mutex->kernel_mutex.lock_count == 0) { + return -EINVAL; + } + + if (mutex->kernel_mutex.owner != _current) { + return -EPERM; + } + + k_mutex_unlock(&mutex->kernel_mutex); + return 0; +} + +#endif /* CONFIG_USERSPACE */ +#endif /* ZEPHYR_INCLUDE_SYS_MUTEX_H_ */ diff --git a/kernel/userspace.c b/kernel/userspace.c index 49f8d7c32a6..1aaf787e171 100644 --- a/kernel/userspace.c +++ b/kernel/userspace.c @@ -20,7 +20,7 @@ #include #include #include -#include +#include #ifdef Z_LIBC_PARTITION_EXISTS K_APPMEM_PARTITION_DEFINE(z_libc_partition); diff --git a/lib/os/mutex.c b/lib/os/mutex.c index 1fb0b4ff5cd..8d887a12df1 100644 --- a/lib/os/mutex.c +++ b/lib/os/mutex.c @@ -5,7 +5,7 @@ */ #include -#include +#include #include #include diff --git a/subsys/net/lib/mqtt/mqtt_os.h b/subsys/net/lib/mqtt/mqtt_os.h index 31c84f10fc0..91e576dddb6 100644 --- a/subsys/net/lib/mqtt/mqtt_os.h +++ b/subsys/net/lib/mqtt/mqtt_os.h @@ -20,7 +20,7 @@ #include #include -#include +#include #include diff --git a/tests/kernel/mutex/sys_mutex/src/main.c b/tests/kernel/mutex/sys_mutex/src/main.c index 53ff6f24aa1..db5ae377eaf 100644 --- a/tests/kernel/mutex/sys_mutex/src/main.c +++ b/tests/kernel/mutex/sys_mutex/src/main.c @@ -48,7 +48,7 @@ #include #include #include -#include +#include #define STACKSIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) diff --git a/tests/kernel/mutex/sys_mutex/src/thread_12.c b/tests/kernel/mutex/sys_mutex/src/thread_12.c index 76cac7a0173..184352acf5f 100644 --- a/tests/kernel/mutex/sys_mutex/src/thread_12.c +++ b/tests/kernel/mutex/sys_mutex/src/thread_12.c @@ -17,7 +17,7 @@ #include #include -#include +#include static int tc_rc = TC_PASS; /* test case return code */ diff --git a/tests/net/socket/getaddrinfo/src/main.c b/tests/net/socket/getaddrinfo/src/main.c index b025fb22ec0..fc12d5e1cf2 100644 --- a/tests/net/socket/getaddrinfo/src/main.c +++ b/tests/net/socket/getaddrinfo/src/main.c @@ -9,7 +9,7 @@ LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL); #include #include -#include +#include #include #include #include