PatchworkOS
Loading...
Searching...
No Matches
mtx_unlock.c
Go to the documentation of this file.
1
#include <
stdatomic.h
>
2
#include <
stdbool.h
>
3
#include <
stdio.h
>
4
#include <
sys/proc.h
>
5
#include <
threads.h
>
6
7
#include "
user/common/syscalls.h
"
8
#include "
user/common/thread.h
"
9
10
int
mtx_unlock
(
mtx_t
*
mutex
)
11
{
12
tid_t
self =
gettid
();
13
if
(
mutex
->
owner
!= self)
14
{
15
return
thrd_error
;
16
}
17
18
mutex
->
depth
--;
19
if
(
mutex
->
depth
> 0)
20
{
21
return
thrd_success
;
22
}
23
mutex
->
owner
=
ERR
;
24
25
if
(
atomic_exchange
(&(
mutex
->
state
),
_MTX_UNLOCKED
) ==
_MTX_CONTESTED
)
26
{
27
futex
(&(
mutex
->
state
), 1,
FUTEX_WAKE
,
CLOCKS_NEVER
);
28
}
29
return
thrd_success
;
30
}
CLOCKS_NEVER
#define CLOCKS_NEVER
Definition
clock_t.h:16
gettid
tid_t gettid(void)
System call to retrieve the current tid.
Definition
gettid.c:6
futex
uint64_t futex(atomic_uint64_t *addr, uint64_t val, futex_op_t op, clock_t timeout)
System call for fast user space mutual exclusion.
Definition
futex.c:6
FUTEX_WAKE
@ FUTEX_WAKE
The futex operation for waking up a amount of threads specified by the val argument.
Definition
proc.h:228
ERR
#define ERR
Integer error value.
Definition
ERR.h:17
tid_t
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition
tid_t.h:12
mutex
static mtx_t mutex
Definition
heap.c:35
mtx_unlock
int mtx_unlock(mtx_t *mutex)
Definition
mtx_unlock.c:10
proc.h
syscalls.h
thread.h
stdatomic.h
atomic_exchange
#define atomic_exchange(object, desired)
Definition
stdatomic.h:282
stdbool.h
stdio.h
mtx_t
Definition
threads.h:52
mtx_t::owner
tid_t owner
Definition
threads.h:54
mtx_t::depth
uint64_t depth
Definition
threads.h:55
mtx_t::state
atomic_uint64_t state
Definition
threads.h:53
threads.h
_MTX_UNLOCKED
#define _MTX_UNLOCKED
Definition
threads.h:47
_MTX_CONTESTED
#define _MTX_CONTESTED
Definition
threads.h:49
thrd_success
@ thrd_success
Definition
threads.h:74
thrd_error
@ thrd_error
Definition
threads.h:76
src
libstd
user
functions
thread
mtx_unlock.c
Generated by
1.9.8