2017-04-27 09:37:44 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_TSD_H
|
|
|
|
#define JEMALLOC_INTERNAL_TSD_H
|
|
|
|
|
|
|
|
#include "jemalloc/internal/arena_types.h"
|
|
|
|
#include "jemalloc/internal/assert.h"
|
2018-11-28 04:38:47 +08:00
|
|
|
#include "jemalloc/internal/bin_types.h"
|
2017-04-27 09:37:44 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_externs.h"
|
2020-05-28 05:31:00 +08:00
|
|
|
#include "jemalloc/internal/peak.h"
|
2017-04-27 09:37:44 +08:00
|
|
|
#include "jemalloc/internal/prof_types.h"
|
|
|
|
#include "jemalloc/internal/ql.h"
|
2017-05-24 05:26:31 +08:00
|
|
|
#include "jemalloc/internal/rtree_tsd.h"
|
2017-04-27 09:37:44 +08:00
|
|
|
#include "jemalloc/internal/tcache_types.h"
|
|
|
|
#include "jemalloc/internal/tcache_structs.h"
|
|
|
|
#include "jemalloc/internal/util.h"
|
2017-05-23 10:32:04 +08:00
|
|
|
#include "jemalloc/internal/witness.h"
|
2017-04-27 09:37:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread-Specific-Data layout
|
|
|
|
*
|
2020-04-08 07:28:43 +08:00
|
|
|
* At least some thread-local data gets touched on the fast-path of almost all
|
|
|
|
* malloc operations. But much of it is only necessary down slow-paths, or
|
|
|
|
* testing. We want to colocate the fast-path data so that it can live on the
|
|
|
|
* same cacheline if possible. So we define three tiers of hotness:
|
|
|
|
* TSD_DATA_FAST: Touched on the alloc/dalloc fast paths.
|
|
|
|
* TSD_DATA_SLOW: Touched down slow paths. "Slow" here is sort of general;
|
|
|
|
* there are "semi-slow" paths like "not a sized deallocation, but can still
|
|
|
|
* live in the tcache". We'll want to keep these closer to the fast-path
|
|
|
|
* data.
|
|
|
|
* TSD_DATA_SLOWER: Only touched in test or debug modes, or not touched at all.
|
|
|
|
*
|
|
|
|
* An additional concern is that the larger tcache bins won't be used (we have a
|
|
|
|
* bin per size class, but by default only cache relatively small objects). So
|
|
|
|
* the earlier bins are in the TSD_DATA_FAST tier, but the later ones are in the
|
|
|
|
* TSD_DATA_SLOWER tier.
|
|
|
|
*
|
|
|
|
* As a result of all this, we put the slow data first, then the fast data, then
|
|
|
|
* the slower data, while keeping the tcache as the last element of the fast
|
|
|
|
* data (so that the fast -> slower transition happens midway through the
|
|
|
|
* tcache). While we don't yet play alignment tricks to guarantee it, this
|
|
|
|
* increases our odds of getting some cache/page locality on fast paths.
|
2017-04-27 09:37:44 +08:00
|
|
|
*/
|
2020-04-08 07:28:43 +08:00
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
typedef void (*test_callback_t)(int *);
|
|
|
|
# define MALLOC_TSD_TEST_DATA_INIT 0x72b65c10
|
|
|
|
# define MALLOC_TEST_TSD \
|
2017-05-23 10:32:04 +08:00
|
|
|
O(test_data, int, int) \
|
|
|
|
O(test_callback, test_callback_t, int)
|
2017-04-27 09:37:44 +08:00
|
|
|
# define MALLOC_TEST_TSD_INITIALIZER , MALLOC_TSD_TEST_DATA_INIT, NULL
|
|
|
|
#else
|
|
|
|
# define MALLOC_TEST_TSD
|
|
|
|
# define MALLOC_TEST_TSD_INITIALIZER
|
|
|
|
#endif
|
|
|
|
|
2020-04-08 07:59:57 +08:00
|
|
|
typedef ql_elm(tsd_t) tsd_link_t;
|
|
|
|
|
2019-09-04 06:04:48 +08:00
|
|
|
/* O(name, type, nullable type) */
|
2020-04-08 07:28:43 +08:00
|
|
|
#define TSD_DATA_SLOW \
|
2017-05-23 10:32:04 +08:00
|
|
|
O(tcache_enabled, bool, bool) \
|
|
|
|
O(arenas_tdata_bypass, bool, bool) \
|
|
|
|
O(reentrancy_level, int8_t, int8_t) \
|
|
|
|
O(narenas_tdata, uint32_t, uint32_t) \
|
2019-09-04 06:04:48 +08:00
|
|
|
O(thread_allocated_last_event, uint64_t, uint64_t) \
|
|
|
|
O(thread_allocated_next_event, uint64_t, uint64_t) \
|
2020-01-29 13:12:06 +08:00
|
|
|
O(thread_deallocated_last_event, uint64_t, uint64_t) \
|
|
|
|
O(thread_deallocated_next_event, uint64_t, uint64_t) \
|
2019-10-25 07:41:45 +08:00
|
|
|
O(tcache_gc_event_wait, uint64_t, uint64_t) \
|
2020-01-29 13:12:06 +08:00
|
|
|
O(tcache_gc_dalloc_event_wait, uint64_t, uint64_t) \
|
2019-09-04 06:04:48 +08:00
|
|
|
O(prof_sample_event_wait, uint64_t, uint64_t) \
|
2019-10-15 00:35:51 +08:00
|
|
|
O(prof_sample_last_event, uint64_t, uint64_t) \
|
2020-01-14 14:29:17 +08:00
|
|
|
O(stats_interval_event_wait, uint64_t, uint64_t) \
|
|
|
|
O(stats_interval_last_event, uint64_t, uint64_t) \
|
2020-05-28 05:31:00 +08:00
|
|
|
O(peak_alloc_event_wait, uint64_t, uint64_t) \
|
|
|
|
O(peak_dalloc_event_wait, uint64_t, uint64_t) \
|
2019-09-04 06:04:48 +08:00
|
|
|
O(prof_tdata, prof_tdata_t *, prof_tdata_t *) \
|
2019-11-05 08:48:12 +08:00
|
|
|
O(prng_state, uint64_t, uint64_t) \
|
2017-05-23 10:32:04 +08:00
|
|
|
O(iarena, arena_t *, arena_t *) \
|
|
|
|
O(arena, arena_t *, arena_t *) \
|
|
|
|
O(arenas_tdata, arena_tdata_t *, arena_tdata_t *)\
|
2020-10-23 05:13:09 +08:00
|
|
|
O(sec_shard, uint8_t, uint8_t) \
|
2020-04-08 07:59:57 +08:00
|
|
|
O(binshards, tsd_binshards_t, tsd_binshards_t)\
|
|
|
|
O(tsd_link, tsd_link_t, tsd_link_t) \
|
2020-04-08 08:48:35 +08:00
|
|
|
O(in_hook, bool, bool) \
|
2020-05-28 05:31:00 +08:00
|
|
|
O(peak, peak_t, peak_t) \
|
2020-04-11 06:02:38 +08:00
|
|
|
O(tcache_slow, tcache_slow_t, tcache_slow_t) \
|
|
|
|
O(rtree_ctx, rtree_ctx_t, rtree_ctx_t)
|
2019-09-04 06:04:48 +08:00
|
|
|
|
2020-04-08 07:28:43 +08:00
|
|
|
#define TSD_DATA_SLOW_INITIALIZER \
|
2019-09-04 06:04:48 +08:00
|
|
|
/* tcache_enabled */ TCACHE_ENABLED_ZERO_INITIALIZER, \
|
|
|
|
/* arenas_tdata_bypass */ false, \
|
|
|
|
/* reentrancy_level */ 0, \
|
|
|
|
/* narenas_tdata */ 0, \
|
2019-10-15 00:35:51 +08:00
|
|
|
/* thread_allocated_last_event */ 0, \
|
2020-04-17 02:50:29 +08:00
|
|
|
/* thread_allocated_next_event */ 0, \
|
2020-01-29 13:12:06 +08:00
|
|
|
/* thread_deallocated_last_event */ 0, \
|
2020-04-17 02:50:29 +08:00
|
|
|
/* thread_deallocated_next_event */ 0, \
|
|
|
|
/* tcache_gc_event_wait */ 0, \
|
|
|
|
/* tcache_gc_dalloc_event_wait */ 0, \
|
|
|
|
/* prof_sample_event_wait */ 0, \
|
2019-10-15 00:35:51 +08:00
|
|
|
/* prof_sample_last_event */ 0, \
|
2020-04-17 02:50:29 +08:00
|
|
|
/* stats_interval_event_wait */ 0, \
|
2020-01-14 14:29:17 +08:00
|
|
|
/* stats_interval_last_event */ 0, \
|
2020-05-28 05:31:00 +08:00
|
|
|
/* peak_alloc_event_wait */ 0, \
|
|
|
|
/* peak_dalloc_event_wait */ 0, \
|
2019-09-04 06:04:48 +08:00
|
|
|
/* prof_tdata */ NULL, \
|
2019-11-05 08:48:12 +08:00
|
|
|
/* prng_state */ 0, \
|
2019-09-04 06:04:48 +08:00
|
|
|
/* iarena */ NULL, \
|
|
|
|
/* arena */ NULL, \
|
|
|
|
/* arenas_tdata */ NULL, \
|
2020-10-23 05:13:09 +08:00
|
|
|
/* sec_shard */ (uint8_t)-1, \
|
2020-04-08 07:59:57 +08:00
|
|
|
/* binshards */ TSD_BINSHARDS_ZERO_INITIALIZER, \
|
|
|
|
/* tsd_link */ {NULL}, \
|
2020-04-08 08:48:35 +08:00
|
|
|
/* in_hook */ false, \
|
2020-05-28 05:31:00 +08:00
|
|
|
/* peak */ PEAK_INITIALIZER, \
|
2020-04-11 06:02:38 +08:00
|
|
|
/* tcache_slow */ TCACHE_SLOW_ZERO_INITIALIZER, \
|
|
|
|
/* rtree_ctx */ RTREE_CTX_ZERO_INITIALIZER,
|
2020-04-08 07:28:43 +08:00
|
|
|
|
|
|
|
/* O(name, type, nullable type) */
|
|
|
|
#define TSD_DATA_FAST \
|
|
|
|
O(thread_allocated, uint64_t, uint64_t) \
|
|
|
|
O(thread_allocated_next_event_fast, uint64_t, uint64_t) \
|
|
|
|
O(thread_deallocated, uint64_t, uint64_t) \
|
|
|
|
O(thread_deallocated_next_event_fast, uint64_t, uint64_t) \
|
|
|
|
O(tcache, tcache_t, tcache_t)
|
|
|
|
|
|
|
|
#define TSD_DATA_FAST_INITIALIZER \
|
|
|
|
/* thread_allocated */ 0, \
|
|
|
|
/* thread_allocated_next_event_fast */ 0, \
|
|
|
|
/* thread_deallocated */ 0, \
|
|
|
|
/* thread_deallocated_next_event_fast */ 0, \
|
|
|
|
/* tcache */ TCACHE_ZERO_INITIALIZER,
|
|
|
|
|
|
|
|
/* O(name, type, nullable type) */
|
|
|
|
#define TSD_DATA_SLOWER \
|
|
|
|
O(witness_tsd, witness_tsd_t, witness_tsdn_t) \
|
|
|
|
MALLOC_TEST_TSD
|
|
|
|
|
|
|
|
#define TSD_DATA_SLOWER_INITIALIZER \
|
2019-09-04 06:04:48 +08:00
|
|
|
/* witness */ WITNESS_TSD_INITIALIZER \
|
2020-04-08 07:28:43 +08:00
|
|
|
/* test data */ MALLOC_TEST_TSD_INITIALIZER
|
|
|
|
|
|
|
|
|
|
|
|
#define TSD_INITIALIZER { \
|
|
|
|
TSD_DATA_SLOW_INITIALIZER \
|
|
|
|
/* state */ ATOMIC_INIT(tsd_state_uninitialized), \
|
|
|
|
TSD_DATA_FAST_INITIALIZER \
|
|
|
|
TSD_DATA_SLOWER_INITIALIZER \
|
2017-04-27 09:37:44 +08:00
|
|
|
}
|
|
|
|
|
2018-04-03 10:16:41 +08:00
|
|
|
void *malloc_tsd_malloc(size_t size);
|
|
|
|
void malloc_tsd_dalloc(void *wrapper);
|
|
|
|
void malloc_tsd_cleanup_register(bool (*f)(void));
|
|
|
|
tsd_t *malloc_tsd_boot0(void);
|
|
|
|
void malloc_tsd_boot1(void);
|
|
|
|
void tsd_cleanup(void *arg);
|
|
|
|
tsd_t *tsd_fetch_slow(tsd_t *tsd, bool internal);
|
|
|
|
void tsd_state_set(tsd_t *tsd, uint8_t new_state);
|
|
|
|
void tsd_slow_update(tsd_t *tsd);
|
2018-07-27 05:42:37 +08:00
|
|
|
void tsd_prefork(tsd_t *tsd);
|
|
|
|
void tsd_postfork_parent(tsd_t *tsd);
|
|
|
|
void tsd_postfork_child(tsd_t *tsd);
|
2018-04-03 10:16:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call ..._inc when your module wants to take all threads down the slow paths,
|
|
|
|
* and ..._dec when it no longer needs to.
|
|
|
|
*/
|
|
|
|
void tsd_global_slow_inc(tsdn_t *tsdn);
|
|
|
|
void tsd_global_slow_dec(tsdn_t *tsdn);
|
2018-05-12 07:43:43 +08:00
|
|
|
bool tsd_global_slow();
|
2018-04-03 10:16:41 +08:00
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
enum {
|
2018-04-03 10:16:41 +08:00
|
|
|
/* Common case --> jnz. */
|
|
|
|
tsd_state_nominal = 0,
|
|
|
|
/* Initialized but on slow path. */
|
|
|
|
tsd_state_nominal_slow = 1,
|
|
|
|
/*
|
|
|
|
* Some thread has changed global state in such a way that all nominal
|
|
|
|
* threads need to recompute their fast / slow status the next time they
|
|
|
|
* get a chance.
|
|
|
|
*
|
|
|
|
* Any thread can change another thread's status *to* recompute, but
|
|
|
|
* threads are the only ones who can change their status *from*
|
|
|
|
* recompute.
|
|
|
|
*/
|
|
|
|
tsd_state_nominal_recompute = 2,
|
|
|
|
/*
|
|
|
|
* The above nominal states should be lower values. We use
|
|
|
|
* tsd_nominal_max to separate nominal states from threads in the
|
|
|
|
* process of being born / dying.
|
|
|
|
*/
|
|
|
|
tsd_state_nominal_max = 2,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A thread might free() during its death as its only allocator action;
|
|
|
|
* in such scenarios, we need tsd, but set up in such a way that no
|
|
|
|
* cleanup is necessary.
|
|
|
|
*/
|
|
|
|
tsd_state_minimal_initialized = 3,
|
|
|
|
/* States during which we know we're in thread death. */
|
|
|
|
tsd_state_purgatory = 4,
|
|
|
|
tsd_state_reincarnated = 5,
|
|
|
|
/*
|
|
|
|
* What it says on the tin; tsd that hasn't been initialized. Note
|
|
|
|
* that even when the tsd struct lives in TLS, when need to keep track
|
|
|
|
* of stuff like whether or not our pthread destructors have been
|
|
|
|
* scheduled, so this really truly is different than the nominal state.
|
|
|
|
*/
|
|
|
|
tsd_state_uninitialized = 6
|
2017-04-27 09:37:44 +08:00
|
|
|
};
|
|
|
|
|
2018-05-12 06:18:52 +08:00
|
|
|
/*
|
|
|
|
* Some TSD accesses can only be done in a nominal state. To enforce this, we
|
|
|
|
* wrap TSD member access in a function that asserts on TSD state, and mangle
|
|
|
|
* field names to prevent touching them accidentally.
|
|
|
|
*/
|
|
|
|
#define TSD_MANGLE(n) cant_access_tsd_items_directly_use_a_getter_or_setter_##n
|
|
|
|
|
2019-03-08 08:01:55 +08:00
|
|
|
#ifdef JEMALLOC_U8_ATOMICS
|
|
|
|
# define tsd_state_t atomic_u8_t
|
|
|
|
# define tsd_atomic_load atomic_load_u8
|
|
|
|
# define tsd_atomic_store atomic_store_u8
|
|
|
|
# define tsd_atomic_exchange atomic_exchange_u8
|
|
|
|
#else
|
|
|
|
# define tsd_state_t atomic_u32_t
|
|
|
|
# define tsd_atomic_load atomic_load_u32
|
|
|
|
# define tsd_atomic_store atomic_store_u32
|
|
|
|
# define tsd_atomic_exchange atomic_exchange_u32
|
|
|
|
#endif
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
/* The actual tsd. */
|
|
|
|
struct tsd_s {
|
|
|
|
/*
|
|
|
|
* The contents should be treated as totally opaque outside the tsd
|
|
|
|
* module. Access any thread-local state through the getters and
|
|
|
|
* setters below.
|
|
|
|
*/
|
2018-03-09 08:34:17 +08:00
|
|
|
|
2020-04-08 07:28:43 +08:00
|
|
|
#define O(n, t, nt) \
|
|
|
|
t TSD_MANGLE(n);
|
|
|
|
|
|
|
|
TSD_DATA_SLOW
|
2019-03-08 08:01:55 +08:00
|
|
|
/*
|
|
|
|
* We manually limit the state to just a single byte. Unless the 8-bit
|
|
|
|
* atomics are unavailable (which is rare).
|
|
|
|
*/
|
|
|
|
tsd_state_t state;
|
2020-04-08 07:28:43 +08:00
|
|
|
TSD_DATA_FAST
|
|
|
|
TSD_DATA_SLOWER
|
2017-04-27 09:37:44 +08:00
|
|
|
#undef O
|
|
|
|
};
|
|
|
|
|
2018-03-09 08:34:17 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE uint8_t
|
|
|
|
tsd_state_get(tsd_t *tsd) {
|
2018-03-09 08:51:07 +08:00
|
|
|
/*
|
|
|
|
* This should be atomic. Unfortunately, compilers right now can't tell
|
|
|
|
* that this can be done as a memory comparison, and forces a load into
|
|
|
|
* a register that hurts fast-path performance.
|
|
|
|
*/
|
|
|
|
/* return atomic_load_u8(&tsd->state, ATOMIC_RELAXED); */
|
|
|
|
return *(uint8_t *)&tsd->state;
|
2018-03-09 08:34:17 +08:00
|
|
|
}
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
/*
|
|
|
|
* Wrapper around tsd_t that makes it possible to avoid implicit conversion
|
|
|
|
* between tsd_t and tsdn_t, where tsdn_t is "nullable" and has to be
|
|
|
|
* explicitly converted to tsd_t, which is non-nullable.
|
|
|
|
*/
|
|
|
|
struct tsdn_s {
|
|
|
|
tsd_t tsd;
|
|
|
|
};
|
|
|
|
#define TSDN_NULL ((tsdn_t *)0)
|
2017-05-23 10:32:04 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE tsdn_t *
|
|
|
|
tsd_tsdn(tsd_t *tsd) {
|
|
|
|
return (tsdn_t *)tsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
tsdn_null(const tsdn_t *tsdn) {
|
|
|
|
return tsdn == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE tsd_t *
|
|
|
|
tsdn_tsd(tsdn_t *tsdn) {
|
|
|
|
assert(!tsdn_null(tsdn));
|
|
|
|
|
|
|
|
return &tsdn->tsd;
|
|
|
|
}
|
2017-04-27 09:37:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We put the platform-specific data declarations and inlines into their own
|
|
|
|
* header files to avoid cluttering this file. They define tsd_boot0,
|
|
|
|
* tsd_boot1, tsd_boot, tsd_booted_get, tsd_get_allocates, tsd_get, and tsd_set.
|
|
|
|
*/
|
|
|
|
#ifdef JEMALLOC_MALLOC_THREAD_CLEANUP
|
|
|
|
#include "jemalloc/internal/tsd_malloc_thread_cleanup.h"
|
|
|
|
#elif (defined(JEMALLOC_TLS))
|
|
|
|
#include "jemalloc/internal/tsd_tls.h"
|
|
|
|
#elif (defined(_WIN32))
|
|
|
|
#include "jemalloc/internal/tsd_win.h"
|
|
|
|
#else
|
|
|
|
#include "jemalloc/internal/tsd_generic.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tsd_foop_get_unsafe(tsd) returns a pointer to the thread-local instance of
|
|
|
|
* foo. This omits some safety checks, and so can be used during tsd
|
|
|
|
* initialization and cleanup.
|
|
|
|
*/
|
2017-05-23 10:32:04 +08:00
|
|
|
#define O(n, t, nt) \
|
2017-04-27 09:37:44 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE t * \
|
|
|
|
tsd_##n##p_get_unsafe(tsd_t *tsd) { \
|
2018-05-12 06:18:52 +08:00
|
|
|
return &tsd->TSD_MANGLE(n); \
|
2017-04-27 09:37:44 +08:00
|
|
|
}
|
2020-04-08 07:28:43 +08:00
|
|
|
TSD_DATA_SLOW
|
|
|
|
TSD_DATA_FAST
|
|
|
|
TSD_DATA_SLOWER
|
2017-04-27 09:37:44 +08:00
|
|
|
#undef O
|
|
|
|
|
|
|
|
/* tsd_foop_get(tsd) returns a pointer to the thread-local instance of foo. */
|
2017-05-23 10:32:04 +08:00
|
|
|
#define O(n, t, nt) \
|
2017-04-27 09:37:44 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE t * \
|
|
|
|
tsd_##n##p_get(tsd_t *tsd) { \
|
2018-04-03 10:16:41 +08:00
|
|
|
/* \
|
|
|
|
* Because the state might change asynchronously if it's \
|
|
|
|
* nominal, we need to make sure that we only read it once. \
|
|
|
|
*/ \
|
|
|
|
uint8_t state = tsd_state_get(tsd); \
|
|
|
|
assert(state == tsd_state_nominal || \
|
|
|
|
state == tsd_state_nominal_slow || \
|
|
|
|
state == tsd_state_nominal_recompute || \
|
|
|
|
state == tsd_state_reincarnated || \
|
|
|
|
state == tsd_state_minimal_initialized); \
|
2017-04-27 09:37:44 +08:00
|
|
|
return tsd_##n##p_get_unsafe(tsd); \
|
|
|
|
}
|
2020-04-08 07:28:43 +08:00
|
|
|
TSD_DATA_SLOW
|
|
|
|
TSD_DATA_FAST
|
|
|
|
TSD_DATA_SLOWER
|
2017-04-27 09:37:44 +08:00
|
|
|
#undef O
|
|
|
|
|
2017-05-23 10:32:04 +08:00
|
|
|
/*
|
|
|
|
* tsdn_foop_get(tsdn) returns either the thread-local instance of foo (if tsdn
|
|
|
|
* isn't NULL), or NULL (if tsdn is NULL), cast to the nullable pointer type.
|
|
|
|
*/
|
|
|
|
#define O(n, t, nt) \
|
|
|
|
JEMALLOC_ALWAYS_INLINE nt * \
|
|
|
|
tsdn_##n##p_get(tsdn_t *tsdn) { \
|
|
|
|
if (tsdn_null(tsdn)) { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
tsd_t *tsd = tsdn_tsd(tsdn); \
|
|
|
|
return (nt *)tsd_##n##p_get(tsd); \
|
|
|
|
}
|
2020-04-08 07:28:43 +08:00
|
|
|
TSD_DATA_SLOW
|
|
|
|
TSD_DATA_FAST
|
|
|
|
TSD_DATA_SLOWER
|
2017-05-23 10:32:04 +08:00
|
|
|
#undef O
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
/* tsd_foo_get(tsd) returns the value of the thread-local instance of foo. */
|
2017-05-23 10:32:04 +08:00
|
|
|
#define O(n, t, nt) \
|
2017-04-27 09:37:44 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE t \
|
|
|
|
tsd_##n##_get(tsd_t *tsd) { \
|
|
|
|
return *tsd_##n##p_get(tsd); \
|
|
|
|
}
|
2020-04-08 07:28:43 +08:00
|
|
|
TSD_DATA_SLOW
|
|
|
|
TSD_DATA_FAST
|
|
|
|
TSD_DATA_SLOWER
|
2017-04-27 09:37:44 +08:00
|
|
|
#undef O
|
|
|
|
|
|
|
|
/* tsd_foo_set(tsd, val) updates the thread-local instance of foo to be val. */
|
2017-05-23 10:32:04 +08:00
|
|
|
#define O(n, t, nt) \
|
2017-04-27 09:37:44 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void \
|
|
|
|
tsd_##n##_set(tsd_t *tsd, t val) { \
|
2018-03-09 08:34:17 +08:00
|
|
|
assert(tsd_state_get(tsd) != tsd_state_reincarnated && \
|
|
|
|
tsd_state_get(tsd) != tsd_state_minimal_initialized); \
|
2017-04-27 09:37:44 +08:00
|
|
|
*tsd_##n##p_get(tsd) = val; \
|
|
|
|
}
|
2020-04-08 07:28:43 +08:00
|
|
|
TSD_DATA_SLOW
|
|
|
|
TSD_DATA_FAST
|
|
|
|
TSD_DATA_SLOWER
|
2017-04-27 09:37:44 +08:00
|
|
|
#undef O
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
|
|
tsd_assert_fast(tsd_t *tsd) {
|
2018-04-03 10:16:41 +08:00
|
|
|
/*
|
|
|
|
* Note that our fastness assertion does *not* include global slowness
|
|
|
|
* counters; it's not in general possible to ensure that they won't
|
|
|
|
* change asynchronously from underneath us.
|
|
|
|
*/
|
2017-04-27 09:37:44 +08:00
|
|
|
assert(!malloc_slow && tsd_tcache_enabled_get(tsd) &&
|
|
|
|
tsd_reentrancy_level_get(tsd) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
tsd_fast(tsd_t *tsd) {
|
2018-03-09 08:34:17 +08:00
|
|
|
bool fast = (tsd_state_get(tsd) == tsd_state_nominal);
|
2017-04-27 09:37:44 +08:00
|
|
|
if (fast) {
|
|
|
|
tsd_assert_fast(tsd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fast;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE tsd_t *
|
2017-06-16 07:53:22 +08:00
|
|
|
tsd_fetch_impl(bool init, bool minimal) {
|
2017-04-27 09:37:44 +08:00
|
|
|
tsd_t *tsd = tsd_get(init);
|
|
|
|
|
|
|
|
if (!init && tsd_get_allocates() && tsd == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
assert(tsd != NULL);
|
|
|
|
|
2018-03-09 08:34:17 +08:00
|
|
|
if (unlikely(tsd_state_get(tsd) != tsd_state_nominal)) {
|
2017-06-16 07:53:22 +08:00
|
|
|
return tsd_fetch_slow(tsd, minimal);
|
2017-04-27 09:37:44 +08:00
|
|
|
}
|
|
|
|
assert(tsd_fast(tsd));
|
|
|
|
tsd_assert_fast(tsd);
|
|
|
|
|
|
|
|
return tsd;
|
|
|
|
}
|
|
|
|
|
2017-06-16 07:53:22 +08:00
|
|
|
/* Get a minimal TSD that requires no cleanup. See comments in free(). */
|
2017-06-08 07:12:50 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE tsd_t *
|
2017-06-16 07:53:22 +08:00
|
|
|
tsd_fetch_min(void) {
|
2017-06-08 07:12:50 +08:00
|
|
|
return tsd_fetch_impl(true, true);
|
|
|
|
}
|
|
|
|
|
2017-06-16 07:53:22 +08:00
|
|
|
/* For internal background threads use only. */
|
|
|
|
JEMALLOC_ALWAYS_INLINE tsd_t *
|
|
|
|
tsd_internal_fetch(void) {
|
|
|
|
tsd_t *tsd = tsd_fetch_min();
|
|
|
|
/* Use reincarnated state to prevent full initialization. */
|
2018-03-09 08:34:17 +08:00
|
|
|
tsd_state_set(tsd, tsd_state_reincarnated);
|
2017-06-16 07:53:22 +08:00
|
|
|
|
|
|
|
return tsd;
|
|
|
|
}
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE tsd_t *
|
|
|
|
tsd_fetch(void) {
|
2017-06-08 07:12:50 +08:00
|
|
|
return tsd_fetch_impl(true, false);
|
2017-04-27 09:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
tsd_nominal(tsd_t *tsd) {
|
2020-01-17 05:00:35 +08:00
|
|
|
bool nominal = tsd_state_get(tsd) <= tsd_state_nominal_max;
|
|
|
|
assert(nominal || tsd_reentrancy_level_get(tsd) > 0);
|
|
|
|
|
|
|
|
return nominal;
|
2017-04-27 09:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE tsdn_t *
|
|
|
|
tsdn_fetch(void) {
|
|
|
|
if (!tsd_booted_get()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-08 07:12:50 +08:00
|
|
|
return tsd_tsdn(tsd_fetch_impl(false, false));
|
2017-04-27 09:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE rtree_ctx_t *
|
|
|
|
tsd_rtree_ctx(tsd_t *tsd) {
|
|
|
|
return tsd_rtree_ctxp_get(tsd);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE rtree_ctx_t *
|
|
|
|
tsdn_rtree_ctx(tsdn_t *tsdn, rtree_ctx_t *fallback) {
|
|
|
|
/*
|
|
|
|
* If tsd cannot be accessed, initialize the fallback rtree_ctx and
|
|
|
|
* return a pointer to it.
|
|
|
|
*/
|
|
|
|
if (unlikely(tsdn_null(tsdn))) {
|
|
|
|
rtree_ctx_data_init(fallback);
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
return tsd_rtree_ctx(tsdn_tsd(tsdn));
|
|
|
|
}
|
|
|
|
|
2019-12-18 03:57:08 +08:00
|
|
|
static inline bool
|
|
|
|
tsd_state_nocleanup(tsd_t *tsd) {
|
|
|
|
return tsd_state_get(tsd) == tsd_state_reincarnated ||
|
|
|
|
tsd_state_get(tsd) == tsd_state_minimal_initialized;
|
|
|
|
}
|
|
|
|
|
2019-12-03 08:45:40 +08:00
|
|
|
/*
|
|
|
|
* These "raw" tsd reentrancy functions don't have any debug checking to make
|
|
|
|
* sure that we're not touching arena 0. Better is to call pre_reentrancy and
|
|
|
|
* post_reentrancy if this is possible.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
tsd_pre_reentrancy_raw(tsd_t *tsd) {
|
|
|
|
bool fast = tsd_fast(tsd);
|
|
|
|
assert(tsd_reentrancy_level_get(tsd) < INT8_MAX);
|
|
|
|
++*tsd_reentrancy_levelp_get(tsd);
|
|
|
|
if (fast) {
|
|
|
|
/* Prepare slow path for reentrancy. */
|
|
|
|
tsd_slow_update(tsd);
|
|
|
|
assert(tsd_state_get(tsd) == tsd_state_nominal_slow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
tsd_post_reentrancy_raw(tsd_t *tsd) {
|
|
|
|
int8_t *reentrancy_level = tsd_reentrancy_levelp_get(tsd);
|
|
|
|
assert(*reentrancy_level > 0);
|
|
|
|
if (--*reentrancy_level == 0) {
|
|
|
|
tsd_slow_update(tsd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_TSD_H */
|