• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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;
9 	pthread_t thread;
10 	void *data;
11 };
12 
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;
15 
16 typedef struct {
17 	bool initialized;
18 	tsd_t val;
19 } tsd_wrapper_t;
20 
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);
24 
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;
29 
30 /* Initialization/cleanup. */
31 JEMALLOC_ALWAYS_INLINE void
tsd_cleanup_wrapper(void * arg)32 tsd_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 
53 JEMALLOC_ALWAYS_INLINE void
tsd_wrapper_set(tsd_wrapper_t * wrapper)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");
57 		abort();
58 	}
59 }
60 
61 JEMALLOC_ALWAYS_INLINE tsd_wrapper_t *
tsd_wrapper_get(bool init)62 tsd_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 			tsd_t initializer = TSD_INITIALIZER;
81 			wrapper->val = initializer;
82 		}
83 		tsd_wrapper_set(wrapper);
84 		tsd_init_finish(&tsd_init_head, &block);
85 	}
86 	return wrapper;
87 }
88 
89 JEMALLOC_ALWAYS_INLINE bool
tsd_boot0(void)90 tsd_boot0(void) {
91 	if (pthread_key_create(&tsd_tsd, tsd_cleanup_wrapper) != 0) {
92 		return true;
93 	}
94 	tsd_wrapper_set(&tsd_boot_wrapper);
95 	tsd_booted = true;
96 	return false;
97 }
98 
99 JEMALLOC_ALWAYS_INLINE void
tsd_boot1(void)100 tsd_boot1(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");
105 		abort();
106 	}
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);
113 }
114 
115 JEMALLOC_ALWAYS_INLINE bool
tsd_boot(void)116 tsd_boot(void) {
117 	if (tsd_boot0()) {
118 		return true;
119 	}
120 	tsd_boot1();
121 	return false;
122 }
123 
124 JEMALLOC_ALWAYS_INLINE bool
tsd_booted_get(void)125 tsd_booted_get(void) {
126 	return tsd_booted;
127 }
128 
129 JEMALLOC_ALWAYS_INLINE bool
tsd_get_allocates(void)130 tsd_get_allocates(void) {
131 	return true;
132 }
133 
134 /* Get/set. */
135 JEMALLOC_ALWAYS_INLINE tsd_t *
tsd_get(bool init)136 tsd_get(bool init) {
137 	tsd_wrapper_t *wrapper;
138 
139 	assert(tsd_booted);
140 	wrapper = tsd_wrapper_get(init);
141 	if (tsd_get_allocates() && !init && wrapper == NULL) {
142 		return NULL;
143 	}
144 	return &wrapper->val;
145 }
146 
147 JEMALLOC_ALWAYS_INLINE void
tsd_set(tsd_t * val)148 tsd_set(tsd_t *val) {
149 	tsd_wrapper_t *wrapper;
150 
151 	assert(tsd_booted);
152 	wrapper = tsd_wrapper_get(true);
153 	if (likely(&wrapper->val != val)) {
154 		wrapper->val = *(val);
155 	}
156 	wrapper->initialized = true;
157 }
158