Skip to content

Commit 5ac79ea

Browse files
RanderWangkv2019i
authored andcommitted
mutex: add mutex support in XTOS
It uses spin lock to support mutex. Suggested-by: Liam Girdwood <liam.r.girdwood@linux.intel.com> Suggested-by: Kai Vehmanen <kai.vehmanen@linux.intel.com> Signed-off-by: Rander Wang <rander.wang@intel.com>
1 parent 7ca9152 commit 5ac79ea

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

xtos/include/rtos/mutex.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
25+
static inline int k_mutex_init(struct k_mutex *mutex)
26+
{
27+
k_spinlock_init(&mutex->lock);
28+
return 0;
29+
}
30+
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+
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

0 commit comments

Comments
 (0)