mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-04 15:45:25 +00:00
When adding tests for `ThreadPool` a handful of deadlocks can be observed when worker threads wait on `m_work_available`. The first deadlock is in the destruction of `ThreadPool` where it is possible for a worker thread to be in the process of acquiring `m_mutex` when the broadcast to `m_work_available` in the destructor happens. This causes the destructor to hang on joining the thread which is now perpetually waiting on `m_work_available`. This is solved by repeatedly broadcasting on `m_work_available` until the thread to join exits. The second deadlock occurs when the final signal to `m_work_done` is missed by the wait in `wait_for_all`. At this point all workers are in the hot loop of attempting to get work from the work queue, however since there is no work remaining all workers end up waiting on `m_work_available`. At this point the `wait_for_all` call is also waiting on `m_work_done`, which will never be signalled again as all workers are waiting on `m_work_available`. This requires 2 changes to fix, the first is that workers will signal `m_done_work` before waiting on `m_work_available`. The second change is to acquire `m_mutex` before checking the wait conditions as done when using `wait_while`.
4.4 KiB
4.4 KiB