1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 12:48:11 +00:00

Kernel: Use SpinlockProtected<T> in WorkQueue

This commit is contained in:
Andreas Kling 2021-10-26 10:44:10 +02:00
parent b443e9e1a9
commit 44b273f3ac
2 changed files with 11 additions and 12 deletions

View file

@ -1,10 +1,10 @@
/* /*
* Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <Kernel/Locking/Spinlock.h>
#include <Kernel/Process.h> #include <Kernel/Process.h>
#include <Kernel/Sections.h> #include <Kernel/Sections.h>
#include <Kernel/WaitQueue.h> #include <Kernel/WaitQueue.h>
@ -29,11 +29,10 @@ UNMAP_AFTER_INIT WorkQueue::WorkQueue(StringView name)
for (;;) { for (;;) {
WorkItem* item; WorkItem* item;
bool have_more; bool have_more;
{ m_items.with([&](auto& items) {
SpinlockLocker lock(m_lock); item = items.take_first();
item = m_items.take_first(); have_more = !items.is_empty();
have_more = !m_items.is_empty(); });
}
if (item) { if (item) {
item->function(); item->function();
delete item; delete item;
@ -50,10 +49,9 @@ UNMAP_AFTER_INIT WorkQueue::WorkQueue(StringView name)
void WorkQueue::do_queue(WorkItem* item) void WorkQueue::do_queue(WorkItem* item)
{ {
{ m_items.with([&](auto& items) {
SpinlockLocker lock(m_lock); items.append(*item);
m_items.append(*item); });
}
m_wait_queue.wake_one(); m_wait_queue.wake_one();
} }

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -8,6 +9,7 @@
#include <AK/IntrusiveList.h> #include <AK/IntrusiveList.h>
#include <Kernel/Forward.h> #include <Kernel/Forward.h>
#include <Kernel/Locking/SpinlockProtected.h>
namespace Kernel { namespace Kernel {
@ -51,8 +53,7 @@ private:
RefPtr<Thread> m_thread; RefPtr<Thread> m_thread;
WaitQueue m_wait_queue; WaitQueue m_wait_queue;
IntrusiveList<&WorkItem::m_node> m_items; SpinlockProtected<IntrusiveList<&WorkItem::m_node>> m_items;
Spinlock m_lock;
}; };
} }