1#ifdef JEMALLOC_INTERNAL_TSD_GENERIC_H
2#error This file should be included only once, by tsd.h.
3#endif
4#define JEMALLOC_INTERNAL_TSD_GENERIC_H
5
6typedef struct tsd_init_block_s tsd_init_block_t;
7struct tsd_init_block_s {
8	ql_elm(tsd_init_block_t) link;
9	pthread_t thread;
10	void *data;
11};
12
13/* Defined in tsd.c, to allow the mutex headers to have tsd dependencies. */
14typedef struct tsd_init_head_s tsd_init_head_t;
15
16typedef struct {
17	bool initialized;
18	tsd_t val;
19} tsd_wrapper_t;
20
21void *tsd_init_check_recursion(tsd_init_head_t *head,
22    tsd_init_block_t *block);
23void tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block);
24
25extern pthread_key_t tsd_tsd;
26extern tsd_init_head_t tsd_init_head;
27extern tsd_wrapper_t tsd_boot_wrapper;
28extern bool tsd_booted;
29
30/* Initialization/cleanup. */
31JEMALLOC_ALWAYS_INLINE void
32tsd_cleanup_wrapper(void *arg) {
33	tsd_wrapper_t *wrapper = (tsd_wrapper_t *)arg;
34
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)
41			{
42				malloc_write("<jemalloc>: Error setting TSD\n");
43				if (opt_abort) {
44					abort();
45				}
46			}
47			return;
48		}
49	}
50	malloc_tsd_dalloc(wrapper);
51}
52
53JEMALLOC_ALWAYS_INLINE void
54tsd_wrapper_set(tsd_wrapper_t *wrapper) {
55	if (pthread_setspecific(tsd_tsd, (void *)wrapper) != 0) {
56		malloc_write("<jemalloc>: Error setting TSD\n");
57		abort();
58	}
59}
60
61JEMALLOC_ALWAYS_INLINE tsd_wrapper_t *
62tsd_wrapper_get(bool init) {
63	tsd_wrapper_t *wrapper = (tsd_wrapper_t *)pthread_getspecific(tsd_tsd);
64
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);
69		if (wrapper) {
70			return wrapper;
71		}
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");
77			abort();
78		} else {
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;
85		}
86		tsd_wrapper_set(wrapper);
87		tsd_init_finish(&tsd_init_head, &block);
88	}
89	return wrapper;
90}
91
92JEMALLOC_ALWAYS_INLINE bool
93tsd_boot0(void) {
94	if (pthread_key_create(&tsd_tsd, tsd_cleanup_wrapper) != 0) {
95		return true;
96	}
97	tsd_wrapper_set(&tsd_boot_wrapper);
98	tsd_booted = true;
99	return false;
100}
101
102JEMALLOC_ALWAYS_INLINE void
103tsd_boot1(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");
108		abort();
109	}
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);
119}
120
121JEMALLOC_ALWAYS_INLINE bool
122tsd_boot(void) {
123	if (tsd_boot0()) {
124		return true;
125	}
126	tsd_boot1();
127	return false;
128}
129
130JEMALLOC_ALWAYS_INLINE bool
131tsd_booted_get(void) {
132	return tsd_booted;
133}
134
135JEMALLOC_ALWAYS_INLINE bool
136tsd_get_allocates(void) {
137	return true;
138}
139
140/* Get/set. */
141JEMALLOC_ALWAYS_INLINE tsd_t *
142tsd_get(bool init) {
143	tsd_wrapper_t *wrapper;
144
145	assert(tsd_booted);
146	wrapper = tsd_wrapper_get(init);
147	if (tsd_get_allocates() && !init && wrapper == NULL) {
148		return NULL;
149	}
150	return &wrapper->val;
151}
152
153JEMALLOC_ALWAYS_INLINE void
154tsd_set(tsd_t *val) {
155	tsd_wrapper_t *wrapper;
156
157	assert(tsd_booted);
158	wrapper = tsd_wrapper_get(true);
159	if (likely(&wrapper->val != val)) {
160		wrapper->val = *(val);
161	}
162	wrapper->initialized = true;
163}
164