352 lines
8.8 KiB
C
Raw Normal View History

2017-06-15 07:15:05 +00:00
#define JEMALLOC_TSD_C_
#include "jemalloc/internal/jemalloc_preamble.h"
#include "jemalloc/internal/jemalloc_internal_includes.h"
#include "jemalloc/internal/assert.h"
#include "jemalloc/internal/mutex.h"
#include "jemalloc/internal/rtree.h"
/******************************************************************************/
/* Data. */
static unsigned ncleanups;
static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX];
2017-06-15 07:15:05 +00:00
#ifdef JEMALLOC_MALLOC_THREAD_CLEANUP
__thread tsd_t JEMALLOC_TLS_MODEL tsd_tls = TSD_INITIALIZER;
__thread bool JEMALLOC_TLS_MODEL tsd_initialized = false;
bool tsd_booted = false;
#elif (defined(JEMALLOC_TLS))
__thread tsd_t JEMALLOC_TLS_MODEL tsd_tls = TSD_INITIALIZER;
pthread_key_t tsd_tsd;
bool tsd_booted = false;
#elif (defined(_WIN32))
DWORD tsd_tsd;
tsd_wrapper_t tsd_boot_wrapper = {false, TSD_INITIALIZER};
bool tsd_booted = false;
#else
/*
* This contains a mutex, but it's pretty convenient to allow the mutex code to
* have a dependency on tsd. So we define the struct here, and only refer to it
* by pointer in the header.
*/
struct tsd_init_head_s {
ql_head(tsd_init_block_t) blocks;
malloc_mutex_t lock;
};
pthread_key_t tsd_tsd;
tsd_init_head_t tsd_init_head = {
ql_head_initializer(blocks),
MALLOC_MUTEX_INITIALIZER
};
tsd_wrapper_t tsd_boot_wrapper = {
false,
TSD_INITIALIZER
};
bool tsd_booted = false;
#endif
2015-08-18 00:21:25 +00:00
/******************************************************************************/
2017-06-15 07:15:05 +00:00
void
tsd_slow_update(tsd_t *tsd) {
if (tsd_nominal(tsd)) {
if (malloc_slow || !tsd_tcache_enabled_get(tsd) ||
tsd_reentrancy_level_get(tsd) > 0) {
tsd->state = tsd_state_nominal_slow;
} else {
tsd->state = tsd_state_nominal;
}
}
}
2017-06-15 07:15:05 +00:00
static bool
tsd_data_init(tsd_t *tsd) {
/*
* We initialize the rtree context first (before the tcache), since the
* tcache initialization depends on it.
*/
rtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));
2018-05-11 00:32:31 +00:00
/*
* A nondeterministic seed based on the address of tsd reduces
* the likelihood of lockstep non-uniform cache index
* utilization among identical concurrent processes, but at the
* cost of test repeatability. For debug builds, instead use a
* deterministic seed.
*/
*tsd_offset_statep_get(tsd) = config_debug ? 0 :
(uint64_t)(uintptr_t)tsd;
2017-06-15 07:15:05 +00:00
return tsd_tcache_enabled_data_init(tsd);
}
2017-06-15 07:15:05 +00:00
static void
assert_tsd_data_cleanup_done(tsd_t *tsd) {
assert(!tsd_nominal(tsd));
assert(*tsd_arenap_get_unsafe(tsd) == NULL);
assert(*tsd_iarenap_get_unsafe(tsd) == NULL);
assert(*tsd_arenas_tdata_bypassp_get_unsafe(tsd) == true);
assert(*tsd_arenas_tdatap_get_unsafe(tsd) == NULL);
assert(*tsd_tcache_enabledp_get_unsafe(tsd) == false);
assert(*tsd_prof_tdatap_get_unsafe(tsd) == NULL);
}
2017-06-15 07:15:05 +00:00
static bool
tsd_data_init_nocleanup(tsd_t *tsd) {
2017-07-03 23:27:57 +00:00
assert(tsd->state == tsd_state_reincarnated ||
tsd->state == tsd_state_minimal_initialized);
2017-06-15 07:15:05 +00:00
/*
* During reincarnation, there is no guarantee that the cleanup function
* will be called (deallocation may happen after all tsd destructors).
* We set up tsd in a way that no cleanup is needed.
*/
rtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));
*tsd_arenas_tdata_bypassp_get(tsd) = true;
*tsd_tcache_enabledp_get_unsafe(tsd) = false;
*tsd_reentrancy_levelp_get(tsd) = 1;
assert_tsd_data_cleanup_done(tsd);
return false;
}
2017-06-15 07:15:05 +00:00
tsd_t *
2017-07-03 23:27:57 +00:00
tsd_fetch_slow(tsd_t *tsd, bool minimal) {
assert(!tsd_fast(tsd));
2017-06-15 07:15:05 +00:00
if (tsd->state == tsd_state_nominal_slow) {
/* On slow path but no work needed. */
assert(malloc_slow || !tsd_tcache_enabled_get(tsd) ||
tsd_reentrancy_level_get(tsd) > 0 ||
*tsd_arenas_tdata_bypassp_get(tsd));
} else if (tsd->state == tsd_state_uninitialized) {
2017-07-03 23:27:57 +00:00
if (!minimal) {
tsd->state = tsd_state_nominal;
tsd_slow_update(tsd);
/* Trigger cleanup handler registration. */
tsd_set(tsd);
tsd_data_init(tsd);
} else {
tsd->state = tsd_state_minimal_initialized;
tsd_set(tsd);
tsd_data_init_nocleanup(tsd);
}
} else if (tsd->state == tsd_state_minimal_initialized) {
if (!minimal) {
/* Switch to fully initialized. */
tsd->state = tsd_state_nominal;
assert(*tsd_reentrancy_levelp_get(tsd) >= 1);
(*tsd_reentrancy_levelp_get(tsd))--;
tsd_slow_update(tsd);
tsd_data_init(tsd);
} else {
assert_tsd_data_cleanup_done(tsd);
}
2017-06-15 07:15:05 +00:00
} else if (tsd->state == tsd_state_purgatory) {
tsd->state = tsd_state_reincarnated;
tsd_set(tsd);
tsd_data_init_nocleanup(tsd);
} else {
assert(tsd->state == tsd_state_reincarnated);
}
return tsd;
}
2017-06-15 07:15:05 +00:00
void *
malloc_tsd_malloc(size_t size) {
return a0malloc(CACHELINE_CEILING(size));
}
void
malloc_tsd_dalloc(void *wrapper) {
a0dalloc(wrapper);
}
#if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
#ifndef _WIN32
JEMALLOC_EXPORT
#endif
void
2017-06-15 07:15:05 +00:00
_malloc_thread_cleanup(void) {
bool pending[MALLOC_TSD_CLEANUPS_MAX], again;
unsigned i;
2017-06-15 07:15:05 +00:00
for (i = 0; i < ncleanups; i++) {
pending[i] = true;
2017-06-15 07:15:05 +00:00
}
do {
again = false;
for (i = 0; i < ncleanups; i++) {
if (pending[i]) {
pending[i] = cleanups[i]();
2017-06-15 07:15:05 +00:00
if (pending[i]) {
again = true;
2017-06-15 07:15:05 +00:00
}
}
}
} while (again);
}
#endif
void
2017-06-15 07:15:05 +00:00
malloc_tsd_cleanup_register(bool (*f)(void)) {
assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX);
cleanups[ncleanups] = f;
ncleanups++;
}
2017-06-15 07:15:05 +00:00
static void
tsd_do_data_cleanup(tsd_t *tsd) {
prof_tdata_cleanup(tsd);
iarena_cleanup(tsd);
arena_cleanup(tsd);
arenas_tdata_cleanup(tsd);
tcache_cleanup(tsd);
witnesses_cleanup(tsd_witness_tsdp_get_unsafe(tsd));
}
void
2017-06-15 07:15:05 +00:00
tsd_cleanup(void *arg) {
2015-08-18 00:21:25 +00:00
tsd_t *tsd = (tsd_t *)arg;
switch (tsd->state) {
2015-10-24 23:18:05 +00:00
case tsd_state_uninitialized:
/* Do nothing. */
break;
2017-07-03 23:27:57 +00:00
case tsd_state_minimal_initialized:
/* This implies the thread only did free() in its life time. */
/* Fall through. */
2017-06-15 07:15:05 +00:00
case tsd_state_reincarnated:
/*
* Reincarnated means another destructor deallocated memory
* after the destructor was called. Cleanup isn't required but
* is still called for testing and completeness.
*/
assert_tsd_data_cleanup_done(tsd);
/* Fall through. */
2015-08-18 00:21:25 +00:00
case tsd_state_nominal:
2017-06-15 07:15:05 +00:00
case tsd_state_nominal_slow:
tsd_do_data_cleanup(tsd);
2015-08-18 00:21:25 +00:00
tsd->state = tsd_state_purgatory;
tsd_set(tsd);
break;
case tsd_state_purgatory:
/*
* The previous time this destructor was called, we set the
* state to tsd_state_purgatory so that other destructors
* wouldn't cause re-creation of the tsd. This time, do
* nothing, and do not request another callback.
*/
break;
default:
not_reached();
}
2017-06-15 07:15:05 +00:00
#ifdef JEMALLOC_JET
test_callback_t test_callback = *tsd_test_callbackp_get_unsafe(tsd);
int *data = tsd_test_datap_get_unsafe(tsd);
if (test_callback != NULL) {
test_callback(data);
}
#endif
2015-08-18 00:21:25 +00:00
}
2016-05-13 04:03:20 +00:00
tsd_t *
2017-06-15 07:15:05 +00:00
malloc_tsd_boot0(void) {
2016-05-13 04:03:20 +00:00
tsd_t *tsd;
ncleanups = 0;
2017-06-15 07:15:05 +00:00
if (tsd_boot0()) {
return NULL;
}
2016-05-13 04:03:20 +00:00
tsd = tsd_fetch();
*tsd_arenas_tdata_bypassp_get(tsd) = true;
2017-06-15 07:15:05 +00:00
return tsd;
2015-08-18 00:21:25 +00:00
}
void
2017-06-15 07:15:05 +00:00
malloc_tsd_boot1(void) {
2015-08-18 00:21:25 +00:00
tsd_boot1();
2017-06-15 07:15:05 +00:00
tsd_t *tsd = tsd_fetch();
/* malloc_slow has been set properly. Update tsd_slow. */
tsd_slow_update(tsd);
*tsd_arenas_tdata_bypassp_get(tsd) = false;
}
#ifdef _WIN32
static BOOL WINAPI
2017-06-15 07:15:05 +00:00
_tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch (fdwReason) {
#ifdef JEMALLOC_LAZY_LOCK
case DLL_THREAD_ATTACH:
isthreaded = true;
break;
#endif
case DLL_THREAD_DETACH:
_malloc_thread_cleanup();
break;
default:
break;
}
2017-06-15 07:15:05 +00:00
return true;
}
2017-06-15 07:15:05 +00:00
/*
* We need to be able to say "read" here (in the "pragma section"), but have
* hooked "read". We won't read for the rest of the file, so we can get away
* with unhooking.
*/
#ifdef read
# undef read
#endif
#ifdef _MSC_VER
# ifdef _M_IX86
# pragma comment(linker, "/INCLUDE:__tls_used")
# pragma comment(linker, "/INCLUDE:_tls_callback")
# else
# pragma comment(linker, "/INCLUDE:_tls_used")
# pragma comment(linker, "/INCLUDE:tls_callback")
# endif
# pragma section(".CRT$XLY",long,read)
#endif
JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used)
BOOL (WINAPI *const tls_callback)(HINSTANCE hinstDLL,
DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;
#endif
2014-01-23 02:47:36 +00:00
#if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \
!defined(_WIN32))
void *
2017-06-15 07:15:05 +00:00
tsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block) {
2014-01-23 02:47:36 +00:00
pthread_t self = pthread_self();
tsd_init_block_t *iter;
/* Check whether this thread has already inserted into the list. */
2016-11-09 18:42:30 +00:00
malloc_mutex_lock(TSDN_NULL, &head->lock);
2014-01-23 02:47:36 +00:00
ql_foreach(iter, &head->blocks, link) {
if (iter->thread == self) {
2016-11-09 18:42:30 +00:00
malloc_mutex_unlock(TSDN_NULL, &head->lock);
2017-06-15 07:15:05 +00:00
return iter->data;
2014-01-23 02:47:36 +00:00
}
}
/* Insert block into list. */
ql_elm_new(block, link);
block->thread = self;
ql_tail_insert(&head->blocks, block, link);
2016-11-09 18:42:30 +00:00
malloc_mutex_unlock(TSDN_NULL, &head->lock);
2017-06-15 07:15:05 +00:00
return NULL;
2014-01-23 02:47:36 +00:00
}
void
2017-06-15 07:15:05 +00:00
tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block) {
2016-11-09 18:42:30 +00:00
malloc_mutex_lock(TSDN_NULL, &head->lock);
2014-01-23 02:47:36 +00:00
ql_remove(&head->blocks, block, link);
2016-11-09 18:42:30 +00:00
malloc_mutex_unlock(TSDN_NULL, &head->lock);
2014-01-23 02:47:36 +00:00
}
#endif