소스 검색

Kernel: Use SpinlockProtected<T> in WorkQueue

Andreas Kling 3 년 전
부모
커밋
44b273f3ac
2개의 변경된 파일11개의 추가작업 그리고 12개의 파일을 삭제
  1. 8 10
      Kernel/WorkQueue.cpp
  2. 3 2
      Kernel/WorkQueue.h

+ 8 - 10
Kernel/WorkQueue.cpp

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

+ 3 - 2
Kernel/WorkQueue.h

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