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-07-19 11:04:42 +00:00
|
|
|
#include <AK/IntrusiveList.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-07-19 15:21:13 +00:00
|
|
|
#include <Kernel/Scheduler.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-07-19 07:04:12 +00:00
|
|
|
#include <LibC/fd_set.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;
|
2019-07-25 19:02:19 +00:00
|
|
|
class ProcessInspectionHandle;
|
2019-03-23 21:03:17 +00:00
|
|
|
class Region;
|
|
|
|
|
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-07-19 11:04:42 +00:00
|
|
|
class Thread {
|
2019-03-23 21:03:17 +00:00
|
|
|
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; }
|
|
|
|
|
2019-07-25 19:02:19 +00:00
|
|
|
String backtrace(ProcessInspectionHandle&) const;
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
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-19 07:37:34 +00:00
|
|
|
Blocked,
|
2019-03-23 21:03:17 +00:00
|
|
|
};
|
|
|
|
|
2019-07-18 16:12:37 +00:00
|
|
|
class Blocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-18 16:12:37 +00:00
|
|
|
virtual ~Blocker() {}
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t now_s, long us) = 0;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const = 0;
|
2019-07-20 09:05:52 +00:00
|
|
|
void set_interrupted_by_signal() { m_was_interrupted_while_blocked = true; }
|
|
|
|
bool was_interrupted_by_signal() const { return m_was_interrupted_while_blocked; }
|
|
|
|
private:
|
|
|
|
bool m_was_interrupted_while_blocked { false };
|
2019-07-21 10:14:58 +00:00
|
|
|
friend class Thread;
|
|
|
|
IntrusiveListNode m_blocker_list_node;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-18 16:12:37 +00:00
|
|
|
class FileDescriptionBlocker : public Blocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
const FileDescription& blocked_description() const;
|
2019-07-18 14:22:26 +00:00
|
|
|
|
2019-07-19 11:32:56 +00:00
|
|
|
protected:
|
|
|
|
explicit FileDescriptionBlocker(const FileDescription&);
|
|
|
|
|
2019-07-18 14:22:26 +00:00
|
|
|
private:
|
2019-07-19 11:19:47 +00:00
|
|
|
NonnullRefPtr<FileDescription> m_blocked_description;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class AcceptBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
explicit AcceptBlocker(const FileDescription&);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Accepting"; }
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class ReceiveBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
explicit ReceiveBlocker(const FileDescription&);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Receiving"; }
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class ConnectBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
explicit ConnectBlocker(const FileDescription&);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Connecting"; }
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class WriteBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
explicit WriteBlocker(const FileDescription&);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Writing"; }
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class ReadBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
explicit ReadBlocker(const FileDescription&);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Reading"; }
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class ConditionBlocker final : public Blocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
ConditionBlocker(const char* state_string, Function<bool()>&& condition);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return m_state_string; }
|
2019-07-18 14:22:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Function<bool()> m_block_until_condition;
|
2019-07-19 11:19:47 +00:00
|
|
|
const char* m_state_string { nullptr };
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class SleepBlocker final : public Blocker {
|
2019-07-18 15:26:11 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
explicit SleepBlocker(u64 wakeup_time);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Sleeping"; }
|
2019-07-18 15:26:11 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
u64 m_wakeup_time { 0 };
|
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class SelectBlocker final : public Blocker {
|
2019-07-18 15:39:49 +00:00
|
|
|
public:
|
2019-07-19 07:04:12 +00:00
|
|
|
typedef Vector<int, FD_SETSIZE> FDVector;
|
|
|
|
SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds);
|
2019-07-18 15:39:49 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Selecting"; }
|
2019-07-18 15:39:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
timeval m_select_timeout;
|
|
|
|
bool m_select_has_timeout { false };
|
2019-07-19 07:04:12 +00:00
|
|
|
const FDVector& m_select_read_fds;
|
|
|
|
const FDVector& m_select_write_fds;
|
|
|
|
const FDVector& m_select_exceptional_fds;
|
2019-07-18 15:39:49 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class WaitBlocker final : public Blocker {
|
2019-07-18 16:05:19 +00:00
|
|
|
public:
|
2019-07-18 16:12:37 +00:00
|
|
|
WaitBlocker(int wait_options, pid_t& waitee_pid);
|
2019-07-18 16:05:19 +00:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Waiting"; }
|
2019-07-18 16:05:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_wait_options { 0 };
|
|
|
|
pid_t& m_waitee_pid;
|
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class SemiPermanentBlocker final : public Blocker {
|
2019-07-19 07:34:11 +00:00
|
|
|
public:
|
|
|
|
enum class Reason {
|
|
|
|
Lurking,
|
|
|
|
Signal,
|
|
|
|
};
|
|
|
|
|
|
|
|
SemiPermanentBlocker(Reason reason);
|
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override
|
|
|
|
{
|
|
|
|
switch (m_reason) {
|
2019-07-19 11:19:47 +00:00
|
|
|
case Reason::Lurking:
|
|
|
|
return "Lurking";
|
|
|
|
case Reason::Signal:
|
|
|
|
return "Signal";
|
2019-07-19 07:51:48 +00:00
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-07-19 07:34:11 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Reason m_reason;
|
|
|
|
};
|
|
|
|
|
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; }
|
2019-07-19 11:19:47 +00:00
|
|
|
bool is_blocked() const { return m_state == Blocked; }
|
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; }
|
2019-07-25 19:02:19 +00:00
|
|
|
const TSS32& tss() const { return m_tss; }
|
2019-03-23 21:03:17 +00:00
|
|
|
State state() const { return m_state; }
|
2019-07-19 07:51:48 +00:00
|
|
|
const char* state_string() const;
|
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
|
|
|
|
2019-07-20 09:05:52 +00:00
|
|
|
enum class BlockResult {
|
|
|
|
WokeNormally,
|
|
|
|
InterruptedBySignal,
|
|
|
|
};
|
|
|
|
|
2019-07-19 08:12:50 +00:00
|
|
|
template <typename T, class... Args>
|
2019-07-20 09:05:52 +00:00
|
|
|
[[nodiscard]] BlockResult block(Args&& ... args)
|
2019-07-19 08:12:50 +00:00
|
|
|
{
|
2019-07-19 15:58:45 +00:00
|
|
|
// We should never be blocking a blocked (or otherwise non-active) thread.
|
|
|
|
ASSERT(state() == Thread::Running);
|
2019-07-21 10:14:58 +00:00
|
|
|
|
2019-07-19 08:12:50 +00:00
|
|
|
T t(AK::forward<Args>(args)...);
|
2019-07-21 10:14:58 +00:00
|
|
|
m_blockers.prepend(t);
|
|
|
|
|
|
|
|
// Enter blocked state.
|
2019-07-19 15:58:45 +00:00
|
|
|
set_state(Thread::Blocked);
|
2019-07-21 10:14:58 +00:00
|
|
|
|
|
|
|
// Yield to the scheduler, and wait for us to resume unblocked.
|
|
|
|
block_helper();
|
|
|
|
|
|
|
|
// We should no longer be blocked once we woke up
|
2019-07-19 15:58:45 +00:00
|
|
|
ASSERT(state() != Thread::Blocked);
|
2019-07-21 10:14:58 +00:00
|
|
|
|
|
|
|
// We should be the first blocker, otherwise we're waking up in a
|
|
|
|
// different order than we are waiting: scheduler bug?
|
|
|
|
ASSERT(m_blockers.first() == &t);
|
|
|
|
|
|
|
|
// Remove ourselves...
|
|
|
|
m_blockers.remove(t);
|
|
|
|
|
|
|
|
// If there are still pending blockers that need to be waited for, then
|
|
|
|
// set state back to Blocked, for the next one to be handled.
|
|
|
|
// Otherwise, we're good now, and done with blocking state.
|
|
|
|
if (!m_blockers.is_empty()) {
|
|
|
|
if (!m_blockers.first()->was_interrupted_by_signal())
|
|
|
|
set_state(Thread::Blocked);
|
|
|
|
}
|
|
|
|
|
2019-07-20 09:05:52 +00:00
|
|
|
if (t.was_interrupted_by_signal())
|
|
|
|
return BlockResult::InterruptedBySignal;
|
2019-07-21 10:14:58 +00:00
|
|
|
|
2019-07-20 09:05:52 +00:00
|
|
|
return BlockResult::WokeNormally;
|
2019-07-19 08:12:50 +00:00
|
|
|
};
|
|
|
|
|
2019-07-20 09:05:52 +00:00
|
|
|
[[nodiscard]] BlockResult block_until(const char* state_string, Function<bool()>&& condition)
|
|
|
|
{
|
|
|
|
return block<ConditionBlocker>(state_string, move(condition));
|
|
|
|
}
|
|
|
|
|
2019-07-19 08:12:50 +00:00
|
|
|
void unblock();
|
2019-07-20 09:05:52 +00:00
|
|
|
|
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; }
|
2019-09-04 02:31:38 +00:00
|
|
|
u32 kernel_stack_top() const { return m_kernel_stack_top; }
|
2019-07-03 19:17:35 +00:00
|
|
|
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
|
|
|
|
2019-08-06 17:43:07 +00:00
|
|
|
void set_dump_backtrace_on_finalization() { m_dump_backtrace_on_finalization = true; }
|
|
|
|
|
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&);
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
static IterationDecision for_each_in_state(State, Callback);
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
static IterationDecision for_each_living(Callback);
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
static IterationDecision 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;
|
|
|
|
}
|
|
|
|
|
2019-07-19 11:04:42 +00:00
|
|
|
private:
|
|
|
|
IntrusiveListNode m_runnable_list_node;
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
private:
|
2019-07-19 15:21:13 +00:00
|
|
|
friend class SchedulerData;
|
2019-08-06 17:43:07 +00:00
|
|
|
String backtrace_impl() const;
|
2019-03-23 21:03:17 +00:00
|
|
|
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-09-04 02:31:38 +00:00
|
|
|
u32 m_kernel_stack_top { 0 };
|
2019-08-01 18:17:12 +00:00
|
|
|
RefPtr<Region> m_userspace_stack_region;
|
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-21 10:14:58 +00:00
|
|
|
IntrusiveList<Blocker, &Blocker::m_blocker_list_node> m_blockers;
|
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_has_used_fpu { false };
|
2019-08-06 17:43:07 +00:00
|
|
|
bool m_dump_backtrace_on_finalization { false };
|
2019-07-19 07:57:35 +00:00
|
|
|
|
|
|
|
void block_helper();
|
2019-03-23 21:03:17 +00:00
|
|
|
};
|
|
|
|
|
2019-05-18 16:31:36 +00:00
|
|
|
HashTable<Thread*>& thread_table();
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
template<typename Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
inline IterationDecision Thread::for_each_living(Callback callback)
|
2019-03-23 21:03:17 +00:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-07-19 10:22:46 +00:00
|
|
|
return Thread::for_each([callback](Thread& thread) -> IterationDecision {
|
|
|
|
if (thread.state() != Thread::State::Dead && thread.state() != Thread::State::Dying)
|
|
|
|
return callback(thread);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-03-23 21:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
inline IterationDecision Thread::for_each(Callback callback)
|
2019-03-23 21:03:17 +00:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-07-19 15:21:13 +00:00
|
|
|
auto ret = Scheduler::for_each_runnable(callback);
|
2019-07-19 10:16:00 +00:00
|
|
|
if (ret == IterationDecision::Break)
|
|
|
|
return ret;
|
2019-07-19 15:21:13 +00:00
|
|
|
return Scheduler::for_each_nonrunnable(callback);
|
2019-05-18 16:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2019-07-19 15:21:13 +00:00
|
|
|
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
|
2019-05-18 16:31:36 +00:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-07-19 15:21:13 +00:00
|
|
|
auto new_callback = [=](Thread& thread) -> IterationDecision {
|
|
|
|
if (thread.state() == state)
|
|
|
|
return callback(thread);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
};
|
|
|
|
if (is_runnable_state(state))
|
|
|
|
return Scheduler::for_each_runnable(new_callback);
|
|
|
|
return Scheduler::for_each_nonrunnable(new_callback);
|
2019-03-23 21:03:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 19:02:19 +00:00
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const Thread& value)
|
|
|
|
{
|
|
|
|
return stream << "Thread{" << &value << "}(" << value.pid() << ":" << value.tid() << ")";
|
|
|
|
}
|
2019-08-07 18:43:54 +00:00
|
|
|
|
|
|
|
struct SchedulerData {
|
|
|
|
typedef IntrusiveList<Thread, &Thread::m_runnable_list_node> ThreadList;
|
|
|
|
|
|
|
|
ThreadList m_runnable_threads;
|
|
|
|
ThreadList m_nonrunnable_threads;
|
|
|
|
|
|
|
|
ThreadList& thread_list_for_state(Thread::State state)
|
|
|
|
{
|
|
|
|
if (Thread::is_runnable_state(state))
|
|
|
|
return m_runnable_threads;
|
|
|
|
return m_nonrunnable_threads;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline IterationDecision Scheduler::for_each_runnable(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
auto& tl = g_scheduler_data->m_runnable_threads;
|
|
|
|
for (auto it = tl.begin(); it != tl.end();) {
|
2019-08-17 09:25:32 +00:00
|
|
|
auto& thread = *it;
|
2019-08-07 18:43:54 +00:00
|
|
|
it = ++it;
|
2019-08-17 09:25:32 +00:00
|
|
|
if (callback(thread) == IterationDecision::Break)
|
2019-08-07 18:43:54 +00:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline IterationDecision Scheduler::for_each_nonrunnable(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
auto& tl = g_scheduler_data->m_nonrunnable_threads;
|
|
|
|
for (auto it = tl.begin(); it != tl.end();) {
|
2019-08-17 09:25:32 +00:00
|
|
|
auto& thread = *it;
|
2019-08-07 18:43:54 +00:00
|
|
|
it = ++it;
|
2019-08-17 09:25:32 +00:00
|
|
|
if (callback(thread) == IterationDecision::Break)
|
2019-08-07 18:43:54 +00:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|