diff options
author | Andreas Kling <kling@serenityos.org> | 2021-08-23 02:09:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-23 02:13:04 +0200 |
commit | 7006cb82bde1dbcd9465186fd31aa884e679f92c (patch) | |
tree | 9504d4c1ae3d4d936e6f5c68af0168c8164c347d | |
parent | 39474830a94fd812dca51ebe622751e364dc1044 (diff) | |
download | serenity-7006cb82bde1dbcd9465186fd31aa884e679f92c.zip |
Kernel: Rename Blocker::not_blocking(bool) to something more descriptive
Namely, will_unblock_immediately_without_blocking(Reason).
This virtual function is called on a blocker *before any block occurs*,
if it turns out that we don't need to block the thread after all.
This can happens for one of two reasons:
- UnblockImmediatelyReason::UnblockConditionAlreadyMet
We don't need to block the thread because the condition for
unblocking it is already met.
- UnblockImmediatelyReason::TimeoutInThePast
We don't need to block the thread because a timeout was specified
and that timeout is already in the past.
This patch does not introduce any behavior changes, it's only meant to
clarify this part of the blocking logic.
-rw-r--r-- | Kernel/FileSystem/Plan9FileSystem.cpp | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/Plan9FileSystem.h | 2 | ||||
-rw-r--r-- | Kernel/Net/Routing.cpp | 6 | ||||
-rw-r--r-- | Kernel/Thread.h | 27 | ||||
-rw-r--r-- | Kernel/ThreadBlockers.cpp | 28 |
5 files changed, 35 insertions, 30 deletions
diff --git a/Kernel/FileSystem/Plan9FileSystem.cpp b/Kernel/FileSystem/Plan9FileSystem.cpp index 7bedfd5997..0b4da5b8e0 100644 --- a/Kernel/FileSystem/Plan9FileSystem.cpp +++ b/Kernel/FileSystem/Plan9FileSystem.cpp @@ -425,7 +425,7 @@ bool Plan9FS::Blocker::unblock(u16 tag) return unblock(); } -void Plan9FS::Blocker::not_blocking(bool) +void Plan9FS::Blocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason) { { SpinlockLocker lock(m_lock); diff --git a/Kernel/FileSystem/Plan9FileSystem.h b/Kernel/FileSystem/Plan9FileSystem.h index 30788b5d06..36148d536f 100644 --- a/Kernel/FileSystem/Plan9FileSystem.h +++ b/Kernel/FileSystem/Plan9FileSystem.h @@ -91,7 +91,7 @@ private: } virtual StringView state_string() const override { return "Waiting"sv; } virtual Type blocker_type() const override { return Type::Plan9FS; } - virtual void not_blocking(bool) override; + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override; const NonnullRefPtr<ReceiveCompletion>& completion() const { return m_completion; } u16 tag() const { return m_completion->tag; } diff --git a/Kernel/Net/Routing.cpp b/Kernel/Net/Routing.cpp index fbd752fc9a..e756237de7 100644 --- a/Kernel/Net/Routing.cpp +++ b/Kernel/Net/Routing.cpp @@ -26,7 +26,7 @@ public: virtual Type blocker_type() const override { return Type::Routing; } virtual bool should_block() override { return m_should_block; } - virtual void not_blocking(bool) override; + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override; bool unblock(bool from_add_blocker, const IPv4Address& ip_addr, const MACAddress& addr) { @@ -90,9 +90,9 @@ ARPTableBlocker::ARPTableBlocker(IPv4Address ip_addr, Optional<MACAddress>& addr m_should_block = false; } -void ARPTableBlocker::not_blocking(bool timeout_in_past) +void ARPTableBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason) { - VERIFY(timeout_in_past || !m_should_block); + VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast || !m_should_block); auto addr = arp_table().with_shared([&](const auto& table) -> auto { return table.get(ip_addr()); }); diff --git a/Kernel/Thread.h b/Kernel/Thread.h index d223c6212e..c15c14ecd4 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -300,7 +300,14 @@ public: virtual Type blocker_type() const = 0; virtual const BlockTimeout& override_timeout(const BlockTimeout& timeout) { return timeout; } virtual bool can_be_interrupted() const { return true; } - virtual void not_blocking(bool) = 0; + + enum class UnblockImmediatelyReason { + UnblockConditionAlreadyMet, + TimeoutInThePast, + }; + + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) = 0; + virtual void was_unblocked(bool did_timeout) { if (did_timeout) { @@ -515,7 +522,7 @@ public: virtual StringView state_string() const override { return "Joining"sv; } virtual bool can_be_interrupted() const override { return false; } virtual bool should_block() override { return !m_join_error && m_should_block; } - virtual void not_blocking(bool) override; + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override; bool unblock(void*, bool); @@ -534,7 +541,7 @@ public: virtual Type blocker_type() const override { return Type::Queue; } virtual StringView state_string() const override { return m_block_reason.is_null() ? m_block_reason : "Queue"sv; } - virtual void not_blocking(bool) override { } + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override { } virtual bool should_block() override { @@ -556,7 +563,7 @@ public: virtual Type blocker_type() const override { return Type::Futex; } virtual StringView state_string() const override { return "Futex"sv; } - virtual void not_blocking(bool) override { } + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override { } virtual bool should_block() override { @@ -620,7 +627,7 @@ public: const FileDescription& blocked_description() const; virtual bool unblock(bool, void*) override; - virtual void not_blocking(bool) override; + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override; protected: explicit FileDescriptionBlocker(FileDescription&, BlockFlags, BlockFlags&); @@ -670,7 +677,7 @@ public: virtual StringView state_string() const override { return "Sleeping"sv; } virtual Type blocker_type() const override { return Type::Sleep; } virtual const BlockTimeout& override_timeout(const BlockTimeout&) override; - virtual void not_blocking(bool) override; + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override; virtual void was_unblocked(bool) override; virtual Thread::BlockResult block_result() override; @@ -694,7 +701,7 @@ public: virtual ~SelectBlocker(); virtual bool unblock(bool, void*) override; - virtual void not_blocking(bool) override; + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override; virtual void was_unblocked(bool) override; virtual StringView state_string() const override { return "Selecting"sv; } @@ -718,7 +725,7 @@ public: virtual StringView state_string() const override { return "Waiting"sv; } virtual Type blocker_type() const override { return Type::Wait; } virtual bool should_block() override { return m_should_block; } - virtual void not_blocking(bool) override; + virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override; virtual void was_unblocked(bool) override; bool unblock(Process& process, UnblockFlags flags, u8 signal, bool from_add_blocker); @@ -881,7 +888,7 @@ public: m_blocker = &blocker; if (!blocker.should_block()) { // Don't block if the wake condition is already met - blocker.not_blocking(false); + blocker.will_unblock_immediately_without_blocking(Blocker::UnblockImmediatelyReason::UnblockConditionAlreadyMet); m_blocker = nullptr; m_in_block = false; return BlockResult::NotBlocked; @@ -903,7 +910,7 @@ public: }); if (!timer_was_added) { // Timeout is already in the past - blocker.not_blocking(true); + blocker.will_unblock_immediately_without_blocking(Blocker::UnblockImmediatelyReason::TimeoutInThePast); m_blocker = nullptr; m_in_block = false; return BlockResult::InterruptedByTimeout; diff --git a/Kernel/ThreadBlockers.cpp b/Kernel/ThreadBlockers.cpp index 5f553df9af..116bd46e21 100644 --- a/Kernel/ThreadBlockers.cpp +++ b/Kernel/ThreadBlockers.cpp @@ -87,18 +87,17 @@ Thread::JoinBlocker::JoinBlocker(Thread& joinee, KResult& try_join_result, void* } } -void Thread::JoinBlocker::not_blocking(bool timeout_in_past) +void Thread::JoinBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason) { if (!m_should_block) { - // add_to_blocker_set returned false, so unblock was already called - VERIFY(!timeout_in_past); + VERIFY(reason == UnblockImmediatelyReason::UnblockConditionAlreadyMet); return; } // If we should have blocked but got here it must have been that the // timeout was already in the past. So we need to ask the BlockerSet // to supply us the information. We cannot hold the lock as unblock // could be called by the BlockerSet at any time! - VERIFY(timeout_in_past); + VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast); m_joinee->m_join_blocker_set.try_unblock(*this); } @@ -217,18 +216,17 @@ bool Thread::FileDescriptionBlocker::unblock(bool from_add_blocker, void*) return true; } -void Thread::FileDescriptionBlocker::not_blocking(bool timeout_in_past) +void Thread::FileDescriptionBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason) { if (!m_should_block) { - // add_to_blocker_set returned false, so unblock was already called - VERIFY(!timeout_in_past); + VERIFY(reason == UnblockImmediatelyReason::UnblockConditionAlreadyMet); return; } // If we should have blocked but got here it must have been that the // timeout was already in the past. So we need to ask the BlockerSet // to supply us the information. We cannot hold the lock as unblock // could be called by the BlockerSet at any time! - VERIFY(timeout_in_past); + VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast); // Just call unblock here because we will query the file description // for the data and don't need any input from the FileBlockerSet. @@ -307,11 +305,11 @@ auto Thread::SleepBlocker::override_timeout(const BlockTimeout& timeout) -> cons return m_deadline; } -void Thread::SleepBlocker::not_blocking(bool timeout_in_past) +void Thread::SleepBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason) { // SleepBlocker::should_block should always return true, so timeout // in the past is the only valid case when this function is called - VERIFY(timeout_in_past); + VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast); calculate_remaining(); } @@ -360,14 +358,14 @@ Thread::SelectBlocker::~SelectBlocker() fd_entry.description->blocker_set().remove_blocker(*this, &fd_entry); } -void Thread::SelectBlocker::not_blocking(bool timeout_in_past) +void Thread::SelectBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason) { // Either the timeout was in the past or we didn't add all blockers - VERIFY(timeout_in_past || !m_should_block); + VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast || !m_should_block); SpinlockLocker lock(m_lock); if (!m_should_block || !m_did_unblock) { m_did_unblock = true; - if (!timeout_in_past) { + if (reason == UnblockImmediatelyReason::UnblockConditionAlreadyMet) { auto count = collect_unblocked_flags(); VERIFY(count > 0); } @@ -626,9 +624,9 @@ Thread::WaitBlocker::WaitBlocker(int wait_options, idtype_t id_type, pid_t id, K m_should_block = false; } -void Thread::WaitBlocker::not_blocking(bool timeout_in_past) +void Thread::WaitBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason) { - VERIFY(timeout_in_past || !m_should_block); + VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast || !m_should_block); if (!m_error) Process::current().wait_blocker_set().try_unblock(*this); } |