From bc32ec3503433fae4c737c7ffe6b3822ce98d5d8 Mon Sep 17 00:00:00 2001 From: David Goldblatt Date: Tue, 4 Apr 2017 15:12:24 -0700 Subject: [PATCH] Move arena-tracking atomics in jemalloc.c to C11-style --- include/jemalloc/internal/extent_inlines.h | 2 +- include/jemalloc/internal/jemalloc_internal.h.in | 7 +++---- src/jemalloc.c | 14 ++++++++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/include/jemalloc/internal/extent_inlines.h b/include/jemalloc/internal/extent_inlines.h index e6e447cf..f1b94776 100644 --- a/include/jemalloc/internal/extent_inlines.h +++ b/include/jemalloc/internal/extent_inlines.h @@ -63,7 +63,7 @@ extent_arena_get(const extent_t *extent) { return NULL; } assert(arena_ind <= MALLOCX_ARENA_MAX); - return arenas[arena_ind]; + return (arena_t *)atomic_load_p(&arenas[arena_ind], ATOMIC_ACQUIRE); } JEMALLOC_INLINE szind_t diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in index 4255b639..04f91c07 100644 --- a/include/jemalloc/internal/jemalloc_internal.h.in +++ b/include/jemalloc/internal/jemalloc_internal.h.in @@ -469,7 +469,7 @@ extern unsigned narenas_auto; * Arenas that are used to service external requests. Not all elements of the * arenas array are necessarily used; arenas are created lazily as needed. */ -extern arena_t *arenas[]; +extern atomic_p_t arenas[]; /* * pind2sz_tab encodes the same information as could be computed by @@ -909,10 +909,9 @@ arena_get(tsdn_t *tsdn, unsigned ind, bool init_if_missing) { assert(ind <= MALLOCX_ARENA_MAX); - ret = arenas[ind]; + ret = (arena_t *)atomic_load_p(&arenas[ind], ATOMIC_ACQUIRE); if (unlikely(ret == NULL)) { - ret = (arena_t *)atomic_read_p((void **)&arenas[ind]); - if (init_if_missing && unlikely(ret == NULL)) { + if (init_if_missing) { ret = arena_init(tsdn, ind, (extent_hooks_t *)&extent_hooks_default); } diff --git a/src/jemalloc.c b/src/jemalloc.c index 7c8fe9c9..94ae030c 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -55,10 +55,12 @@ static malloc_mutex_t arenas_lock; * arenas[0..narenas_auto) are used for automatic multiplexing of threads and * arenas. arenas[narenas_auto..narenas_total) are only used if the application * takes some action to create them and allocate from them. + * + * Points to an arena_t. */ JEMALLOC_ALIGNED(CACHELINE) -arena_t *arenas[MALLOCX_ARENA_MAX + 1]; -static unsigned narenas_total; /* Use narenas_total_*(). */ +atomic_p_t arenas[MALLOCX_ARENA_MAX + 1]; +static atomic_u_t narenas_total; /* Use narenas_total_*(). */ static arena_t *a0; /* arenas[0]; read-only after initialization. */ unsigned narenas_auto; /* Read-only after initialization. */ @@ -363,22 +365,22 @@ bootstrap_free(void *ptr) { void arena_set(unsigned ind, arena_t *arena) { - atomic_write_p((void **)&arenas[ind], arena); + atomic_store_p(&arenas[ind], arena, ATOMIC_RELEASE); } static void narenas_total_set(unsigned narenas) { - atomic_write_u(&narenas_total, narenas); + atomic_store_u(&narenas_total, narenas, ATOMIC_RELEASE); } static void narenas_total_inc(void) { - atomic_add_u(&narenas_total, 1); + atomic_fetch_add_u(&narenas_total, 1, ATOMIC_RELEASE); } unsigned narenas_total_get(void) { - return atomic_read_u(&narenas_total); + return atomic_load_u(&narenas_total, ATOMIC_ACQUIRE); } /* Create a new arena and insert it into the arenas array at index ind. */