MOS Source Code
Loading...
Searching...
No Matches
mutex.c
Go to the documentation of this file.
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include <
mos/lib/sync/mutex.h
>
4
5
#ifdef __MOS_KERNEL__
6
#include <
mos/locks/futex.h
>
7
#else
8
#include <
mos/syscall/usermode.h
>
9
#define futex_wait(futex, val) syscall_futex_wait(futex, val)
10
#define futex_wake(futex, val) syscall_futex_wake(futex, val)
11
#endif
12
13
// a mutex_t holds a value of 0 or 1, meaning:
14
// mutex acquired = 1
15
// mutex released = 0
16
17
void
mutex_acquire
(
mutex_t
*m)
18
{
19
while
(1)
20
{
21
mutex_t
zero = 0;
22
// try setting the mutex to 1 (only if it's 0)
23
if
(__atomic_compare_exchange_n(m, &zero, 1,
false
, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
24
return
;
25
26
// tell the kernel that "the mutex should be 1", and wait until it is 0
27
futex_wait
(m, 1);
28
}
29
}
30
31
void
mutex_release
(
mutex_t
*m)
32
{
33
mutex_t
one = 1;
34
// try setting the mutex to 0 (only if it's 1)
35
if
(__atomic_compare_exchange_n(m, &one, 0,
false
, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
36
{
37
bool
result =
futex_wake
(m, 1);
// TODO: Handle error
38
MOS_UNUSED
(result);
39
}
40
}
futex.h
MOS_UNUSED
#define MOS_UNUSED(x)
Definition
mos_global.h:64
futex_wake
#define futex_wake(futex, val)
Definition
mutex.c:10
futex_wait
#define futex_wait(futex, val)
Definition
mutex.c:9
mutex.h
mutex_t
futex_word_t mutex_t
Definition
mutex.h:8
mutex_acquire
#define mutex_acquire(mutex)
Definition
rpc_client.c:41
mutex_release
#define mutex_release(mutex)
Definition
rpc_client.c:42
usermode.h
kernel
lib
locks
mutex.c
Generated on Sun Sep 1 2024 18:22:52 for MOS Source Code by
1.12.0