xref: /freebsd/contrib/jemalloc/include/jemalloc/internal/tsd_tls.h (revision c5ad81420c495d1d5de04209b0ec4fcb435c322c)
1 #ifdef JEMALLOC_INTERNAL_TSD_TLS_H
2 #error This file should be included only once, by tsd.h.
3 #endif
4 #define JEMALLOC_INTERNAL_TSD_TLS_H
5 
6 #define JEMALLOC_TSD_TYPE_ATTR(type) __thread type JEMALLOC_TLS_MODEL
7 
8 extern JEMALLOC_TSD_TYPE_ATTR(tsd_t) tsd_tls;
9 extern pthread_key_t tsd_tsd;
10 extern bool tsd_booted;
11 
12 /* Initialization/cleanup. */
13 JEMALLOC_ALWAYS_INLINE bool
tsd_boot0(void)14 tsd_boot0(void) {
15 	if (pthread_key_create(&tsd_tsd, &tsd_cleanup) != 0) {
16 		return true;
17 	}
18 	tsd_booted = true;
19 	return false;
20 }
21 
22 JEMALLOC_ALWAYS_INLINE void
tsd_boot1(void)23 tsd_boot1(void) {
24 	/* Do nothing. */
25 }
26 
27 JEMALLOC_ALWAYS_INLINE bool
tsd_boot(void)28 tsd_boot(void) {
29 	return tsd_boot0();
30 }
31 
32 JEMALLOC_ALWAYS_INLINE bool
tsd_booted_get(void)33 tsd_booted_get(void) {
34 	return tsd_booted;
35 }
36 
37 JEMALLOC_ALWAYS_INLINE bool
tsd_get_allocates(void)38 tsd_get_allocates(void) {
39 	return false;
40 }
41 
42 /* Get/set. */
43 JEMALLOC_ALWAYS_INLINE tsd_t *
tsd_get(bool init)44 tsd_get(bool init) {
45 	return &tsd_tls;
46 }
47 
48 JEMALLOC_ALWAYS_INLINE void
tsd_set(tsd_t * val)49 tsd_set(tsd_t *val) {
50 	assert(tsd_booted);
51 	if (likely(&tsd_tls != val)) {
52 		tsd_tls = (*val);
53 	}
54 	if (pthread_setspecific(tsd_tsd, (void *)(&tsd_tls)) != 0) {
55 		malloc_write("<jemalloc>: Error setting tsd.\n");
56 		if (opt_abort) {
57 			abort();
58 		}
59 	}
60 }
61