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 tsd_t initializer = TSD_INITIALIZER;
81 wrapper->val = initializer;
83 tsd_wrapper_set(wrapper);
84 tsd_init_finish(&tsd_init_head, &block);
89 JEMALLOC_ALWAYS_INLINE bool
91 if (pthread_key_create(&tsd_tsd, tsd_cleanup_wrapper) != 0) {
94 tsd_wrapper_set(&tsd_boot_wrapper);
99 JEMALLOC_ALWAYS_INLINE void
101 tsd_wrapper_t *wrapper;
102 wrapper = (tsd_wrapper_t *)malloc_tsd_malloc(sizeof(tsd_wrapper_t));
103 if (wrapper == NULL) {
104 malloc_write("<jemalloc>: Error allocating TSD\n");
107 tsd_boot_wrapper.initialized = false;
108 tsd_cleanup(&tsd_boot_wrapper.val);
109 wrapper->initialized = false;
110 tsd_t initializer = TSD_INITIALIZER;
111 wrapper->val = initializer;
112 tsd_wrapper_set(wrapper);
115 JEMALLOC_ALWAYS_INLINE bool
124 JEMALLOC_ALWAYS_INLINE bool
125 tsd_booted_get(void) {
129 JEMALLOC_ALWAYS_INLINE bool
130 tsd_get_allocates(void) {
135 JEMALLOC_ALWAYS_INLINE tsd_t *
137 tsd_wrapper_t *wrapper;
140 wrapper = tsd_wrapper_get(init);
141 if (tsd_get_allocates() && !init && wrapper == NULL) {
144 return &wrapper->val;
147 JEMALLOC_ALWAYS_INLINE void
148 tsd_set(tsd_t *val) {
149 tsd_wrapper_t *wrapper;
152 wrapper = tsd_wrapper_get(true);
153 if (likely(&wrapper->val != val)) {
154 wrapper->val = *(val);
156 wrapper->initialized = true;