summaryrefslogtreecommitdiff
path: root/lib/libc/thread/thrd_detach.c
blob: 1cbe7627cf75d30a87dfd1bf948a30119d1013e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stddef.h"
#include <__thread.h>
#include <stdatomic.h>
#include <sys/mman.h>
#include <threads.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;
}