2023-02-01 01:53:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-02-02 23:24:12 +02:00
|
|
|
#include <BAN/NoCopyMove.h>
|
2023-04-10 21:03:41 +03:00
|
|
|
#include <BAN/RefPtr.h>
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
class Process;
|
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
class Thread
|
2023-02-01 01:53:35 +02:00
|
|
|
{
|
2023-03-30 19:13:28 +03:00
|
|
|
BAN_NON_COPYABLE(Thread);
|
|
|
|
BAN_NON_MOVABLE(Thread);
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
public:
|
2023-03-09 15:25:39 +02:00
|
|
|
using entry_t = void(*)(void*);
|
|
|
|
|
2023-04-09 22:18:12 +03:00
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
NotStarted,
|
|
|
|
Executing,
|
|
|
|
Terminating,
|
|
|
|
};
|
|
|
|
|
2023-03-09 15:25:39 +02:00
|
|
|
public:
|
2023-03-30 19:13:28 +03:00
|
|
|
static BAN::ErrorOr<Thread*> create(entry_t, void* = nullptr, BAN::RefPtr<Process> = nullptr);
|
2023-02-01 01:53:35 +02:00
|
|
|
~Thread();
|
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
pid_t tid() const { return m_tid; }
|
2023-02-01 01:53:35 +02:00
|
|
|
|
|
|
|
void set_rsp(uintptr_t rsp) { m_rsp = rsp; }
|
2023-02-02 23:24:12 +02:00
|
|
|
void set_rip(uintptr_t rip) { m_rip = rip; }
|
2023-02-01 01:53:35 +02:00
|
|
|
uintptr_t rsp() const { return m_rsp; }
|
2023-02-02 23:24:12 +02:00
|
|
|
uintptr_t rip() const { return m_rip; }
|
2023-03-07 19:17:49 +02:00
|
|
|
|
2023-04-09 22:18:12 +03:00
|
|
|
void set_started() { ASSERT(m_state == State::NotStarted); m_state = State::Executing; }
|
|
|
|
State state() const { return m_state; }
|
|
|
|
void terminate() { m_state = State::Terminating; }
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
static Thread& current() ;
|
2023-03-16 12:17:04 +02:00
|
|
|
BAN::RefPtr<Process> process();
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
private:
|
2023-03-16 12:17:04 +02:00
|
|
|
Thread(pid_t tid, BAN::RefPtr<Process>);
|
|
|
|
|
2023-03-09 15:25:39 +02:00
|
|
|
BAN::ErrorOr<void> initialize(entry_t, void*);
|
2023-02-02 23:24:12 +02:00
|
|
|
void on_exit();
|
2023-02-01 01:53:35 +02:00
|
|
|
|
|
|
|
private:
|
2023-03-16 12:17:04 +02:00
|
|
|
void* m_stack_base = nullptr;
|
|
|
|
uintptr_t m_rip = 0;
|
|
|
|
uintptr_t m_rsp = 0;
|
|
|
|
const pid_t m_tid = 0;
|
2023-04-09 22:18:12 +03:00
|
|
|
State m_state { State::NotStarted };
|
2023-03-16 12:17:04 +02:00
|
|
|
BAN::RefPtr<Process> m_process;
|
2023-04-09 22:18:12 +03:00
|
|
|
|
|
|
|
friend class Scheduler;
|
2023-02-01 01:53:35 +02:00
|
|
|
};
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
}
|