PatchworkOS
19e446b
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
thrd_exit.c
Go to the documentation of this file.
1
#include <
stdatomic.h
>
2
#include <
stdbool.h
>
3
#include <
stdio.h
>
4
#include <
stdlib.h
>
5
#include <
sys/proc.h
>
6
#include <
threads.h
>
7
8
#include "
user/common/syscalls.h
"
9
#include "
user/common/threading.h
"
10
11
void
thrd_exit
(
int
res)
12
{
13
_thread_t
* thread =
_THREAD_SELF
->
self
;
14
if
(thread ==
NULL
)
15
{
16
fprintf
(
stderr
,
"libstd: thrd_exit called from unknown thread\n"
);
17
abort
();
18
}
19
20
thread->
result
= res;
21
22
uint64_t
state =
atomic_exchange
(&thread->
state
,
_THREAD_EXITED
);
23
if
(state ==
_THREAD_DETACHED
)
24
{
25
_thread_free
(thread);
26
}
27
else
28
{
29
futex
(&thread->
state
,
FUTEX_ALL
,
FUTEX_WAKE
,
CLOCKS_NEVER
);
30
}
31
32
_syscall_thread_exit
();
33
}
CLOCKS_NEVER
#define CLOCKS_NEVER
Definition
clock_t.h:18
_THREAD_EXITED
#define _THREAD_EXITED
Definition
threading.h:32
_THREAD_SELF
#define _THREAD_SELF
Definition
threading.h:53
_THREAD_DETACHED
#define _THREAD_DETACHED
Definition
threading.h:30
_thread_free
void _thread_free(_thread_t *thread)
Definition
threading.c:121
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_ALL
#define FUTEX_ALL
Futex wake all constant.
Definition
proc.h:203
FUTEX_WAKE
@ FUTEX_WAKE
Wake up one or more threads waiting on the futex.
Definition
proc.h:193
NULL
#define NULL
Pointer error value.
Definition
NULL.h:25
proc.h
stdatomic.h
atomic_exchange
#define atomic_exchange(object, desired)
Definition
stdatomic.h:282
stdbool.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
stderr
FILE * stderr
Definition
std_streams.c:19
fprintf
_PUBLIC int fprintf(FILE *_RESTRICT stream, const char *_RESTRICT format,...)
Definition
fprintf.c:3
stdlib.h
abort
_PUBLIC _NORETURN void abort(void)
Definition
abort.c:9
_thread_t
Definition
threading.h:35
_thread_t::result
int result
Definition
threading.h:39
_thread_t::state
atomic_uint64_t state
Definition
threading.h:37
_thread_t::self
_thread_t * self
Definition
threading.h:36
syscalls.h
_syscall_thread_exit
static _NORETURN void _syscall_thread_exit(void)
Definition
syscalls.h:90
thrd_exit
void thrd_exit(int res)
Definition
thrd_exit.c:11
threading.h
threads.h
src
libstd
user
functions
thread
thrd_exit.c
Generated on Sat Jan 24 2026 10:59:25 for PatchworkOS by
1.9.8