MOS Source Code
Loading...
Searching...
No Matches
mutex.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include <
mos/lib/sync/mutex.hpp
>
4
5
#ifdef __MOS_KERNEL__
6
#include <
mos/locks/futex.hpp
>
7
#else
8
#include <mos/syscall/usermode.hpp>
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.hpp
MOS_UNUSED
#define MOS_UNUSED(x)
Definition
mos_global.h:65
futex_wake
#define futex_wake(futex, val)
Definition
mutex.cpp:10
futex_wait
#define futex_wait(futex, val)
Definition
mutex.cpp:9
mutex.hpp
mutex_t
futex_word_t mutex_t
Definition
mutex.hpp:8
mutex_acquire
#define mutex_acquire(mutex)
Definition
rpc_client.cpp:42
mutex_release
#define mutex_release(mutex)
Definition
rpc_client.cpp:43
kernel
lib
locks
mutex.cpp
Generated on Tue Feb 18 2025 16:41:40 for MOS Source Code by
1.13.2