2023-05-18 12:43:53 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-06-21 16:28:42 -07:00
|
|
|
#include <glacier/container/intrusive_list.h>
|
2023-06-21 15:07:40 -07:00
|
|
|
#include <glacier/memory/ref_ptr.h>
|
2023-05-18 12:43:53 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-08-01 18:22:41 -07:00
|
|
|
#include "include/ztypes.h"
|
2023-06-06 20:13:07 -07:00
|
|
|
#include "object/kernel_object.h"
|
2023-05-29 15:50:38 -07:00
|
|
|
|
2023-05-18 12:43:53 -07:00
|
|
|
// Forward decl due to cyclic dependency.
|
|
|
|
class Process;
|
2023-06-16 14:53:57 -07:00
|
|
|
class Thread;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct KernelObjectTag<Thread> {
|
|
|
|
static const uint64_t type = KernelObject::THREAD;
|
|
|
|
};
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-06-21 16:28:42 -07:00
|
|
|
class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
|
2023-05-18 12:43:53 -07:00
|
|
|
public:
|
2023-06-16 14:53:57 -07:00
|
|
|
uint64_t TypeTag() override { return KernelObject::THREAD; }
|
2023-08-01 18:22:41 -07:00
|
|
|
static uint64_t DefaultPermissions() {
|
|
|
|
return kZionPerm_Read | kZionPerm_Write;
|
|
|
|
}
|
|
|
|
|
2023-05-29 13:51:00 -07:00
|
|
|
enum State {
|
|
|
|
UNSPECIFIED,
|
2023-06-06 16:24:03 -07:00
|
|
|
CREATED,
|
2023-05-29 13:51:00 -07:00
|
|
|
RUNNING,
|
|
|
|
RUNNABLE,
|
2023-06-20 15:36:17 -07:00
|
|
|
BLOCKED,
|
2023-05-29 13:51:00 -07:00
|
|
|
FINISHED,
|
|
|
|
};
|
2023-06-21 15:07:40 -07:00
|
|
|
static glcr::RefPtr<Thread> RootThread(Process& root_proc);
|
|
|
|
static glcr::RefPtr<Thread> Create(Process& proc, uint64_t tid);
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-30 21:27:20 -07:00
|
|
|
uint64_t tid() const { return id_; };
|
|
|
|
uint64_t pid() const;
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-30 21:27:20 -07:00
|
|
|
Process& process() { return process_; }
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-18 13:24:02 -07:00
|
|
|
uint64_t* Rsp0Ptr() { return &rsp0_; }
|
2023-05-18 16:03:09 -07:00
|
|
|
uint64_t Rsp0Start() { return rsp0_start_; }
|
2023-05-18 13:24:02 -07:00
|
|
|
|
2023-06-06 16:24:03 -07:00
|
|
|
// Switches the thread's state to runnable and enqueues it.
|
|
|
|
void Start(uint64_t entry, uint64_t arg1, uint64_t arg2);
|
|
|
|
|
2023-05-18 13:56:54 -07:00
|
|
|
// Called the first time the thread starts up.
|
|
|
|
void Init();
|
|
|
|
|
2023-05-29 13:51:00 -07:00
|
|
|
// State Management.
|
|
|
|
State GetState() { return state_; };
|
|
|
|
void SetState(State state) { state_ = state; }
|
|
|
|
void Exit();
|
|
|
|
|
2023-06-22 02:17:50 -07:00
|
|
|
void Wait();
|
|
|
|
|
2023-05-18 12:43:53 -07:00
|
|
|
private:
|
2023-06-21 15:07:40 -07:00
|
|
|
friend class glcr::MakeRefCountedFriend<Thread>;
|
2023-06-06 19:05:03 -07:00
|
|
|
Thread(Process& proc, uint64_t tid);
|
2023-05-18 13:24:02 -07:00
|
|
|
// Special constructor for the root thread only.
|
2023-05-30 21:27:20 -07:00
|
|
|
Thread(Process& proc) : process_(proc), id_(0) {}
|
|
|
|
Process& process_;
|
2023-05-18 12:43:53 -07:00
|
|
|
uint64_t id_;
|
2023-06-06 16:24:03 -07:00
|
|
|
State state_ = CREATED;
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-30 01:27:47 -07:00
|
|
|
// Startup Context for the thread.
|
|
|
|
uint64_t rip_;
|
2023-06-06 16:24:03 -07:00
|
|
|
uint64_t arg1_;
|
|
|
|
uint64_t arg2_;
|
2023-05-29 00:32:54 -07:00
|
|
|
|
2023-05-18 13:24:02 -07:00
|
|
|
// Stack pointer to take on resume.
|
|
|
|
// Stack will contain the full thread context.
|
|
|
|
uint64_t rsp0_;
|
2023-05-18 16:03:09 -07:00
|
|
|
// Stack pointer to take when returning from userspace.
|
|
|
|
// I don't think me mind clobbering the stack here.
|
|
|
|
uint64_t rsp0_start_;
|
2023-06-22 02:17:50 -07:00
|
|
|
|
|
|
|
glcr::IntrusiveList<Thread> blocked_threads_;
|
2023-05-18 12:43:53 -07:00
|
|
|
};
|