xref: /freebsd/contrib/jemalloc/include/jemalloc/internal/tsd_malloc_thread_cleanup.h (revision c5ad81420c495d1d5de04209b0ec4fcb435c322c)
1 #ifdef JEMALLOC_INTERNAL_TSD_MALLOC_THREAD_CLEANUP_H
2 #error This file should be included only once, by tsd.h.
3 #endif
4 #define JEMALLOC_INTERNAL_TSD_MALLOC_THREAD_CLEANUP_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 JEMALLOC_TSD_TYPE_ATTR(bool) tsd_initialized;
10 extern bool tsd_booted;
11 
12 /* Initialization/cleanup. */
13 JEMALLOC_ALWAYS_INLINE bool
tsd_cleanup_wrapper(void)14 tsd_cleanup_wrapper(void) {
15 	if (tsd_initialized) {
16 		tsd_initialized = false;
17 		tsd_cleanup(&tsd_tls);
18 	}
19 	return tsd_initialized;
20 }
21 
22 JEMALLOC_ALWAYS_INLINE bool
tsd_boot0(void)23 tsd_boot0(void) {
24 	malloc_tsd_cleanup_register(&tsd_cleanup_wrapper);
25 	tsd_booted = true;
26 	return false;
27 }
28 
29 JEMALLOC_ALWAYS_INLINE void
tsd_boot1(void)30 tsd_boot1(void) {
31 	/* Do nothing. */
32 }
33 
34 JEMALLOC_ALWAYS_INLINE bool
tsd_boot(void)35 tsd_boot(void) {
36 	return tsd_boot0();
37 }
38 
39 JEMALLOC_ALWAYS_INLINE bool
tsd_booted_get(void)40 tsd_booted_get(void) {
41 	return tsd_booted;
42 }
43 
44 JEMALLOC_ALWAYS_INLINE bool
tsd_get_allocates(void)45 tsd_get_allocates(void) {
46 	return false;
47 }
48 
49 /* Get/set. */
50 JEMALLOC_ALWAYS_INLINE tsd_t *
tsd_get(bool init)51 tsd_get(bool init) {
52 	return &tsd_tls;
53 }
54 JEMALLOC_ALWAYS_INLINE void
tsd_set(tsd_t * val)55 tsd_set(tsd_t *val) {
56 	assert(tsd_booted);
57 	if (likely(&tsd_tls != val)) {
58 		tsd_tls = (*val);
59 	}
60 	tsd_initialized = true;
61 }
62