MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / background_thread_create_locked

Function background_thread_create_locked

deps/jemalloc/src/background_thread.c:537–589  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

535}
536
537static bool
538background_thread_create_locked(tsd_t *tsd, unsigned arena_ind) {
539 assert(have_background_thread);
540 malloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);
541
542 /* We create at most NCPUs threads. */
543 size_t thread_ind = arena_ind % max_background_threads;
544 background_thread_info_t *info = &background_thread_info[thread_ind];
545
546 bool need_new_thread;
547 malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
548 need_new_thread = background_thread_enabled() &&
549 (info->state == background_thread_stopped);
550 if (need_new_thread) {
551 background_thread_init(tsd, info);
552 }
553 malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
554 if (!need_new_thread) {
555 return false;
556 }
557 if (arena_ind != 0) {
558 /* Threads are created asynchronously by Thread 0. */
559 background_thread_info_t *t0 = &background_thread_info[0];
560 malloc_mutex_lock(tsd_tsdn(tsd), &t0->mtx);
561 assert(t0->state == background_thread_started);
562 pthread_cond_signal(&t0->cond);
563 malloc_mutex_unlock(tsd_tsdn(tsd), &t0->mtx);
564
565 return false;
566 }
567
568 pre_reentrancy(tsd, NULL);
569 /*
570 * To avoid complications (besides reentrancy), create internal
571 * background threads with the underlying pthread_create.
572 */
573 int err = background_thread_create_signals_masked(&info->thread, NULL,
574 background_thread_entry, (void *)thread_ind);
575 post_reentrancy(tsd);
576
577 if (err != 0) {
578 malloc_printf("<jemalloc>: arena 0 background thread creation "
579 "failed (%d)\n", err);
580 malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
581 info->state = background_thread_stopped;
582 n_background_threads--;
583 malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
584
585 return true;
586 }
587
588 return false;
589}
590
591/* Create a new background thread if needed. */
592bool

Callers 2

background_thread_createFunction · 0.85

Calls 10

background_thread_initFunction · 0.70
malloc_printfFunction · 0.70
tsd_tsdnFunction · 0.50
malloc_mutex_lockFunction · 0.50
malloc_mutex_unlockFunction · 0.50
pre_reentrancyFunction · 0.50
post_reentrancyFunction · 0.50

Tested by

no test coverage detected