PatchworkOS
966e257
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
thrd_join.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
#include <
time.h
>
7
8
#include "
user/common/syscalls.h
"
9
#include "
user/common/thread.h
"
10
11
int
thrd_join
(
thrd_t
thr,
int
* res)
12
{
13
_thread_t
* thread =
_thread_get
(thr.
id
);
14
if
(thread ==
NULL
)
15
{
16
return
thrd_error
;
17
}
18
19
uint64_t
expected =
_THREAD_ATTACHED
;
20
if
(!
atomic_compare_exchange_strong
(&thread->
state
, &expected,
_THREAD_JOINING
))
21
{
22
if
(expected ==
_THREAD_DETACHED
)
23
{
24
return
thrd_error
;
25
}
26
}
27
28
while
(
true
)
29
{
30
uint64_t
state =
atomic_load
(&thread->
state
);
31
if
(state ==
_THREAD_EXITED
)
32
{
33
break
;
34
}
35
36
futex
(&thread->
state
, state,
FUTEX_WAIT
,
CLOCKS_NEVER
);
37
}
38
39
if
(res !=
NULL
)
40
{
41
*res = thread->
result
;
42
}
43
44
_thread_free
(thread);
45
return
thrd_success
;
46
}
CLOCKS_NEVER
#define CLOCKS_NEVER
Definition
clock_t.h:16
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_WAIT
@ FUTEX_WAIT
Wait until the timeout expires or the futex value changes.
Definition
proc.h:193
NULL
#define NULL
Pointer error value.
Definition
NULL.h:23
_thread_get
_thread_t * _thread_get(tid_t id)
Definition
thread.c:129
_thread_free
void _thread_free(_thread_t *thread)
Definition
thread.c:120
proc.h
thread.h
_THREAD_EXITED
#define _THREAD_EXITED
Definition
thread.h:20
_THREAD_JOINING
#define _THREAD_JOINING
Definition
thread.h:19
_THREAD_DETACHED
#define _THREAD_DETACHED
Definition
thread.h:18
_THREAD_ATTACHED
#define _THREAD_ATTACHED
Definition
thread.h:17
stdatomic.h
atomic_compare_exchange_strong
#define atomic_compare_exchange_strong(object, expected, desired)
Definition
stdatomic.h:278
atomic_load
#define atomic_load(object)
Definition
stdatomic.h:288
stdbool.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
_thread_t
Definition
thread.h:23
_thread_t::result
int result
Definition
thread.h:26
_thread_t::state
atomic_uint64_t state
Definition
thread.h:24
thrd_t
Definition
threads.h:38
thrd_t::id
tid_t id
Definition
threads.h:39
syscalls.h
thrd_join
int thrd_join(thrd_t thr, int *res)
Definition
thrd_join.c:11
threads.h
thrd_success
@ thrd_success
Definition
threads.h:74
thrd_error
@ thrd_error
Definition
threads.h:76
time.h
src
libstd
user
functions
thread
thrd_join.c
Generated on Mon Dec 15 2025 21:55:53 for PatchworkOS by
1.9.8