From 39d6420c0c39619176af3477b827e8a92442b768 Mon Sep 17 00:00:00 2001 From: David Goldblatt Date: Thu, 8 Mar 2018 16:51:07 -0800 Subject: [PATCH] TSD: Make state atomic. This will let us change the state of another thread remotely, eventually. --- include/jemalloc/internal/tsd.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/include/jemalloc/internal/tsd.h b/include/jemalloc/internal/tsd.h index aa64d937..53ac7415 100644 --- a/include/jemalloc/internal/tsd.h +++ b/include/jemalloc/internal/tsd.h @@ -78,7 +78,7 @@ typedef void (*test_callback_t)(int *); MALLOC_TEST_TSD #define TSD_INITIALIZER { \ - tsd_state_uninitialized, \ + ATOMIC_INIT(tsd_state_uninitialized), \ TCACHE_ENABLED_ZERO_INITIALIZER, \ false, \ 0, \ @@ -116,7 +116,7 @@ struct tsd_s { */ /* We manually limit the state to just a single byte. */ - uint8_t state; + atomic_u8_t state; #define O(n, t, nt) \ t use_a_getter_or_setter_instead_##n; MALLOC_TSD @@ -125,12 +125,18 @@ MALLOC_TSD JEMALLOC_ALWAYS_INLINE uint8_t tsd_state_get(tsd_t *tsd) { - return tsd->state; + /* + * 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; } JEMALLOC_ALWAYS_INLINE void tsd_state_set(tsd_t *tsd, uint8_t state) { - tsd->state = state; + atomic_store_u8(&tsd->state, state, ATOMIC_RELAXED); } /*