summaryrefslogtreecommitdiff
path: root/lib/libc/thread/thrd_detach.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/thread/thrd_detach.c')
-rw-r--r--lib/libc/thread/thrd_detach.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/libc/thread/thrd_detach.c b/lib/libc/thread/thrd_detach.c
new file mode 100644
index 00000000..b315a20a
--- /dev/null
+++ b/lib/libc/thread/thrd_detach.c
@@ -0,0 +1,23 @@
+#include <__thread.h>
+#include <libc/futex.h>
+#include <stdatomic.h>
+#include <sys/mman.h>
+#include <threads.h>
+#include <unistd.h>
+
+int thrd_detach(thrd_t thr)
+{
+ struct __thread_self *tcb = (struct __thread_self *)thr;
+
+ if (tcb == NULL)
+ return thrd_error;
+
+ int state = atomic_exchange_explicit(&tcb->state, THREAD_STATE_DETACHED, memory_order_seq_cst);
+
+ if (state == THREAD_STATE_EXITED) {
+ if (tcb->map_base && tcb->map_size)
+ munmap(tcb->map_base, tcb->map_size);
+ }
+
+ return thrd_success;
+}