Pārlūkot izejas kodu

Kernel: Use IntrusiveList for keeping track of InodeWatchers

Andreas Kling 4 gadi atpakaļ
vecāks
revīzija
43d6a7e74e

+ 11 - 11
Kernel/FileSystem/Inode.cpp

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
@@ -105,7 +105,7 @@ Inode::~Inode()
     all_with_lock().remove(*this);
     all_with_lock().remove(*this);
 
 
     for (auto& watcher : m_watchers) {
     for (auto& watcher : m_watchers) {
-        watcher->unregister_by_inode({}, identifier());
+        watcher.unregister_by_inode({}, identifier());
     }
     }
 }
 }
 
 
@@ -168,15 +168,15 @@ bool Inode::unbind_socket()
 void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
 void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
 {
 {
     MutexLocker locker(m_inode_lock);
     MutexLocker locker(m_inode_lock);
-    VERIFY(!m_watchers.contains(&watcher));
-    m_watchers.set(&watcher);
+    VERIFY(!m_watchers.contains(watcher));
+    m_watchers.append(watcher);
 }
 }
 
 
 void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
 void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
 {
 {
     MutexLocker locker(m_inode_lock);
     MutexLocker locker(m_inode_lock);
-    VERIFY(m_watchers.contains(&watcher));
-    m_watchers.remove(&watcher);
+    VERIFY(m_watchers.contains(watcher));
+    m_watchers.remove(watcher);
 }
 }
 
 
 NonnullRefPtr<FIFO> Inode::fifo()
 NonnullRefPtr<FIFO> Inode::fifo()
@@ -209,7 +209,7 @@ void Inode::set_metadata_dirty(bool metadata_dirty)
         // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
         // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
         //        We don't always end up on this particular code path, for instance when writing to an ext2fs file.
         //        We don't always end up on this particular code path, for instance when writing to an ext2fs file.
         for (auto& watcher : m_watchers) {
         for (auto& watcher : m_watchers) {
-            watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
+            watcher.notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
         }
         }
     }
     }
 }
 }
@@ -219,7 +219,7 @@ void Inode::did_add_child(InodeIdentifier const&, String const& name)
     MutexLocker locker(m_inode_lock);
     MutexLocker locker(m_inode_lock);
 
 
     for (auto& watcher : m_watchers) {
     for (auto& watcher : m_watchers) {
-        watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
+        watcher.notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
     }
     }
 }
 }
 
 
@@ -233,7 +233,7 @@ void Inode::did_remove_child(InodeIdentifier const&, String const& name)
     }
     }
 
 
     for (auto& watcher : m_watchers) {
     for (auto& watcher : m_watchers) {
-        watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
+        watcher.notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
     }
     }
 }
 }
 
 
@@ -241,7 +241,7 @@ void Inode::did_modify_contents()
 {
 {
     MutexLocker locker(m_inode_lock);
     MutexLocker locker(m_inode_lock);
     for (auto& watcher : m_watchers) {
     for (auto& watcher : m_watchers) {
-        watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
+        watcher.notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
     }
     }
 }
 }
 
 
@@ -249,7 +249,7 @@ void Inode::did_delete_self()
 {
 {
     MutexLocker locker(m_inode_lock);
     MutexLocker locker(m_inode_lock);
     for (auto& watcher : m_watchers) {
     for (auto& watcher : m_watchers) {
-        watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
+        watcher.notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
     }
     }
 }
 }
 
 

+ 3 - 3
Kernel/FileSystem/Inode.h

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
@@ -8,7 +8,6 @@
 #pragma once
 #pragma once
 
 
 #include <AK/Function.h>
 #include <AK/Function.h>
-#include <AK/HashTable.h>
 #include <AK/IntrusiveList.h>
 #include <AK/IntrusiveList.h>
 #include <AK/RefCounted.h>
 #include <AK/RefCounted.h>
 #include <AK/String.h>
 #include <AK/String.h>
@@ -17,6 +16,7 @@
 #include <Kernel/FileSystem/FileSystem.h>
 #include <Kernel/FileSystem/FileSystem.h>
 #include <Kernel/FileSystem/InodeIdentifier.h>
 #include <Kernel/FileSystem/InodeIdentifier.h>
 #include <Kernel/FileSystem/InodeMetadata.h>
 #include <Kernel/FileSystem/InodeMetadata.h>
+#include <Kernel/FileSystem/InodeWatcher.h>
 #include <Kernel/Forward.h>
 #include <Kernel/Forward.h>
 #include <Kernel/KResult.h>
 #include <Kernel/KResult.h>
 #include <Kernel/Mutex.h>
 #include <Kernel/Mutex.h>
@@ -119,7 +119,7 @@ private:
     InodeIndex m_index { 0 };
     InodeIndex m_index { 0 };
     WeakPtr<SharedInodeVMObject> m_shared_vmobject;
     WeakPtr<SharedInodeVMObject> m_shared_vmobject;
     RefPtr<LocalSocket> m_socket;
     RefPtr<LocalSocket> m_socket;
-    HashTable<InodeWatcher*> m_watchers;
+    InodeWatcher::List m_watchers;
     bool m_metadata_dirty { false };
     bool m_metadata_dirty { false };
     RefPtr<FIFO> m_fifo;
     RefPtr<FIFO> m_fifo;
     IntrusiveListNode<Inode> m_inode_list_node;
     IntrusiveListNode<Inode> m_inode_list_node;

+ 5 - 0
Kernel/FileSystem/InodeWatcher.h

@@ -80,6 +80,11 @@ private:
     // watch description, so they will overlap.
     // watch description, so they will overlap.
     HashMap<int, NonnullOwnPtr<WatchDescription>> m_wd_to_watches;
     HashMap<int, NonnullOwnPtr<WatchDescription>> m_wd_to_watches;
     HashMap<InodeIdentifier, WatchDescription*> m_inode_to_watches;
     HashMap<InodeIdentifier, WatchDescription*> m_inode_to_watches;
+
+    IntrusiveListNode<InodeWatcher> m_list_node;
+
+public:
+    using List = IntrusiveList<InodeWatcher, RawPtr<InodeWatcher>, &InodeWatcher::m_list_node>;
 };
 };
 
 
 }
 }