@@ -70,7 +70,7 @@ unsigned opt_narenas = 0;
7070unsigned ncpus ;
7171
7272/* Protects arenas initialization. */
73- static malloc_mutex_t arenas_lock ;
73+ malloc_mutex_t arenas_lock ;
7474/*
7575 * Arenas that are used to service external requests. Not all elements of the
7676 * arenas array are necessarily used; arenas are created lazily as needed.
@@ -335,13 +335,35 @@ arena_init_locked(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {
335335 return arena ;
336336}
337337
338+ static void
339+ arena_new_create_background_thread (tsdn_t * tsdn , unsigned ind ) {
340+ if (ind == 0 ) {
341+ return ;
342+ }
343+ /* background_thread_create() handles reentrancy internally. */
344+ if (have_background_thread ) {
345+ bool err ;
346+ malloc_mutex_lock (tsdn , & background_thread_lock );
347+ err = background_thread_create (tsdn_tsd (tsdn ), ind );
348+ malloc_mutex_unlock (tsdn , & background_thread_lock );
349+ if (err ) {
350+ malloc_printf ("<jemalloc>: error in background thread "
351+ "creation for arena %u. Abort.\n" , ind );
352+ abort ();
353+ }
354+ }
355+ }
356+
338357arena_t *
339358arena_init (tsdn_t * tsdn , unsigned ind , extent_hooks_t * extent_hooks ) {
340359 arena_t * arena ;
341360
342361 malloc_mutex_lock (tsdn , & arenas_lock );
343362 arena = arena_init_locked (tsdn , ind , extent_hooks );
344363 malloc_mutex_unlock (tsdn , & arenas_lock );
364+
365+ arena_new_create_background_thread (tsdn , ind );
366+
345367 return arena ;
346368}
347369
@@ -475,6 +497,7 @@ arena_choose_hard(tsd_t *tsd, bool internal) {
475497
476498 if (narenas_auto > 1 ) {
477499 unsigned i , j , choose [2 ], first_null ;
500+ bool is_new_arena [2 ];
478501
479502 /*
480503 * Determine binding for both non-internal and internal
@@ -486,6 +509,7 @@ arena_choose_hard(tsd_t *tsd, bool internal) {
486509
487510 for (j = 0 ; j < 2 ; j ++ ) {
488511 choose [j ] = 0 ;
512+ is_new_arena [j ] = false;
489513 }
490514
491515 first_null = narenas_auto ;
@@ -545,13 +569,23 @@ arena_choose_hard(tsd_t *tsd, bool internal) {
545569 & arenas_lock );
546570 return NULL ;
547571 }
572+ is_new_arena [j ] = true;
548573 if (!!j == internal ) {
549574 ret = arena ;
550575 }
551576 }
552577 arena_bind (tsd , choose [j ], !!j );
553578 }
554579 malloc_mutex_unlock (tsd_tsdn (tsd ), & arenas_lock );
580+
581+ for (j = 0 ; j < 2 ; j ++ ) {
582+ if (is_new_arena [j ]) {
583+ assert (choose [j ] > 0 );
584+ arena_new_create_background_thread (
585+ tsd_tsdn (tsd ), choose [j ]);
586+ }
587+ }
588+
555589 } else {
556590 ret = arena_get (tsd_tsdn (tsd ), 0 , false);
557591 arena_bind (tsd , 0 , false);
0 commit comments