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.
35 lines
634 B
C++
35 lines
634 B
C++
/*
|
|
* Copyright (c) 2026, the Ladybird developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Function.h>
|
|
#include <AK/Queue.h>
|
|
#include <AK/Vector.h>
|
|
#include <LibThreading/ConditionVariable.h>
|
|
#include <LibThreading/Mutex.h>
|
|
#include <LibThreading/Thread.h>
|
|
|
|
namespace Threading {
|
|
|
|
class ThreadPool {
|
|
public:
|
|
static ThreadPool& the();
|
|
|
|
void submit(Function<void()>);
|
|
|
|
private:
|
|
ThreadPool();
|
|
|
|
intptr_t worker_thread_func();
|
|
|
|
Mutex m_mutex;
|
|
ConditionVariable m_condition { m_mutex };
|
|
Queue<Function<void()>> m_work_queue;
|
|
Vector<NonnullRefPtr<Thread>> m_threads;
|
|
};
|
|
|
|
}
|