Skip to content

Truly independent web browser

AK+LibThreading: Let pool threads give back memory kept for later jobs

Andreas Kling78dd5d500055
diff --git a/AK/FFIHelpers.h b/AK/FFIHelpers.h
index aa4b0698139..a45f2ed9069 100644
--- a/AK/FFIHelpers.h
+++ b/AK/FFIHelpers.h
@@ -30,6 +30,10 @@ void ladybird_dealloc(void*, size_t alignment);
// Gives the memory the calling thread freed back to the system (see ak_kmalloc_collect()).
void ladybird_release_unused_allocator_memory();
+// Sets what ak_release_idle_thread_memory() calls. The function gives back the memory that the calling thread keeps
+// for its next jobs, and does nothing on a thread that keeps none.
+void ladybird_set_idle_thread_memory_release(void (*)());
+
// For a thread that frees all of its memory when it ends: while one runs, what the allocator purges goes back to the
// system right away.
void ladybird_did_start_short_lived_thread();
diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rs
index 4d3c1ae487c..c449c53c608 100644
--- a/AK/Rust/src/lib.rs
+++ b/AK/Rust/src/lib.rs
@@ -118,6 +118,7 @@ unsafe extern "C" {
) -> u8;
fn ladybird_encode_base64_to_utf16(data: *const u8, length: usize, url: bool, omit_padding: bool) -> usize;
fn ladybird_release_unused_allocator_memory();
+ fn ladybird_set_idle_thread_memory_release(release: extern "C" fn());
fn ladybird_convert_to_decimal_exponential_form(
value: f64,
sign: *mut bool,
@@ -530,6 +531,13 @@ pub fn release_unused_allocator_memory() {
unsafe { ladybird_release_unused_allocator_memory() }
}
+/// Sets the function that a thread pool calls on a thread that goes idle. It gives back the memory that the calling
+/// thread keeps outside the allocator for its next jobs, and does nothing on a thread that keeps none.
+pub fn set_idle_thread_memory_release(release: extern "C" fn()) {
+ // SAFETY: Any thread may set the function, which stays valid for as long as the process runs.
+ unsafe { ladybird_set_idle_thread_memory_release(release) }
+}
+
/// Maps in, in one go, the pages of the room `vector` has past its elements for `additional` more, for writes about to
/// fill it: a vector a large write grows into fresh memory then takes its pages at once rather than a fault at a time.
/// Where the system cannot, the writes map them in as they come.
diff --git a/AK/kmalloc.cpp b/AK/kmalloc.cpp
index 7e8ec472ff2..f20d5c5d3c8 100644
--- a/AK/kmalloc.cpp
+++ b/AK/kmalloc.cpp
@@ -210,3 +210,16 @@ extern "C" void ladybird_did_end_short_lived_thread()
}
#endif
+
+static Atomic<void (*)(), AK::MemoryOrder::memory_order_relaxed> s_idle_thread_memory_release { nullptr };
+
+extern "C" void ladybird_set_idle_thread_memory_release(void (*release)())
+{
+ s_idle_thread_memory_release.store(release);
+}
+
+void ak_release_idle_thread_memory()
+{
+ if (auto* release = s_idle_thread_memory_release.load())
+ release();
+}
diff --git a/AK/kmalloc.h b/AK/kmalloc.h
index 9517b892c27..b6ba185e65e 100644
--- a/AK/kmalloc.h
+++ b/AK/kmalloc.h
@@ -31,6 +31,9 @@ void ak_kfree(void* ptr);
[[nodiscard]] void* ak_krealloc(HeapPartition, void* ptr, size_t size);
[[nodiscard]] size_t ak_kmalloc_usable_size(void const* ptr);
void ak_kmalloc_collect();
+// Gives back the memory that the calling thread keeps outside the allocator for its next jobs, for a thread that goes
+// idle (see ladybird_set_idle_thread_memory_release()).
+void ak_release_idle_thread_memory();
[[nodiscard]] inline void* kcalloc(size_t count, size_t size)
{
diff --git a/Libraries/LibThreading/ThreadPool.cpp b/Libraries/LibThreading/ThreadPool.cpp
index d77a52506c2..802f17b8c95 100644
--- a/Libraries/LibThreading/ThreadPool.cpp
+++ b/Libraries/LibThreading/ThreadPool.cpp
@@ -57,6 +57,7 @@ intptr_t ThreadPool::worker_thread_func()
}
if (should_collect) {
+ ak_release_idle_thread_memory();
ak_kmalloc_collect();
collected_since_last_work = true;
continue;