1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2022 Intel Corporation. All rights reserved.
4  *
5  */
6 
7 /*
8  * Simple mutex implementation for SOF.
9  */
10 
11 #ifndef __XTOS_RTOS_MUTEX_H
12 #define __XTOS_RTOS_MUTEX_H
13 
14 #include <rtos/kernel.h>
15 #include <rtos/spinlock.h>
16 #include <stdint.h>
17 
18 #define K_FOREVER ((k_timeout_t) { .ticks = 0xffffffff })
19 
20 struct k_mutex {
21 	struct k_spinlock lock;
22 	k_spinlock_key_t key;
23 };
24 
k_mutex_init(struct k_mutex * mutex)25 static inline int k_mutex_init(struct k_mutex *mutex)
26 {
27 	k_spinlock_init(&mutex->lock);
28 	return 0;
29 }
30 
k_mutex_lock(struct k_mutex * mutex,k_timeout_t timeout)31 static inline int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout)
32 {
33 	mutex->key = k_spin_lock(&mutex->lock);
34 	return 0;
35 }
36 
k_mutex_unlock(struct k_mutex * mutex)37 static inline int k_mutex_unlock(struct k_mutex *mutex)
38 {
39 	k_spin_unlock(&mutex->lock, mutex->key);
40 	return 0;
41 }
42 
43 #endif
44