1 #define JEMALLOC_TSD_C_
2 #include "jemalloc/internal/jemalloc_preamble.h"
3 #include "jemalloc/internal/jemalloc_internal_includes.h"
5 #include "jemalloc/internal/assert.h"
6 #include "jemalloc/internal/mutex.h"
7 #include "jemalloc/internal/rtree.h"
9 /******************************************************************************/
12 static unsigned ncleanups;
13 static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX];
15 #ifdef JEMALLOC_MALLOC_THREAD_CLEANUP
16 __thread tsd_t JEMALLOC_TLS_MODEL tsd_tls = TSD_INITIALIZER;
17 __thread bool JEMALLOC_TLS_MODEL tsd_initialized = false;
18 bool tsd_booted = false;
19 #elif (defined(JEMALLOC_TLS))
20 __thread tsd_t JEMALLOC_TLS_MODEL tsd_tls = TSD_INITIALIZER;
21 pthread_key_t tsd_tsd;
22 bool tsd_booted = false;
23 #elif (defined(_WIN32))
25 tsd_wrapper_t tsd_boot_wrapper = {false, TSD_INITIALIZER};
26 bool tsd_booted = false;
30 * This contains a mutex, but it's pretty convenient to allow the mutex code to
31 * have a dependency on tsd. So we define the struct here, and only refer to it
32 * by pointer in the header.
34 struct tsd_init_head_s {
35 ql_head(tsd_init_block_t) blocks;
39 pthread_key_t tsd_tsd;
40 tsd_init_head_t tsd_init_head = {
41 ql_head_initializer(blocks),
42 MALLOC_MUTEX_INITIALIZER
44 tsd_wrapper_t tsd_boot_wrapper = {
48 bool tsd_booted = false;
52 /******************************************************************************/
55 tsd_slow_update(tsd_t *tsd) {
56 if (tsd_nominal(tsd)) {
57 if (malloc_slow || !tsd_tcache_enabled_get(tsd) ||
58 tsd_reentrancy_level_get(tsd) > 0) {
59 tsd->state = tsd_state_nominal_slow;
61 tsd->state = tsd_state_nominal;
67 tsd_data_init(tsd_t *tsd) {
69 * We initialize the rtree context first (before the tcache), since the
70 * tcache initialization depends on it.
72 rtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));
74 return tsd_tcache_enabled_data_init(tsd);
78 assert_tsd_data_cleanup_done(tsd_t *tsd) {
79 assert(!tsd_nominal(tsd));
80 assert(*tsd_arenap_get_unsafe(tsd) == NULL);
81 assert(*tsd_iarenap_get_unsafe(tsd) == NULL);
82 assert(*tsd_arenas_tdata_bypassp_get_unsafe(tsd) == true);
83 assert(*tsd_arenas_tdatap_get_unsafe(tsd) == NULL);
84 assert(*tsd_tcache_enabledp_get_unsafe(tsd) == false);
85 assert(*tsd_prof_tdatap_get_unsafe(tsd) == NULL);
89 tsd_data_init_nocleanup(tsd_t *tsd) {
90 assert(tsd->state == tsd_state_reincarnated);
92 * During reincarnation, there is no guarantee that the cleanup function
93 * will be called (deallocation may happen after all tsd destructors).
94 * We set up tsd in a way that no cleanup is needed.
96 rtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));
97 *tsd_arenas_tdata_bypassp_get(tsd) = true;
98 *tsd_tcache_enabledp_get_unsafe(tsd) = false;
99 *tsd_reentrancy_levelp_get(tsd) = 1;
100 assert_tsd_data_cleanup_done(tsd);
106 tsd_fetch_slow(tsd_t *tsd, bool internal) {
108 /* For internal background threads use only. */
109 assert(tsd->state == tsd_state_uninitialized);
110 tsd->state = tsd_state_reincarnated;
112 tsd_data_init_nocleanup(tsd);
116 if (tsd->state == tsd_state_nominal_slow) {
117 /* On slow path but no work needed. */
118 assert(malloc_slow || !tsd_tcache_enabled_get(tsd) ||
119 tsd_reentrancy_level_get(tsd) > 0 ||
120 *tsd_arenas_tdata_bypassp_get(tsd));
121 } else if (tsd->state == tsd_state_uninitialized) {
122 tsd->state = tsd_state_nominal;
123 tsd_slow_update(tsd);
124 /* Trigger cleanup handler registration. */
127 } else if (tsd->state == tsd_state_purgatory) {
128 tsd->state = tsd_state_reincarnated;
130 tsd_data_init_nocleanup(tsd);
132 assert(tsd->state == tsd_state_reincarnated);
139 malloc_tsd_malloc(size_t size) {
140 return a0malloc(CACHELINE_CEILING(size));
144 malloc_tsd_dalloc(void *wrapper) {
148 #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
153 _malloc_thread_cleanup(void) {
154 bool pending[MALLOC_TSD_CLEANUPS_MAX], again;
157 for (i = 0; i < ncleanups; i++) {
163 for (i = 0; i < ncleanups; i++) {
165 pending[i] = cleanups[i]();
176 malloc_tsd_cleanup_register(bool (*f)(void)) {
177 assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX);
178 cleanups[ncleanups] = f;
183 tsd_do_data_cleanup(tsd_t *tsd) {
184 prof_tdata_cleanup(tsd);
187 arenas_tdata_cleanup(tsd);
189 witnesses_cleanup(tsd_witness_tsdp_get_unsafe(tsd));
193 tsd_cleanup(void *arg) {
194 tsd_t *tsd = (tsd_t *)arg;
196 switch (tsd->state) {
197 case tsd_state_uninitialized:
200 case tsd_state_reincarnated:
202 * Reincarnated means another destructor deallocated memory
203 * after the destructor was called. Cleanup isn't required but
204 * is still called for testing and completeness.
206 assert_tsd_data_cleanup_done(tsd);
208 case tsd_state_nominal:
209 case tsd_state_nominal_slow:
210 tsd_do_data_cleanup(tsd);
211 tsd->state = tsd_state_purgatory;
214 case tsd_state_purgatory:
216 * The previous time this destructor was called, we set the
217 * state to tsd_state_purgatory so that other destructors
218 * wouldn't cause re-creation of the tsd. This time, do
219 * nothing, and do not request another callback.
226 test_callback_t test_callback = *tsd_test_callbackp_get_unsafe(tsd);
227 int *data = tsd_test_datap_get_unsafe(tsd);
228 if (test_callback != NULL) {
235 malloc_tsd_boot0(void) {
243 *tsd_arenas_tdata_bypassp_get(tsd) = true;
248 malloc_tsd_boot1(void) {
250 tsd_t *tsd = tsd_fetch();
251 /* malloc_slow has been set properly. Update tsd_slow. */
252 tsd_slow_update(tsd);
253 *tsd_arenas_tdata_bypassp_get(tsd) = false;
258 _tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
260 #ifdef JEMALLOC_LAZY_LOCK
261 case DLL_THREAD_ATTACH:
265 case DLL_THREAD_DETACH:
266 _malloc_thread_cleanup();
275 * We need to be able to say "read" here (in the "pragma section"), but have
276 * hooked "read". We won't read for the rest of the file, so we can get away
285 # pragma comment(linker, "/INCLUDE:__tls_used")
286 # pragma comment(linker, "/INCLUDE:_tls_callback")
288 # pragma comment(linker, "/INCLUDE:_tls_used")
289 # pragma comment(linker, "/INCLUDE:tls_callback")
291 # pragma section(".CRT$XLY",long,read)
293 JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used)
294 BOOL (WINAPI *const tls_callback)(HINSTANCE hinstDLL,
295 DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;
298 #if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \
301 tsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block) {
302 pthread_t self = pthread_self();
303 tsd_init_block_t *iter;
305 /* Check whether this thread has already inserted into the list. */
306 malloc_mutex_lock(TSDN_NULL, &head->lock);
307 ql_foreach(iter, &head->blocks, link) {
308 if (iter->thread == self) {
309 malloc_mutex_unlock(TSDN_NULL, &head->lock);
313 /* Insert block into list. */
314 ql_elm_new(block, link);
315 block->thread = self;
316 ql_tail_insert(&head->blocks, block, link);
317 malloc_mutex_unlock(TSDN_NULL, &head->lock);
322 tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block) {
323 malloc_mutex_lock(TSDN_NULL, &head->lock);
324 ql_remove(&head->blocks, block, link);
325 malloc_mutex_unlock(TSDN_NULL, &head->lock);