about summary refs log tree commit diff
path: root/src/thread/pthread_key_create.c
blob: 8d7f81625bbddce6dbcd2f7d97081de34bc02b37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "pthread_impl.h"

const size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };

static void nodtor(void *dummy)
{
}

int pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
{
	static void (*keys[PTHREAD_KEYS_MAX])(void *);
	unsigned i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
	unsigned j = i;

	pthread_self();
	libc.tsd_keys = keys;
	if (!dtor) dtor = nodtor;
	do {
		if (!a_cas_p(keys+j, 0, dtor)) {
			*k = j;
			return 0;
		}
	} while ((j=(j+1)%PTHREAD_KEYS_MAX) != i);
	return EAGAIN;
}