1 #ifdef JEMALLOC_INTERNAL_TSD_GENERIC_H
2 #error This file should be included only once, by tsd.h.
4 #define JEMALLOC_INTERNAL_TSD_GENERIC_H
6 typedef struct tsd_init_block_s tsd_init_block_t;
7 struct tsd_init_block_s {
8 ql_elm(tsd_init_block_t) link;
13 /* Defined in tsd.c, to allow the mutex headers to have tsd dependencies. */
14 typedef struct tsd_init_head_s tsd_init_head_t;
21 void *tsd_init_check_recursion(tsd_init_head_t *head,
22 tsd_init_block_t *block);
23 void tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block);
25 extern pthread_key_t tsd_tsd;
26 extern tsd_init_head_t tsd_init_head;
27 extern tsd_wrapper_t tsd_boot_wrapper;
28 extern bool tsd_booted;
30 /* Initialization/cleanup. */
31 JEMALLOC_ALWAYS_INLINE void
32 tsd_cleanup_wrapper(void *arg) {
33 tsd_wrapper_t *wrapper = (tsd_wrapper_t *)arg;
35 if (wrapper->initialized) {
36 wrapper->initialized = false;
37 tsd_cleanup(&wrapper->val);
38 if (wrapper->initialized) {
39 /* Trigger another cleanup round. */
40 if (pthread_setspecific(tsd_tsd, (void *)wrapper) != 0)
42 malloc_write("<jemalloc>: Error setting TSD\n");
50 malloc_tsd_dalloc(wrapper);
53 JEMALLOC_ALWAYS_INLINE void
54 tsd_wrapper_set(tsd_wrapper_t *wrapper) {
55 if (pthread_setspecific(tsd_tsd, (void *)wrapper) != 0) {
56 malloc_write("<jemalloc>: Error setting TSD\n");
61 JEMALLOC_ALWAYS_INLINE tsd_wrapper_t *
62 tsd_wrapper_get(bool init) {
63 tsd_wrapper_t *wrapper = (tsd_wrapper_t *)pthread_getspecific(tsd_tsd);
65 if (init && unlikely(wrapper == NULL)) {
66 tsd_init_block_t block;
67 wrapper = (tsd_wrapper_t *)
68 tsd_init_check_recursion(&tsd_init_head, &block);
72 wrapper = (tsd_wrapper_t *)
73 malloc_tsd_malloc(sizeof(tsd_wrapper_t));
74 block.data = (void *)wrapper;
75 if (wrapper == NULL) {
76 malloc_write("<jemalloc>: Error allocating TSD\n");
79 wrapper->initialized = false;
80 JEMALLOC_DIAGNOSTIC_PUSH
81 JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS
82 tsd_t initializer = TSD_INITIALIZER;
83 JEMALLOC_DIAGNOSTIC_POP
84 wrapper->val = initializer;
86 tsd_wrapper_set(wrapper);
87 tsd_init_finish(&tsd_init_head, &block);
92 JEMALLOC_ALWAYS_INLINE bool
94 if (pthread_key_create(&tsd_tsd, tsd_cleanup_wrapper) != 0) {
97 tsd_wrapper_set(&tsd_boot_wrapper);
102 JEMALLOC_ALWAYS_INLINE void
104 tsd_wrapper_t *wrapper;
105 wrapper = (tsd_wrapper_t *)malloc_tsd_malloc(sizeof(tsd_wrapper_t));
106 if (wrapper == NULL) {
107 malloc_write("<jemalloc>: Error allocating TSD\n");
110 tsd_boot_wrapper.initialized = false;
111 tsd_cleanup(&tsd_boot_wrapper.val);
112 wrapper->initialized = false;
113 JEMALLOC_DIAGNOSTIC_PUSH
114 JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS
115 tsd_t initializer = TSD_INITIALIZER;
116 JEMALLOC_DIAGNOSTIC_POP
117 wrapper->val = initializer;
118 tsd_wrapper_set(wrapper);
121 JEMALLOC_ALWAYS_INLINE bool
130 JEMALLOC_ALWAYS_INLINE bool
131 tsd_booted_get(void) {
135 JEMALLOC_ALWAYS_INLINE bool
136 tsd_get_allocates(void) {
141 JEMALLOC_ALWAYS_INLINE tsd_t *
143 tsd_wrapper_t *wrapper;
146 wrapper = tsd_wrapper_get(init);
147 if (tsd_get_allocates() && !init && wrapper == NULL) {
150 return &wrapper->val;
153 JEMALLOC_ALWAYS_INLINE void
154 tsd_set(tsd_t *val) {
155 tsd_wrapper_t *wrapper;
158 wrapper = tsd_wrapper_get(true);
159 if (likely(&wrapper->val != val)) {
160 wrapper->val = *(val);
162 wrapper->initialized = true;