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_nominal:
81 tsd->state = tsd_state_purgatory;
84 case tsd_state_purgatory:
86 * The previous time this destructor was called, we set the
87 * state to tsd_state_purgatory so that other destructors
88 * wouldn't cause re-creation of the tsd. This time, do
89 * nothing, and do not request another callback.
92 case tsd_state_reincarnated:
94 * Another destructor deallocated memory after this destructor
95 * was called. Reset state to tsd_state_purgatory and request
98 tsd->state = tsd_state_purgatory;
107 malloc_tsd_boot0(void)
113 *tsd_arenas_cache_bypassp_get(tsd_fetch()) = true;
118 malloc_tsd_boot1(void)
122 *tsd_arenas_cache_bypassp_get(tsd_fetch()) = false;
127 _tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
131 #ifdef JEMALLOC_LAZY_LOCK
132 case DLL_THREAD_ATTACH:
136 case DLL_THREAD_DETACH:
137 _malloc_thread_cleanup();
147 # pragma comment(linker, "/INCLUDE:__tls_used")
149 # pragma comment(linker, "/INCLUDE:_tls_used")
151 # pragma section(".CRT$XLY",long,read)
153 JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used)
154 static BOOL (WINAPI *const tls_callback)(HINSTANCE hinstDLL,
155 DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;
158 #if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \
161 tsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block)
163 pthread_t self = pthread_self();
164 tsd_init_block_t *iter;
166 /* Check whether this thread has already inserted into the list. */
167 malloc_mutex_lock(&head->lock);
168 ql_foreach(iter, &head->blocks, link) {
169 if (iter->thread == self) {
170 malloc_mutex_unlock(&head->lock);
174 /* Insert block into list. */
175 ql_elm_new(block, link);
176 block->thread = self;
177 ql_tail_insert(&head->blocks, block, link);
178 malloc_mutex_unlock(&head->lock);
183 tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block)
186 malloc_mutex_lock(&head->lock);
187 ql_remove(&head->blocks, block, link);
188 malloc_mutex_unlock(&head->lock);