summaryrefslogtreecommitdiff
path: root/src/nonstd/__pthread_per_thread.c
blob: 9217a64abdaa5363976932a58102bd80de3f9545 (plain)
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
27
28
#if defined _POSIX_C_SOURCE && 199506L <= _POSIX_C_SOURCE

#include "pthread.h"

static pthread_key_t __per_thread_key;

static void __make_key(void)
{
	pthread_key_create(&__per_thread_key, NULL);
}

static struct per_thread *per_thread(void)
{
	static pthread_once_t key_once = PTHREAD_ONCE_INIT;
	struct per_thread *pt;
	pthread_once(&key_once, __make_key);
	if ((pt = pthread_getspecific(__per_thread_key)) == NULL) {
		pt = calloc(1, sizeof (*pt));
		pthread_setspecific(__per_thread_key, pt);
	}
	return pt;
}
#endif

/*
LINK(pthread)
POSIX(199506)
*/