mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-04-18 18:00:31 +00:00
Add a simple thread pool with a fixed number of worker threads and a shared work queue. The pool is accessed via ThreadPool::the() which lazily creates a singleton with 4 worker threads. submit() enqueues a work item and signals a condvar. Worker threads loop waiting on the condvar, picking up and executing work items. Worker threads use 8 MiB stacks to match the main thread, since the JS parser can build deep call stacks during off-thread parsing.
55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
/*
|
|
* Copyright (c) 2026, the Ladybird developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibThreading/ThreadPool.h>
|
|
|
|
static constexpr size_t THREAD_COUNT = 4;
|
|
static constexpr size_t THREAD_STACK_SIZE = 8 * MiB;
|
|
|
|
namespace Threading {
|
|
|
|
ThreadPool& ThreadPool::the()
|
|
{
|
|
static ThreadPool* instance = new ThreadPool;
|
|
return *instance;
|
|
}
|
|
|
|
ThreadPool::ThreadPool()
|
|
{
|
|
for (size_t i = 0; i < THREAD_COUNT; ++i) {
|
|
auto name = ByteString::formatted("Pool/{}", i);
|
|
auto thread = Thread::construct(name, [this]() -> intptr_t {
|
|
return worker_thread_func();
|
|
});
|
|
thread->set_stack_size(THREAD_STACK_SIZE);
|
|
thread->start();
|
|
m_threads.append(move(thread));
|
|
}
|
|
}
|
|
|
|
intptr_t ThreadPool::worker_thread_func()
|
|
{
|
|
while (true) {
|
|
Function<void()> work;
|
|
|
|
{
|
|
MutexLocker locker(m_mutex);
|
|
m_condition.wait_while([this] { return m_work_queue.is_empty(); });
|
|
work = m_work_queue.dequeue();
|
|
}
|
|
|
|
work();
|
|
}
|
|
}
|
|
|
|
void ThreadPool::submit(Function<void()> work)
|
|
{
|
|
MutexLocker locker(m_mutex);
|
|
m_work_queue.enqueue(move(work));
|
|
m_condition.signal();
|
|
}
|
|
|
|
}
|