Move arena-tracking atomics in jemalloc.c to C11-style

This commit is contained in:
David Goldblatt 2017-04-04 15:12:24 -07:00 committed by David Goldblatt
parent 864adb7f42
commit bc32ec3503
3 changed files with 12 additions and 11 deletions

View File

@ -63,7 +63,7 @@ extent_arena_get(const extent_t *extent) {
return NULL; return NULL;
} }
assert(arena_ind <= MALLOCX_ARENA_MAX); 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 JEMALLOC_INLINE szind_t

View File

@ -469,7 +469,7 @@ extern unsigned narenas_auto;
* Arenas that are used to service external requests. Not all elements of the * Arenas that are used to service external requests. Not all elements of the
* arenas array are necessarily used; arenas are created lazily as needed. * 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 * 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); assert(ind <= MALLOCX_ARENA_MAX);
ret = arenas[ind]; ret = (arena_t *)atomic_load_p(&arenas[ind], ATOMIC_ACQUIRE);
if (unlikely(ret == NULL)) { if (unlikely(ret == NULL)) {
ret = (arena_t *)atomic_read_p((void **)&arenas[ind]); if (init_if_missing) {
if (init_if_missing && unlikely(ret == NULL)) {
ret = arena_init(tsdn, ind, ret = arena_init(tsdn, ind,
(extent_hooks_t *)&extent_hooks_default); (extent_hooks_t *)&extent_hooks_default);
} }

View File

@ -55,10 +55,12 @@ static malloc_mutex_t arenas_lock;
* arenas[0..narenas_auto) are used for automatic multiplexing of threads and * arenas[0..narenas_auto) are used for automatic multiplexing of threads and
* arenas. arenas[narenas_auto..narenas_total) are only used if the application * arenas. arenas[narenas_auto..narenas_total) are only used if the application
* takes some action to create them and allocate from them. * takes some action to create them and allocate from them.
*
* Points to an arena_t.
*/ */
JEMALLOC_ALIGNED(CACHELINE) JEMALLOC_ALIGNED(CACHELINE)
arena_t *arenas[MALLOCX_ARENA_MAX + 1]; atomic_p_t arenas[MALLOCX_ARENA_MAX + 1];
static unsigned narenas_total; /* Use narenas_total_*(). */ static atomic_u_t narenas_total; /* Use narenas_total_*(). */
static arena_t *a0; /* arenas[0]; read-only after initialization. */ static arena_t *a0; /* arenas[0]; read-only after initialization. */
unsigned narenas_auto; /* Read-only after initialization. */ unsigned narenas_auto; /* Read-only after initialization. */
@ -363,22 +365,22 @@ bootstrap_free(void *ptr) {
void void
arena_set(unsigned ind, arena_t *arena) { arena_set(unsigned ind, arena_t *arena) {
atomic_write_p((void **)&arenas[ind], arena); atomic_store_p(&arenas[ind], arena, ATOMIC_RELEASE);
} }
static void static void
narenas_total_set(unsigned narenas) { narenas_total_set(unsigned narenas) {
atomic_write_u(&narenas_total, narenas); atomic_store_u(&narenas_total, narenas, ATOMIC_RELEASE);
} }
static void static void
narenas_total_inc(void) { narenas_total_inc(void) {
atomic_add_u(&narenas_total, 1); atomic_fetch_add_u(&narenas_total, 1, ATOMIC_RELEASE);
} }
unsigned unsigned
narenas_total_get(void) { 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. */ /* Create a new arena and insert it into the arenas array at index ind. */