1 #define JEMALLOC_TSD_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
4 /******************************************************************************/
7 static unsigned ncleanups;
8 static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX];
10 malloc_tsd_data(, , tsd_t, TSD_INITIALIZER)
12 /******************************************************************************/
15 malloc_tsd_malloc(size_t size)
18 return (a0malloc(CACHELINE_CEILING(size)));
22 malloc_tsd_dalloc(void *wrapper)
29 malloc_tsd_no_cleanup(void *arg)
35 #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
40 _malloc_thread_cleanup(void)
42 bool pending[MALLOC_TSD_CLEANUPS_MAX], again;
45 for (i = 0; i < ncleanups; i++)
50 for (i = 0; i < ncleanups; i++) {
52 pending[i] = cleanups[i]();
62 malloc_tsd_cleanup_register(bool (*f)(void))
65 assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX);
66 cleanups[ncleanups] = f;
71 tsd_cleanup(void *arg)
73 tsd_t *tsd = (tsd_t *)arg;
76 case tsd_state_uninitialized:
79 case tsd_state_nominal:
84 tsd->state = tsd_state_purgatory;
87 case tsd_state_purgatory:
89 * The previous time this destructor was called, we set the
90 * state to tsd_state_purgatory so that other destructors
91 * wouldn't cause re-creation of the tsd. This time, do
92 * nothing, and do not request another callback.
95 case tsd_state_reincarnated:
97 * Another destructor deallocated memory after this destructor
98 * was called. Reset state to tsd_state_purgatory and request
101 tsd->state = tsd_state_purgatory;
110 malloc_tsd_boot0(void)
118 *tsd_arenas_tdata_bypassp_get(tsd) = true;
123 malloc_tsd_boot1(void)
127 *tsd_arenas_tdata_bypassp_get(tsd_fetch()) = false;
132 _tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
136 #ifdef JEMALLOC_LAZY_LOCK
137 case DLL_THREAD_ATTACH:
141 case DLL_THREAD_DETACH:
142 _malloc_thread_cleanup();
152 # pragma comment(linker, "/INCLUDE:__tls_used")
153 # pragma comment(linker, "/INCLUDE:_tls_callback")
155 # pragma comment(linker, "/INCLUDE:_tls_used")
156 # pragma comment(linker, "/INCLUDE:tls_callback")
158 # pragma section(".CRT$XLY",long,read)
160 JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used)
161 BOOL (WINAPI *const tls_callback)(HINSTANCE hinstDLL,
162 DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;
165 #if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \
168 tsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block)
170 pthread_t self = pthread_self();
171 tsd_init_block_t *iter;
173 /* Check whether this thread has already inserted into the list. */
174 malloc_mutex_lock(NULL, &head->lock);
175 ql_foreach(iter, &head->blocks, link) {
176 if (iter->thread == self) {
177 malloc_mutex_unlock(NULL, &head->lock);
181 /* Insert block into list. */
182 ql_elm_new(block, link);
183 block->thread = self;
184 ql_tail_insert(&head->blocks, block, link);
185 malloc_mutex_unlock(NULL, &head->lock);
190 tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block)
193 malloc_mutex_lock(NULL, &head->lock);
194 ql_remove(&head->blocks, block, link);
195 malloc_mutex_unlock(NULL, &head->lock);