#pragma once #include #include #include #include #include #include #include #include #include #include #include class Alarm; class FileDescription; class Process; class Region; class Thread; enum class ShouldUnblockThread { No = 0, Yes }; struct SignalActionData { VirtualAddress handler_or_sigaction; u32 mask { 0 }; int flags { 0 }; }; extern InlineLinkedList* g_runnable_threads; extern InlineLinkedList* g_nonrunnable_threads; class Thread : public InlineLinkedListNode { friend class Process; friend class Scheduler; public: explicit Thread(Process&); ~Thread(); static void initialize(); static void finalize_dying_threads(); static Vector all_threads(); static bool is_thread(void*); int tid() const { return m_tid; } int pid() const; Process& process() { return m_process; } const Process& process() const { return m_process; } void finalize(); enum State : u8 { Invalid = 0, Runnable, Running, Skip1SchedulerPass, Skip0SchedulerPasses, Dying, Dead, Stopped, Blocked, }; class Blocker { public: virtual ~Blocker() {} virtual bool should_unblock(Thread&, time_t now_s, long us) = 0; virtual const char* state_string() const = 0; }; class FileDescriptionBlocker : public Blocker { public: FileDescriptionBlocker(const RefPtr& description); RefPtr blocked_description() const; private: RefPtr m_blocked_description; }; class AcceptBlocker : public FileDescriptionBlocker { public: AcceptBlocker(const RefPtr& description); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Accepting"; } }; class ReceiveBlocker : public FileDescriptionBlocker { public: ReceiveBlocker(const RefPtr& description); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Receiving"; } }; class ConnectBlocker : public FileDescriptionBlocker { public: ConnectBlocker(const RefPtr& description); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Connecting"; } }; class WriteBlocker : public FileDescriptionBlocker { public: WriteBlocker(const RefPtr& description); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Writing"; } }; class ReadBlocker : public FileDescriptionBlocker { public: ReadBlocker(const RefPtr& description); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Reading"; } }; class ConditionBlocker : public Blocker { public: 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; }; class SleepBlocker : public Blocker { public: SleepBlocker(u64 wakeup_time); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Sleeping"; } private: u64 m_wakeup_time { 0 }; }; class SelectBlocker : 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); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Selecting"; } private: timeval m_select_timeout; bool m_select_has_timeout { false }; const FDVector& m_select_read_fds; const FDVector& m_select_write_fds; const FDVector& m_select_exceptional_fds; }; class WaitBlocker : public Blocker { public: WaitBlocker(int wait_options, pid_t& waitee_pid); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { return "Waiting"; } private: int m_wait_options { 0 }; pid_t& m_waitee_pid; }; class SemiPermanentBlocker : public Blocker { public: enum class Reason { Lurking, Signal, }; SemiPermanentBlocker(Reason reason); virtual bool should_unblock(Thread&, time_t, long) override; virtual const char* state_string() const override { switch (m_reason) { case Reason::Lurking: return "Lurking"; case Reason::Signal: return "Signal"; } ASSERT_NOT_REACHED(); } private: Reason m_reason; }; void did_schedule() { ++m_times_scheduled; } 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 in_kernel() const { return (m_tss.cs & 0x03) == 0; } u32 frame_ptr() const { return m_tss.ebp; } u32 stack_ptr() const { return m_tss.esp; } u16 selector() const { return m_far_ptr.selector; } TSS32& tss() { return m_tss; } State state() const { return m_state; } const char* state_string() const; u32 ticks() const { return m_ticks; } u64 sleep(u32 ticks); void block(Thread::State); void block(Blocker& blocker); void unblock(); void block_until(const char* state_string, Function&&); KResult wait_for_connect(FileDescription&); const FarPtr& far_ptr() const { return m_far_ptr; } bool tick(); void set_ticks_left(u32 t) { m_ticks_left = t; } u32 ticks_left() const { return m_ticks_left; } u32 kernel_stack_base() const { return m_kernel_stack_base; } u32 kernel_stack_for_signal_handler_base() const { return m_kernel_stack_for_signal_handler_region ? m_kernel_stack_for_signal_handler_region->vaddr().get() : 0; } void set_selector(u16 s) { m_far_ptr.selector = s; } void set_state(State); void send_signal(u8 signal, Process* sender); void consider_unblock(time_t now_sec, long now_usec); ShouldUnblockThread dispatch_one_pending_signal(); ShouldUnblockThread dispatch_signal(u8 signal); bool has_unmasked_pending_signals() const; void terminate_due_to_signal(u8 signal); bool should_ignore_signal(u8 signal) const; FPUState& fpu_state() { return *m_fpu_state; } bool has_used_fpu() const { return m_has_used_fpu; } void set_has_used_fpu(bool b) { m_has_used_fpu = b; } void set_default_signal_dispositions(); void push_value_on_stack(u32); void make_userspace_stack_for_main_thread(Vector arguments, Vector environment); void make_userspace_stack_for_secondary_thread(void* argument); Thread* clone(Process&); // For InlineLinkedList Thread* m_prev { nullptr }; Thread* m_next { nullptr }; InlineLinkedList* thread_list() { return m_thread_list; } void set_thread_list(InlineLinkedList*); template static void for_each_in_state(State, Callback); template static void for_each_living(Callback); template static void for_each_runnable(Callback); template static void for_each_nonrunnable(Callback); template static void for_each(Callback); static bool is_runnable_state(Thread::State state) { return state == Thread::State::Running || state == Thread::State::Runnable; } static InlineLinkedList* thread_list_for_state(Thread::State state) { if (is_runnable_state(state)) return g_runnable_threads; return g_nonrunnable_threads; } private: Process& m_process; int m_tid { -1 }; TSS32 m_tss; OwnPtr m_tss_to_resume_kernel; FarPtr m_far_ptr; u32 m_ticks { 0 }; u32 m_ticks_left { 0 }; u32 m_times_scheduled { 0 }; u32 m_pending_signals { 0 }; u32 m_signal_mask { 0 }; u32 m_kernel_stack_base { 0 }; RefPtr m_kernel_stack_region; RefPtr m_kernel_stack_for_signal_handler_region; SignalActionData m_signal_action_data[32]; Region* m_signal_stack_user_region { nullptr }; OwnPtr m_blocker; FPUState* m_fpu_state { nullptr }; InlineLinkedList* m_thread_list { nullptr }; State m_state { Invalid }; bool m_has_used_fpu { false }; bool m_was_interrupted_while_blocked { false }; }; HashTable& thread_table(); template inline void Thread::for_each_in_state(State state, Callback callback) { ASSERT_INTERRUPTS_DISABLED(); for (auto* thread = thread_list_for_state(state)->head(); thread;) { auto* next_thread = thread->next(); if (thread->state() == state) callback(*thread); thread = next_thread; } } template inline void Thread::for_each_living(Callback callback) { ASSERT_INTERRUPTS_DISABLED(); for (auto* thread = g_runnable_threads->head(); thread;) { auto* next_thread = thread->next(); if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying) callback(*thread); thread = next_thread; } for (auto* thread = g_nonrunnable_threads->head(); thread;) { auto* next_thread = thread->next(); if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying) callback(*thread); thread = next_thread; } } template inline void Thread::for_each(Callback callback) { ASSERT_INTERRUPTS_DISABLED(); for_each_runnable(callback); for_each_nonrunnable(callback); } template inline void Thread::for_each_runnable(Callback callback) { ASSERT_INTERRUPTS_DISABLED(); for (auto* thread = g_runnable_threads->head(); thread;) { auto* next_thread = thread->next(); if (callback(*thread) == IterationDecision::Break) return; thread = next_thread; } } template inline void Thread::for_each_nonrunnable(Callback callback) { ASSERT_INTERRUPTS_DISABLED(); for (auto* thread = g_nonrunnable_threads->head(); thread;) { auto* next_thread = thread->next(); if (callback(*thread) == IterationDecision::Break) return; thread = next_thread; } }