| 602 | } |
| 603 | |
| 604 | bool |
| 605 | background_threads_enable(tsd_t *tsd) { |
| 606 | assert(n_background_threads == 0); |
| 607 | assert(background_thread_enabled()); |
| 608 | malloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock); |
| 609 | |
| 610 | VARIABLE_ARRAY(bool, marked, max_background_threads); |
| 611 | unsigned i, nmarked; |
| 612 | for (i = 0; i < max_background_threads; i++) { |
| 613 | marked[i] = false; |
| 614 | } |
| 615 | nmarked = 0; |
| 616 | /* Thread 0 is required and created at the end. */ |
| 617 | marked[0] = true; |
| 618 | /* Mark the threads we need to create for thread 0. */ |
| 619 | unsigned n = narenas_total_get(); |
| 620 | for (i = 1; i < n; i++) { |
| 621 | if (marked[i % max_background_threads] || |
| 622 | arena_get(tsd_tsdn(tsd), i, false) == NULL) { |
| 623 | continue; |
| 624 | } |
| 625 | background_thread_info_t *info = &background_thread_info[ |
| 626 | i % max_background_threads]; |
| 627 | malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx); |
| 628 | assert(info->state == background_thread_stopped); |
| 629 | background_thread_init(tsd, info); |
| 630 | malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx); |
| 631 | marked[i % max_background_threads] = true; |
| 632 | if (++nmarked == max_background_threads) { |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | return background_thread_create_locked(tsd, 0); |
| 638 | } |
| 639 | |
| 640 | bool |
| 641 | background_threads_disable(tsd_t *tsd) { |
no test coverage detected