[Zion] Add a thread sleep call.

For now this can only sleep in increments of the scheduler quantum
(currently 50ms). It also uses a somewhat ineffecient way of tracking
the sleeping threads - it will scale linearly with the number of
sleeping threads.
This commit is contained in:
Drew Galbraith 2023-12-07 00:19:17 -08:00
parent 66e94ac41b
commit 8adde27d9b
9 changed files with 48 additions and 5 deletions

View File

@ -1,7 +1,10 @@
#include <mammoth/util/debug.h> #include <mammoth/util/debug.h>
#include <zcall.h>
uint64_t main(uint64_t init_port_cap) { uint64_t main(uint64_t init_port_cap) {
dbgln("testbed"); dbgln("testbed");
check(ZThreadSleep(2000));
dbgln("testbed2");
return glcr::OK; return glcr::OK;
} }

View File

@ -17,6 +17,7 @@ SYS4(ThreadStart, z_cap_t, thread_cap, uint64_t, entry, uint64_t, arg1,
uint64_t, arg2); uint64_t, arg2);
SYS0(ThreadExit); SYS0(ThreadExit);
SYS1(ThreadWait, z_cap_t, thread_cap); SYS1(ThreadWait, z_cap_t, thread_cap);
SYS1(ThreadSleep, uint64_t, millis);
SYS5(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t, SYS5(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t,
vmmo_cap, uint64_t, align, uint64_t*, vaddr); vmmo_cap, uint64_t, align, uint64_t*, vaddr);

View File

@ -19,6 +19,7 @@ const uint64_t kZionThreadCreate = 0x10;
const uint64_t kZionThreadStart = 0x11; const uint64_t kZionThreadStart = 0x11;
const uint64_t kZionThreadExit = 0x12; const uint64_t kZionThreadExit = 0x12;
const uint64_t kZionThreadWait = 0x13; const uint64_t kZionThreadWait = 0x13;
const uint64_t kZionThreadSleep = 0x14;
// Memory Calls // Memory Calls
const uint64_t kZionAddressSpaceMap = 0x21; const uint64_t kZionAddressSpaceMap = 0x21;

View File

@ -29,6 +29,7 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
RUNNING, RUNNING,
RUNNABLE, RUNNABLE,
BLOCKED, BLOCKED,
SLEEPING,
CLEANUP, CLEANUP,
FINISHED, FINISHED,
}; };
@ -69,6 +70,9 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
void Wait(); void Wait();
void SetSleepTicks(uint64_t sleep_ticks) { sleep_ticks_ = sleep_ticks; }
bool DecrementSleepTicks() { return --sleep_ticks_ == 0; }
private: private:
friend class glcr::MakeRefCountedFriend<Thread>; friend class glcr::MakeRefCountedFriend<Thread>;
Thread(Process& proc, uint64_t tid); Thread(Process& proc, uint64_t tid);
@ -79,6 +83,7 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
State state_ = CREATED; State state_ = CREATED;
bool is_kernel_ = false; bool is_kernel_ = false;
uint64_t user_stack_base_; uint64_t user_stack_base_;
uint64_t sleep_ticks_;
// Startup Context for the thread. // Startup Context for the thread.
uint64_t rip_; uint64_t rip_;

View File

@ -45,14 +45,11 @@ void Scheduler::Preempt() {
return; return;
} }
DecrementSleepingThreads();
ClearDeadThreadsFromFront(); ClearDeadThreadsFromFront();
asm volatile("cli"); asm volatile("cli");
if (current_thread_ == sleep_thread_) {
// Sleep should never be preempted. (We should yield it if another thread
// becomes scheduleable).
return;
}
if (runnable_threads_.size() == 0) { if (runnable_threads_.size() == 0) {
// Continue. // Continue.
@ -102,9 +99,33 @@ void Scheduler::Yield() {
SwapToCurrent(*prev); SwapToCurrent(*prev);
} }
void Scheduler::Sleep(uint64_t millis) {
// FIXME: Improve resolution of sleep calls.
uint64_t ticks = (millis / 50) + 1;
current_thread_->SetSleepTicks(ticks);
current_thread_->SetState(Thread::SLEEPING);
sleeping_threads_.PushBack(current_thread_);
Yield();
}
void Scheduler::ClearDeadThreadsFromFront() { void Scheduler::ClearDeadThreadsFromFront() {
while (runnable_threads_.size() > 0 && while (runnable_threads_.size() > 0 &&
runnable_threads_.PeekFront()->IsDying()) { runnable_threads_.PeekFront()->IsDying()) {
runnable_threads_.PopFront(); runnable_threads_.PopFront();
} }
} }
void Scheduler::DecrementSleepingThreads() {
auto thread = sleeping_threads_.PeekFront();
while (thread) {
if (thread->DecrementSleepTicks()) {
auto thread_next = thread->next_;
sleeping_threads_.Remove(thread);
thread->SetState(Thread::RUNNABLE);
runnable_threads_.PushBack(thread);
thread = thread_next;
} else {
thread = thread->next_;
}
}
}

View File

@ -23,18 +23,23 @@ class Scheduler {
void Preempt(); void Preempt();
void Yield(); void Yield();
void Sleep(uint64_t millis);
private: private:
bool enabled_ = false; bool enabled_ = false;
glcr::RefPtr<Thread> current_thread_; glcr::RefPtr<Thread> current_thread_;
glcr::IntrusiveList<Thread> runnable_threads_; glcr::IntrusiveList<Thread> runnable_threads_;
glcr::IntrusiveList<Thread> sleeping_threads_;
glcr::RefPtr<Thread> sleep_thread_; glcr::RefPtr<Thread> sleep_thread_;
Scheduler(); Scheduler();
void SwapToCurrent(Thread& prev); void SwapToCurrent(Thread& prev);
void ClearDeadThreadsFromFront(); void ClearDeadThreadsFromFront();
void DecrementSleepingThreads();
}; };
extern Scheduler* gScheduler; extern Scheduler* gScheduler;

View File

@ -59,6 +59,7 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
CASE(ThreadStart); CASE(ThreadStart);
CASE(ThreadExit); CASE(ThreadExit);
CASE(ThreadWait); CASE(ThreadWait);
CASE(ThreadSleep);
// syscall/address_space.h // syscall/address_space.h
CASE(AddressSpaceMap); CASE(AddressSpaceMap);
CASE(AddressSpaceUnmap); CASE(AddressSpaceUnmap);

View File

@ -53,3 +53,8 @@ glcr::ErrorCode ThreadWait(ZThreadWaitReq* req) {
thread->Wait(); thread->Wait();
return glcr::OK; return glcr::OK;
} }
glcr::ErrorCode ThreadSleep(ZThreadSleepReq* req) {
gScheduler->Sleep(req->millis);
return glcr::OK;
}

View File

@ -8,3 +8,4 @@ glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req);
glcr::ErrorCode ThreadStart(ZThreadStartReq* req); glcr::ErrorCode ThreadStart(ZThreadStartReq* req);
glcr::ErrorCode ThreadExit(ZThreadExitReq*); glcr::ErrorCode ThreadExit(ZThreadExitReq*);
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req); glcr::ErrorCode ThreadWait(ZThreadWaitReq* req);
glcr::ErrorCode ThreadSleep(ZThreadSleepReq* req);