Wake up background threads on demand

This change allows every allocator conforming to PAI communicate that it
deferred some work for the future. Without it if a background thread goes into
indefinite sleep, there is no way to notify it about upcoming deferred work.
This commit is contained in:
Alex Lapenkou
2021-08-18 19:24:37 -07:00
committed by Alexander Lapenkov
parent 97da57c13a
commit 8229cc77c5
21 changed files with 445 additions and 214 deletions

View File

@@ -65,6 +65,23 @@ set_background_thread_enabled(bool enabled) {
expect_d_eq(0, err, "Unexpected mallctl failure");
}
static void
wait_until_thread_is_enabled(unsigned arena_id) {
tsd_t* tsd = tsd_fetch();
bool sleeping = false;
int iterations = 0;
do {
background_thread_info_t *info =
background_thread_info_get(arena_id);
malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
sleeping = background_thread_indefinite_sleep(info);
assert_d_lt(iterations, (int)1e6,
"Waiting for a thread to start for too long");
} while (!sleeping);
}
static void
expect_purging(unsigned arena_ind, bool expect_deferred) {
size_t empty_ndirty;
@@ -132,6 +149,7 @@ TEST_BEGIN(test_hpa_background_thread_enable_disable) {
expect_purging(arena_ind, false);
set_background_thread_enabled(true);
wait_until_thread_is_enabled(arena_ind);
expect_purging(arena_ind, true);
}
TEST_END