mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-11 09:18:05 +03:00
c922a7da09
This matches MutexLocker, and doesn't sound like it's a lock itself.
58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
|
#include <Kernel/Process.h>
|
|
#include <Kernel/Sections.h>
|
|
#include <Kernel/WaitQueue.h>
|
|
#include <Kernel/WorkQueue.h>
|
|
|
|
namespace Kernel {
|
|
|
|
WorkQueue* g_io_work;
|
|
|
|
UNMAP_AFTER_INIT void WorkQueue::initialize()
|
|
{
|
|
g_io_work = new WorkQueue("IO WorkQueue");
|
|
}
|
|
|
|
UNMAP_AFTER_INIT WorkQueue::WorkQueue(const char* name)
|
|
{
|
|
RefPtr<Thread> thread;
|
|
Process::create_kernel_process(thread, name, [this] {
|
|
for (;;) {
|
|
WorkItem* item;
|
|
bool have_more;
|
|
{
|
|
SpinlockLocker lock(m_lock);
|
|
item = m_items.take_first();
|
|
have_more = !m_items.is_empty();
|
|
}
|
|
if (item) {
|
|
item->function();
|
|
delete item;
|
|
|
|
if (have_more)
|
|
continue;
|
|
}
|
|
[[maybe_unused]] auto result = m_wait_queue.wait_on({});
|
|
}
|
|
});
|
|
// If we can't create the thread we're in trouble...
|
|
m_thread = thread.release_nonnull();
|
|
}
|
|
|
|
void WorkQueue::do_queue(WorkItem* item)
|
|
{
|
|
{
|
|
SpinlockLocker lock(m_lock);
|
|
m_items.append(*item);
|
|
}
|
|
m_wait_queue.wake_one();
|
|
}
|
|
|
|
}
|