2019-03-23 21:03:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Kernel/i386.h>
|
|
|
|
#include <Kernel/KResult.h>
|
2019-04-06 12:29:29 +00:00
|
|
|
#include <Kernel/LinearAddress.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-05-14 09:51:00 +00:00
|
|
|
#include <Kernel/VM/Region.h>
|
2019-03-23 21:03:17 +00:00
|
|
|
#include <AK/AKString.h>
|
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-04-20 17:23:45 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-03-23 21:03:17 +00:00
|
|
|
#include <AK/RetainPtr.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
class Alarm;
|
2019-05-03 18:15:54 +00:00
|
|
|
class FileDescriptor;
|
2019-03-23 21:03:17 +00:00
|
|
|
class Process;
|
|
|
|
class Region;
|
|
|
|
|
|
|
|
enum class ShouldUnblockThread { No = 0, Yes };
|
|
|
|
|
|
|
|
struct SignalActionData {
|
|
|
|
LinearAddress handler_or_sigaction;
|
|
|
|
dword mask { 0 };
|
|
|
|
int flags { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
class Thread : public InlineLinkedListNode<Thread> {
|
|
|
|
friend class Process;
|
|
|
|
friend class Scheduler;
|
|
|
|
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-04-20 17:29:48 +00:00
|
|
|
enum State : byte {
|
2019-03-23 21:03:17 +00:00
|
|
|
Invalid = 0,
|
|
|
|
Runnable,
|
|
|
|
Running,
|
|
|
|
Skip1SchedulerPass,
|
|
|
|
Skip0SchedulerPasses,
|
|
|
|
Dying,
|
|
|
|
Dead,
|
|
|
|
Stopped,
|
|
|
|
BlockedLurking,
|
|
|
|
BlockedSleep,
|
|
|
|
BlockedWait,
|
|
|
|
BlockedRead,
|
|
|
|
BlockedWrite,
|
|
|
|
BlockedSignal,
|
|
|
|
BlockedSelect,
|
|
|
|
BlockedConnect,
|
|
|
|
BlockedReceive,
|
|
|
|
BlockedSnoozing,
|
|
|
|
};
|
|
|
|
|
|
|
|
void did_schedule() { ++m_times_scheduled; }
|
|
|
|
dword times_scheduled() const { return m_times_scheduled; }
|
|
|
|
|
|
|
|
bool is_stopped() const { return m_state == Stopped; }
|
|
|
|
bool is_blocked() const
|
|
|
|
{
|
|
|
|
return m_state == BlockedSleep || m_state == BlockedWait || m_state == BlockedRead || m_state == BlockedWrite || m_state == BlockedSignal || m_state == BlockedSelect;
|
|
|
|
}
|
|
|
|
bool in_kernel() const { return (m_tss.cs & 0x03) == 0; }
|
|
|
|
|
|
|
|
dword frame_ptr() const { return m_tss.ebp; }
|
|
|
|
dword stack_ptr() const { return m_tss.esp; }
|
|
|
|
|
|
|
|
word selector() const { return m_far_ptr.selector; }
|
|
|
|
TSS32& tss() { return m_tss; }
|
|
|
|
State state() const { return m_state; }
|
|
|
|
dword ticks() const { return m_ticks; }
|
|
|
|
pid_t waitee_pid() const { return m_waitee_pid; }
|
|
|
|
|
2019-03-24 00:52:10 +00:00
|
|
|
void sleep(dword ticks);
|
2019-03-23 21:03:17 +00:00
|
|
|
void block(Thread::State);
|
2019-05-03 18:15:54 +00:00
|
|
|
void block(Thread::State, FileDescriptor&);
|
2019-03-23 21:03:17 +00:00
|
|
|
void unblock();
|
|
|
|
|
2019-04-13 23:29:14 +00:00
|
|
|
void set_wakeup_time(qword t) { m_wakeup_time = t; }
|
|
|
|
qword wakeup_time() const { return m_wakeup_time; }
|
2019-03-23 21:03:17 +00:00
|
|
|
void snooze_until(Alarm&);
|
2019-05-03 18:15:54 +00:00
|
|
|
KResult wait_for_connect(FileDescriptor&);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
const FarPtr& far_ptr() const { return m_far_ptr; }
|
|
|
|
|
|
|
|
bool tick();
|
|
|
|
void set_ticks_left(dword t) { m_ticks_left = t; }
|
|
|
|
dword ticks_left() const { return m_ticks_left; }
|
|
|
|
|
2019-05-17 01:43:51 +00:00
|
|
|
dword kernel_stack_base() const { return m_kernel_stack_base; }
|
2019-05-14 10:17:59 +00:00
|
|
|
dword kernel_stack_for_signal_handler_base() const { return m_kernel_stack_for_signal_handler_region ? m_kernel_stack_for_signal_handler_region->laddr().get() : 0; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
void set_selector(word s) { m_far_ptr.selector = s; }
|
|
|
|
void set_state(State s) { m_state = s; }
|
|
|
|
|
|
|
|
void send_signal(byte signal, Process* sender);
|
|
|
|
|
|
|
|
ShouldUnblockThread dispatch_one_pending_signal();
|
|
|
|
ShouldUnblockThread dispatch_signal(byte signal);
|
|
|
|
bool has_unmasked_pending_signals() const;
|
|
|
|
void terminate_due_to_signal(byte signal);
|
|
|
|
|
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();
|
|
|
|
void push_value_on_stack(dword);
|
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 };
|
|
|
|
|
|
|
|
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(Callback);
|
|
|
|
|
|
|
|
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;
|
|
|
|
dword m_ticks { 0 };
|
|
|
|
dword m_ticks_left { 0 };
|
2019-04-13 23:29:14 +00:00
|
|
|
qword m_wakeup_time { 0 };
|
2019-03-23 21:03:17 +00:00
|
|
|
dword m_times_scheduled { 0 };
|
|
|
|
dword m_pending_signals { 0 };
|
|
|
|
dword m_signal_mask { 0 };
|
2019-05-17 01:43:51 +00:00
|
|
|
dword m_kernel_stack_base { 0 };
|
2019-05-14 09:51:00 +00:00
|
|
|
RetainPtr<Region> m_kernel_stack_region;
|
2019-05-14 10:06:09 +00:00
|
|
|
RetainPtr<Region> m_kernel_stack_for_signal_handler_region;
|
2019-03-23 21:03:17 +00:00
|
|
|
pid_t m_waitee_pid { -1 };
|
2019-05-03 18:15:54 +00:00
|
|
|
RetainPtr<FileDescriptor> m_blocked_descriptor;
|
2019-03-23 21:03:17 +00:00
|
|
|
timeval m_select_timeout;
|
|
|
|
SignalActionData m_signal_action_data[32];
|
|
|
|
Region* m_signal_stack_user_region { nullptr };
|
|
|
|
Alarm* m_snoozing_alarm { nullptr };
|
|
|
|
Vector<int> m_select_read_fds;
|
|
|
|
Vector<int> m_select_write_fds;
|
|
|
|
Vector<int> m_select_exceptional_fds;
|
2019-03-27 14:27:45 +00:00
|
|
|
FPUState* m_fpu_state { nullptr };
|
2019-04-20 17:29:48 +00:00
|
|
|
State m_state { Invalid };
|
2019-03-23 21:03:17 +00:00
|
|
|
bool m_select_has_timeout { false };
|
|
|
|
bool m_has_used_fpu { false };
|
|
|
|
bool m_was_interrupted_while_blocked { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
extern InlineLinkedList<Thread>* g_threads;
|
|
|
|
|
|
|
|
const char* to_string(Thread::State);
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline void Thread::for_each_in_state(State state, Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* thread = g_threads->head(); thread;) {
|
|
|
|
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();
|
|
|
|
for (auto* thread = g_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<typename Callback>
|
|
|
|
inline void Thread::for_each(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* thread = g_threads->head(); thread;) {
|
|
|
|
auto* next_thread = thread->next();
|
|
|
|
if (callback(*thread) == IterationDecision::Abort)
|
|
|
|
return;
|
|
|
|
thread = next_thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|