From 705cd2491cc0e5bb6b9196901d4ad67c7a4aed5f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 19 Jul 2019 13:19:47 +0200 Subject: Kernel: Some small refinements to the thread blockers. Committing some things my hands did while browsing through this code. - Mark all leaf classes "final". - FileDescriptionBlocker now stores a NonnullRefPtr. - FileDescriptionBlocker::blocked_description() now returns a reference. - ConditionBlocker takes a Function&&. --- Kernel/FileSystem/FileDescription.cpp | 10 ++++--- Kernel/FileSystem/FileDescription.h | 4 +-- Kernel/Scheduler.cpp | 32 +++++++++----------- Kernel/Thread.cpp | 2 +- Kernel/Thread.h | 55 +++++++++++++++++------------------ 5 files changed, 49 insertions(+), 54 deletions(-) diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index a8a35faf35..9a02da8f8f 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -143,14 +143,16 @@ ssize_t FileDescription::write(const u8* data, ssize_t size) return nwritten; } -bool FileDescription::can_write() +bool FileDescription::can_write() const { - return m_file->can_write(*this); + // FIXME: Remove this const_cast. + return m_file->can_write(const_cast(*this)); } -bool FileDescription::can_read() +bool FileDescription::can_read() const { - return m_file->can_read(*this); + // FIXME: Remove this const_cast. + return m_file->can_read(const_cast(*this)); } ByteBuffer FileDescription::read_entire_file() diff --git a/Kernel/FileSystem/FileDescription.h b/Kernel/FileSystem/FileDescription.h index 6ef4309dec..ebf9e44e8f 100644 --- a/Kernel/FileSystem/FileDescription.h +++ b/Kernel/FileSystem/FileDescription.h @@ -36,8 +36,8 @@ public: KResult fchmod(mode_t); - bool can_read(); - bool can_write(); + bool can_read() const; + bool can_write() const; ssize_t get_dir_entries(u8* buffer, ssize_t); diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index 9bf6cded20..7bef7efc55 100644 --- a/Kernel/Scheduler.cpp +++ b/Kernel/Scheduler.cpp @@ -51,67 +51,63 @@ void Scheduler::beep() s_beep_timeout = g_uptime + 100; } -Thread::FileDescriptionBlocker::FileDescriptionBlocker(const RefPtr& description) +Thread::FileDescriptionBlocker::FileDescriptionBlocker(const FileDescription& description) : m_blocked_description(description) {} -RefPtr Thread::FileDescriptionBlocker::blocked_description() const +const FileDescription& Thread::FileDescriptionBlocker::blocked_description() const { return m_blocked_description; } -Thread::AcceptBlocker::AcceptBlocker(const RefPtr& description) +Thread::AcceptBlocker::AcceptBlocker(const FileDescription& description) : FileDescriptionBlocker(description) { } bool Thread::AcceptBlocker::should_unblock(Thread&, time_t, long) { - auto& description = *blocked_description(); - auto& socket = *description.socket(); - + auto& socket = *blocked_description().socket(); return socket.can_accept(); } -Thread::ReceiveBlocker::ReceiveBlocker(const RefPtr& description) +Thread::ReceiveBlocker::ReceiveBlocker(const FileDescription& description) : FileDescriptionBlocker(description) { } bool Thread::ReceiveBlocker::should_unblock(Thread&, time_t now_sec, long now_usec) { - auto& description = *blocked_description(); - auto& socket = *description.socket(); + auto& socket = *blocked_description().socket(); // FIXME: Block until the amount of data wanted is available. bool timed_out = now_sec > socket.receive_deadline().tv_sec || (now_sec == socket.receive_deadline().tv_sec && now_usec >= socket.receive_deadline().tv_usec); - if (timed_out || description.can_read()) + if (timed_out || blocked_description().can_read()) return true; return false; } -Thread::ConnectBlocker::ConnectBlocker(const RefPtr& description) +Thread::ConnectBlocker::ConnectBlocker(const FileDescription& description) : FileDescriptionBlocker(description) { } bool Thread::ConnectBlocker::should_unblock(Thread&, time_t, long) { - auto& description = *blocked_description(); - auto& socket = *description.socket(); + auto& socket = *blocked_description().socket(); return socket.is_connected(); } -Thread::WriteBlocker::WriteBlocker(const RefPtr& description) +Thread::WriteBlocker::WriteBlocker(const FileDescription& description) : FileDescriptionBlocker(description) { } bool Thread::WriteBlocker::should_unblock(Thread&, time_t, long) { - return blocked_description()->can_write(); + return blocked_description().can_write(); } -Thread::ReadBlocker::ReadBlocker(const RefPtr& description) +Thread::ReadBlocker::ReadBlocker(const FileDescription& description) : FileDescriptionBlocker(description) { } @@ -119,10 +115,10 @@ Thread::ReadBlocker::ReadBlocker(const RefPtr& description) bool Thread::ReadBlocker::should_unblock(Thread&, time_t, long) { // FIXME: Block until the amount of data wanted is available. - return blocked_description()->can_read(); + return blocked_description().can_read(); } -Thread::ConditionBlocker::ConditionBlocker(const char* state_string, Function &condition) +Thread::ConditionBlocker::ConditionBlocker(const char* state_string, Function&& condition) : m_block_until_condition(move(condition)) , m_state_string(state_string) { diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index b8fca99784..fafdac3536 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -110,7 +110,7 @@ void Thread::unblock() void Thread::block_until(const char* state_string, Function&& condition) { - block(state_string, condition); + block(state_string, move(condition)); } void Thread::block_helper() diff --git a/Kernel/Thread.h b/Kernel/Thread.h index bd7bbc1081..d668f5bd76 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -75,62 +75,62 @@ public: class FileDescriptionBlocker : public Blocker { public: - FileDescriptionBlocker(const RefPtr& description); - RefPtr blocked_description() const; + explicit FileDescriptionBlocker(const FileDescription&); + const FileDescription& blocked_description() const; private: - RefPtr m_blocked_description; + NonnullRefPtr m_blocked_description; }; - class AcceptBlocker : public FileDescriptionBlocker { + class AcceptBlocker final : public FileDescriptionBlocker { public: - AcceptBlocker(const RefPtr& description); + explicit AcceptBlocker(const FileDescription&); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Accepting"; } }; - class ReceiveBlocker : public FileDescriptionBlocker { + class ReceiveBlocker final : public FileDescriptionBlocker { public: - ReceiveBlocker(const RefPtr& description); + explicit ReceiveBlocker(const FileDescription&); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Receiving"; } }; - class ConnectBlocker : public FileDescriptionBlocker { + class ConnectBlocker final : public FileDescriptionBlocker { public: - ConnectBlocker(const RefPtr& description); + explicit ConnectBlocker(const FileDescription&); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Connecting"; } }; - class WriteBlocker : public FileDescriptionBlocker { + class WriteBlocker final : public FileDescriptionBlocker { public: - WriteBlocker(const RefPtr& description); + explicit WriteBlocker(const FileDescription&); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Writing"; } }; - class ReadBlocker : public FileDescriptionBlocker { + class ReadBlocker final : public FileDescriptionBlocker { public: - ReadBlocker(const RefPtr& description); + explicit ReadBlocker(const FileDescription&); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Reading"; } }; - class ConditionBlocker : public Blocker { + class ConditionBlocker final : public Blocker { public: - ConditionBlocker(const char* state_string, Function &condition); + ConditionBlocker(const char* state_string, Function&& condition); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return m_state_string; } private: Function m_block_until_condition; - const char* m_state_string; + const char* m_state_string { nullptr }; }; - class SleepBlocker : public Blocker { + class SleepBlocker final : public Blocker { public: - SleepBlocker(u64 wakeup_time); + explicit SleepBlocker(u64 wakeup_time); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Sleeping"; } @@ -138,7 +138,7 @@ public: u64 m_wakeup_time { 0 }; }; - class SelectBlocker : public Blocker { + class SelectBlocker final : public Blocker { public: typedef Vector FDVector; SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds); @@ -153,7 +153,7 @@ public: const FDVector& m_select_exceptional_fds; }; - class WaitBlocker : public Blocker { + class WaitBlocker final : public Blocker { public: WaitBlocker(int wait_options, pid_t& waitee_pid); virtual bool should_unblock(Thread&, time_t, long) override; @@ -164,7 +164,7 @@ public: pid_t& m_waitee_pid; }; - class SemiPermanentBlocker : public Blocker { + class SemiPermanentBlocker final : public Blocker { public: enum class Reason { Lurking, @@ -176,10 +176,10 @@ public: virtual const char* state_string() const override { switch (m_reason) { - case Reason::Lurking: - return "Lurking"; - case Reason::Signal: - return "Signal"; + case Reason::Lurking: + return "Lurking"; + case Reason::Signal: + return "Signal"; } ASSERT_NOT_REACHED(); } @@ -192,10 +192,7 @@ public: u32 times_scheduled() const { return m_times_scheduled; } bool is_stopped() const { return m_state == Stopped; } - bool is_blocked() const - { - return m_state == Blocked; - } + bool is_blocked() const { return m_state == Blocked; } bool in_kernel() const { return (m_tss.cs & 0x03) == 0; } u32 frame_ptr() const { return m_tss.ebp; } -- cgit v1.2.3