2019-03-23 21:03:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/AKString.h>
|
2019-07-14 12:51:54 +00:00
|
|
|
#include <AK/Function.h>
|
2019-03-23 21:03:17 +00:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-04-20 17:23:45 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-06-21 16:45:35 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2019-03-23 21:03:17 +00:00
|
|
|
#include <AK/Vector.h>
|
2019-06-07 18:02:01 +00:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/KResult.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
|
|
|
#include <Kernel/VM/Region.h>
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
class Alarm;
|
2019-06-07 07:36:51 +00:00
|
|
|
class FileDescription;
|
2019-03-23 21:03:17 +00:00
|
|
|
class Process;
|
|
|
|
class Region;
|
2019-05-18 16:31:36 +00:00
|
|
|
class Thread;
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum class ShouldUnblockThread {
|
2019-05-28 09:53:16 +00:00
|
|
|
No = 0,
|
|
|
|
Yes
|
|
|
|
};
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
struct SignalActionData {
|
2019-06-07 10:56:50 +00:00
|
|
|
VirtualAddress handler_or_sigaction;
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 mask { 0 };
|
2019-03-23 21:03:17 +00:00
|
|
|
int flags { 0 };
|
|
|
|
};
|
|
|
|
|
2019-05-18 16:31:36 +00:00
|
|
|
extern InlineLinkedList<Thread>* g_runnable_threads;
|
|
|
|
extern InlineLinkedList<Thread>* g_nonrunnable_threads;
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
class Thread : public InlineLinkedListNode<Thread> {
|
|
|
|
friend class Process;
|
|
|
|
friend class Scheduler;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
public:
|
|
|
|
explicit Thread(Process&);
|
|
|
|
~Thread();
|
|
|
|
|
|
|
|
static void initialize();
|
|
|
|
static void finalize_dying_threads();
|
|
|
|
|
2019-03-23 22:50:34 +00:00
|
|
|
static Vector<Thread*> all_threads();
|
2019-04-17 10:41:51 +00:00
|
|
|
static bool is_thread(void*);
|
2019-03-23 22:50:34 +00:00
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
int tid() const { return m_tid; }
|
|
|
|
int pid() const;
|
|
|
|
|
|
|
|
Process& process() { return m_process; }
|
|
|
|
const Process& process() const { return m_process; }
|
|
|
|
|
|
|
|
void finalize();
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
enum State : u8 {
|
2019-03-23 21:03:17 +00:00
|
|
|
Invalid = 0,
|
|
|
|
Runnable,
|
|
|
|
Running,
|
|
|
|
Skip1SchedulerPass,
|
|
|
|
Skip0SchedulerPasses,
|
|
|
|
Dying,
|
|
|
|
Dead,
|
|
|
|
Stopped,
|
2019-07-13 18:14:39 +00:00
|
|
|
|
|
|
|
__Begin_Blocked_States__,
|
2019-03-23 21:03:17 +00:00
|
|
|
BlockedLurking,
|
|
|
|
BlockedSignal,
|
2019-07-14 12:51:54 +00:00
|
|
|
BlockedCondition,
|
2019-07-13 18:14:39 +00:00
|
|
|
__End_Blocked_States__
|
2019-03-23 21:03:17 +00:00
|
|
|
};
|
|
|
|
|
2019-07-18 14:22:26 +00:00
|
|
|
class ThreadBlocker {
|
|
|
|
public:
|
|
|
|
virtual ~ThreadBlocker() {}
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t now_s, long us) = 0;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadBlockerFileDescription : public ThreadBlocker {
|
|
|
|
public:
|
|
|
|
ThreadBlockerFileDescription(const RefPtr<FileDescription>& description);
|
|
|
|
RefPtr<FileDescription> blocked_description() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<FileDescription> m_blocked_description;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadBlockerAccept : public ThreadBlockerFileDescription {
|
|
|
|
public:
|
|
|
|
ThreadBlockerAccept(const RefPtr<FileDescription>& description);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadBlockerReceive : public ThreadBlockerFileDescription {
|
|
|
|
public:
|
|
|
|
ThreadBlockerReceive(const RefPtr<FileDescription>& description);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadBlockerConnect : public ThreadBlockerFileDescription {
|
|
|
|
public:
|
|
|
|
ThreadBlockerConnect(const RefPtr<FileDescription>& description);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadBlockerWrite : public ThreadBlockerFileDescription {
|
|
|
|
public:
|
|
|
|
ThreadBlockerWrite(const RefPtr<FileDescription>& description);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadBlockerRead : public ThreadBlockerFileDescription {
|
|
|
|
public:
|
|
|
|
ThreadBlockerRead(const RefPtr<FileDescription>& description);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadBlockerCondition : public ThreadBlocker {
|
|
|
|
public:
|
|
|
|
ThreadBlockerCondition(Function<bool()> &condition);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-18 14:22:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Function<bool()> m_block_until_condition;
|
|
|
|
};
|
|
|
|
|
2019-07-18 15:26:11 +00:00
|
|
|
class ThreadBlockerSleep : public ThreadBlocker {
|
|
|
|
public:
|
|
|
|
ThreadBlockerSleep(u64 wakeup_time);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-18 15:26:11 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
u64 m_wakeup_time { 0 };
|
|
|
|
};
|
|
|
|
|
2019-07-18 15:39:49 +00:00
|
|
|
class ThreadBlockerSelect : public ThreadBlocker {
|
|
|
|
public:
|
|
|
|
ThreadBlockerSelect(const timeval& tv, bool select_has_timeout, const Vector<int>& read_fds, const Vector<int>& write_fds, const Vector<int>& except_fds);
|
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
timeval m_select_timeout;
|
|
|
|
bool m_select_has_timeout { false };
|
|
|
|
const Vector<int>& m_select_read_fds;
|
|
|
|
const Vector<int>& m_select_write_fds;
|
|
|
|
const Vector<int>& m_select_exceptional_fds;
|
|
|
|
};
|
|
|
|
|
2019-07-18 16:05:19 +00:00
|
|
|
class ThreadBlockerWait : public ThreadBlocker {
|
|
|
|
public:
|
|
|
|
ThreadBlockerWait(int wait_options, pid_t& waitee_pid);
|
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_wait_options { 0 };
|
|
|
|
pid_t& m_waitee_pid;
|
|
|
|
};
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
void did_schedule() { ++m_times_scheduled; }
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 times_scheduled() const { return m_times_scheduled; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
bool is_stopped() const { return m_state == Stopped; }
|
|
|
|
bool is_blocked() const
|
|
|
|
{
|
2019-07-13 18:14:39 +00:00
|
|
|
return m_state > __Begin_Blocked_States__ && m_state < __End_Blocked_States__;
|
2019-03-23 21:03:17 +00:00
|
|
|
}
|
|
|
|
bool in_kernel() const { return (m_tss.cs & 0x03) == 0; }
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 frame_ptr() const { return m_tss.ebp; }
|
|
|
|
u32 stack_ptr() const { return m_tss.esp; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u16 selector() const { return m_far_ptr.selector; }
|
2019-03-23 21:03:17 +00:00
|
|
|
TSS32& tss() { return m_tss; }
|
|
|
|
State state() const { return m_state; }
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 ticks() const { return m_ticks; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-07-18 15:26:11 +00:00
|
|
|
u64 sleep(u32 ticks);
|
2019-03-23 21:03:17 +00:00
|
|
|
void block(Thread::State);
|
2019-07-18 14:22:26 +00:00
|
|
|
void block(ThreadBlocker& blocker);
|
2019-03-23 21:03:17 +00:00
|
|
|
void unblock();
|
|
|
|
|
2019-07-14 12:51:54 +00:00
|
|
|
void block_until(Function<bool()>&&);
|
2019-06-07 07:36:51 +00:00
|
|
|
KResult wait_for_connect(FileDescription&);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
const FarPtr& far_ptr() const { return m_far_ptr; }
|
|
|
|
|
|
|
|
bool tick();
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_ticks_left(u32 t) { m_ticks_left = t; }
|
|
|
|
u32 ticks_left() const { return m_ticks_left; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
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; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_selector(u16 s) { m_far_ptr.selector = s; }
|
2019-05-18 16:31:36 +00:00
|
|
|
void set_state(State);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void send_signal(u8 signal, Process* sender);
|
2019-07-18 12:10:28 +00:00
|
|
|
void consider_unblock(time_t now_sec, long now_usec);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
ShouldUnblockThread dispatch_one_pending_signal();
|
2019-07-03 19:17:35 +00:00
|
|
|
ShouldUnblockThread dispatch_signal(u8 signal);
|
2019-03-23 21:03:17 +00:00
|
|
|
bool has_unmasked_pending_signals() const;
|
2019-07-03 19:17:35 +00:00
|
|
|
void terminate_due_to_signal(u8 signal);
|
2019-07-08 16:59:48 +00:00
|
|
|
bool should_ignore_signal(u8 signal) const;
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-03-27 14:27:45 +00:00
|
|
|
FPUState& fpu_state() { return *m_fpu_state; }
|
2019-03-23 21:03:17 +00:00
|
|
|
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();
|
2019-07-03 19:17:35 +00:00
|
|
|
void push_value_on_stack(u32);
|
2019-03-23 21:59:08 +00:00
|
|
|
void make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment);
|
|
|
|
void make_userspace_stack_for_secondary_thread(void* argument);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
Thread* clone(Process&);
|
|
|
|
|
|
|
|
// For InlineLinkedList
|
|
|
|
Thread* m_prev { nullptr };
|
|
|
|
Thread* m_next { nullptr };
|
|
|
|
|
2019-05-18 16:31:36 +00:00
|
|
|
InlineLinkedList<Thread>* thread_list() { return m_thread_list; }
|
2019-05-18 18:07:00 +00:00
|
|
|
void set_thread_list(InlineLinkedList<Thread>*);
|
2019-05-18 16:31:36 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename Callback>
|
|
|
|
static void for_each_in_state(State, Callback);
|
|
|
|
template<typename Callback>
|
|
|
|
static void for_each_living(Callback);
|
|
|
|
template<typename Callback>
|
|
|
|
static void for_each_runnable(Callback);
|
|
|
|
template<typename Callback>
|
|
|
|
static void for_each_nonrunnable(Callback);
|
|
|
|
template<typename Callback>
|
|
|
|
static void for_each(Callback);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-05-18 16:31:36 +00:00
|
|
|
static bool is_runnable_state(Thread::State state)
|
|
|
|
{
|
|
|
|
return state == Thread::State::Running || state == Thread::State::Runnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static InlineLinkedList<Thread>* thread_list_for_state(Thread::State state)
|
|
|
|
{
|
|
|
|
if (is_runnable_state(state))
|
|
|
|
return g_runnable_threads;
|
|
|
|
return g_nonrunnable_threads;
|
|
|
|
}
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
private:
|
|
|
|
Process& m_process;
|
|
|
|
int m_tid { -1 };
|
|
|
|
TSS32 m_tss;
|
2019-04-20 17:23:45 +00:00
|
|
|
OwnPtr<TSS32> m_tss_to_resume_kernel;
|
2019-03-23 21:03:17 +00:00
|
|
|
FarPtr m_far_ptr;
|
2019-07-03 19:17:35 +00:00
|
|
|
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 };
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Region> m_kernel_stack_region;
|
|
|
|
RefPtr<Region> m_kernel_stack_for_signal_handler_region;
|
2019-03-23 21:03:17 +00:00
|
|
|
SignalActionData m_signal_action_data[32];
|
|
|
|
Region* m_signal_stack_user_region { nullptr };
|
2019-07-18 14:22:26 +00:00
|
|
|
OwnPtr<ThreadBlocker> m_blocker;
|
2019-03-27 14:27:45 +00:00
|
|
|
FPUState* m_fpu_state { nullptr };
|
2019-05-18 16:31:36 +00:00
|
|
|
InlineLinkedList<Thread>* m_thread_list { nullptr };
|
2019-04-20 17:29:48 +00:00
|
|
|
State m_state { Invalid };
|
2019-03-23 21:03:17 +00:00
|
|
|
bool m_has_used_fpu { false };
|
|
|
|
bool m_was_interrupted_while_blocked { false };
|
|
|
|
};
|
|
|
|
|
2019-05-18 16:31:36 +00:00
|
|
|
HashTable<Thread*>& thread_table();
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
const char* to_string(Thread::State);
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline void Thread::for_each_in_state(State state, Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-05-18 16:31:36 +00:00
|
|
|
for (auto* thread = thread_list_for_state(state)->head(); thread;) {
|
2019-03-23 21:03:17 +00:00
|
|
|
auto* next_thread = thread->next();
|
|
|
|
if (thread->state() == state)
|
|
|
|
callback(*thread);
|
|
|
|
thread = next_thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline void Thread::for_each_living(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-05-18 16:31:36 +00:00
|
|
|
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;) {
|
2019-03-23 21:03:17 +00:00
|
|
|
auto* next_thread = thread->next();
|
|
|
|
if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying)
|
|
|
|
callback(*thread);
|
|
|
|
thread = next_thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline void Thread::for_each(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-05-18 16:31:36 +00:00
|
|
|
for_each_runnable(callback);
|
|
|
|
for_each_nonrunnable(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline void Thread::for_each_runnable(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* thread = g_runnable_threads->head(); thread;) {
|
2019-03-23 21:03:17 +00:00
|
|
|
auto* next_thread = thread->next();
|
2019-06-07 15:13:23 +00:00
|
|
|
if (callback(*thread) == IterationDecision::Break)
|
2019-03-23 21:03:17 +00:00
|
|
|
return;
|
|
|
|
thread = next_thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:31:36 +00:00
|
|
|
template<typename Callback>
|
|
|
|
inline void Thread::for_each_nonrunnable(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* thread = g_nonrunnable_threads->head(); thread;) {
|
|
|
|
auto* next_thread = thread->next();
|
2019-06-07 15:13:23 +00:00
|
|
|
if (callback(*thread) == IterationDecision::Break)
|
2019-05-18 16:31:36 +00:00
|
|
|
return;
|
|
|
|
thread = next_thread;
|
|
|
|
}
|
|
|
|
}
|