[zion] Add a thread wait syscall
This commit is contained in:
parent
f0e8ce14a4
commit
36d82370c1
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <glacier/status/error.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
class Thread {
|
class Thread {
|
||||||
|
@ -9,6 +10,8 @@ class Thread {
|
||||||
Thread() : thread_cap_(0) {}
|
Thread() : thread_cap_(0) {}
|
||||||
Thread(Entry e, const void* arg1);
|
Thread(Entry e, const void* arg1);
|
||||||
|
|
||||||
|
[[nodiscard]] glcr::ErrorCode Join();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64_t thread_cap_;
|
uint64_t thread_cap_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace {
|
||||||
extern "C" void thread_entry(Thread::Entry entry, void* arg1) {
|
extern "C" void thread_entry(Thread::Entry entry, void* arg1) {
|
||||||
entry(arg1);
|
entry(arg1);
|
||||||
|
|
||||||
ZThreadExit();
|
(void)ZThreadExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -21,3 +21,5 @@ Thread::Thread(Entry e, const void* arg1) {
|
||||||
reinterpret_cast<uint64_t>(e),
|
reinterpret_cast<uint64_t>(e),
|
||||||
reinterpret_cast<uint64_t>(arg1)));
|
reinterpret_cast<uint64_t>(arg1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glcr::ErrorCode Thread::Join() { return ZThreadWait(thread_cap_); }
|
||||||
|
|
|
@ -96,6 +96,7 @@ SYS2(ThreadCreate, z_cap_t, proc_cap, z_cap_t*, thread_cap);
|
||||||
SYS4(ThreadStart, z_cap_t, thread_cap, uint64_t, entry, uint64_t, arg1,
|
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);
|
||||||
|
|
||||||
SYS4(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t,
|
SYS4(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t,
|
||||||
vmmo_cap, uint64_t*, vaddr);
|
vmmo_cap, uint64_t*, vaddr);
|
||||||
|
|
|
@ -17,6 +17,7 @@ const uint64_t kZionProcessSpawn = 0x2;
|
||||||
const uint64_t kZionThreadCreate = 0x10;
|
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;
|
||||||
|
|
||||||
// Memory Calls
|
// Memory Calls
|
||||||
const uint64_t kZionAddressSpaceMap = 0x21;
|
const uint64_t kZionAddressSpaceMap = 0x21;
|
||||||
|
|
|
@ -69,5 +69,18 @@ void Thread::Exit() {
|
||||||
#endif
|
#endif
|
||||||
state_ = FINISHED;
|
state_ = FINISHED;
|
||||||
process_.CheckState();
|
process_.CheckState();
|
||||||
|
while (!blocked_threads_.size() == 0) {
|
||||||
|
auto thread = blocked_threads_.PopFront();
|
||||||
|
thread->SetState(Thread::RUNNABLE);
|
||||||
|
gScheduler->Enqueue(thread);
|
||||||
|
}
|
||||||
|
gScheduler->Yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Thread::Wait() {
|
||||||
|
// FIXME: We need synchronization code here.
|
||||||
|
auto thread = gScheduler->CurrentThread();
|
||||||
|
thread->SetState(Thread::BLOCKED);
|
||||||
|
blocked_threads_.PushBack(thread);
|
||||||
gScheduler->Yield();
|
gScheduler->Yield();
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,8 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
|
||||||
void SetState(State state) { state_ = state; }
|
void SetState(State state) { state_ = state; }
|
||||||
void Exit();
|
void Exit();
|
||||||
|
|
||||||
|
void Wait();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class glcr::MakeRefCountedFriend<Thread>;
|
friend class glcr::MakeRefCountedFriend<Thread>;
|
||||||
Thread(Process& proc, uint64_t tid);
|
Thread(Process& proc, uint64_t tid);
|
||||||
|
@ -68,4 +70,6 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
|
||||||
// Stack pointer to take when returning from userspace.
|
// Stack pointer to take when returning from userspace.
|
||||||
// I don't think me mind clobbering the stack here.
|
// I don't think me mind clobbering the stack here.
|
||||||
uint64_t rsp0_start_;
|
uint64_t rsp0_start_;
|
||||||
|
|
||||||
|
glcr::IntrusiveList<Thread> blocked_threads_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -55,6 +55,7 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
|
||||||
CASE(ThreadCreate);
|
CASE(ThreadCreate);
|
||||||
CASE(ThreadStart);
|
CASE(ThreadStart);
|
||||||
CASE(ThreadExit);
|
CASE(ThreadExit);
|
||||||
|
CASE(ThreadWait);
|
||||||
// syscall/address_space.h
|
// syscall/address_space.h
|
||||||
CASE(AddressSpaceMap);
|
CASE(AddressSpaceMap);
|
||||||
// syscall/memory_object.h
|
// syscall/memory_object.h
|
||||||
|
|
|
@ -3,18 +3,18 @@
|
||||||
#include "capability/capability.h"
|
#include "capability/capability.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
|
|
||||||
z_err_t ThreadCreate(ZThreadCreateReq* req) {
|
glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req) {
|
||||||
auto& curr_proc = gScheduler->CurrentProcess();
|
auto& curr_proc = gScheduler->CurrentProcess();
|
||||||
auto cap = curr_proc.GetCapability(req->proc_cap);
|
auto cap = curr_proc.GetCapability(req->proc_cap);
|
||||||
RET_ERR(ValidateCapability<Process>(cap, ZC_PROC_SPAWN_THREAD));
|
RET_ERR(ValidateCapability<Process>(cap, ZC_PROC_SPAWN_THREAD));
|
||||||
|
|
||||||
auto parent_proc = cap->obj<Process>();
|
auto parent_proc = cap->obj<Process>();
|
||||||
auto thread = parent_proc->CreateThread();
|
auto thread = parent_proc->CreateThread();
|
||||||
*req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE);
|
*req->thread_cap = curr_proc.AddNewCapability(thread, ZC_WRITE | ZC_READ);
|
||||||
return glcr::OK;
|
return glcr::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
z_err_t ThreadStart(ZThreadStartReq* req) {
|
glcr::ErrorCode ThreadStart(ZThreadStartReq* req) {
|
||||||
auto& curr_proc = gScheduler->CurrentProcess();
|
auto& curr_proc = gScheduler->CurrentProcess();
|
||||||
auto cap = curr_proc.GetCapability(req->thread_cap);
|
auto cap = curr_proc.GetCapability(req->thread_cap);
|
||||||
RET_ERR(ValidateCapability<Thread>(cap, ZC_WRITE));
|
RET_ERR(ValidateCapability<Thread>(cap, ZC_WRITE));
|
||||||
|
@ -25,8 +25,17 @@ z_err_t ThreadStart(ZThreadStartReq* req) {
|
||||||
return glcr::OK;
|
return glcr::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
z_err_t ThreadExit(ZThreadExitReq*) {
|
glcr::ErrorCode ThreadExit(ZThreadExitReq*) {
|
||||||
auto curr_thread = gScheduler->CurrentThread();
|
auto curr_thread = gScheduler->CurrentThread();
|
||||||
curr_thread->Exit();
|
curr_thread->Exit();
|
||||||
panic("Returned from thread exit");
|
panic("Returned from thread exit");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req) {
|
||||||
|
auto& curr_proc = gScheduler->CurrentProcess();
|
||||||
|
auto cap = curr_proc.GetCapability(req->thread_cap);
|
||||||
|
RET_ERR(ValidateCapability<Thread>(cap, ZC_READ));
|
||||||
|
auto thread = cap->obj<Thread>();
|
||||||
|
thread->Wait();
|
||||||
|
return glcr::OK;
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <glacier/status/error.h>
|
||||||
|
|
||||||
#include "include/zcall.h"
|
#include "include/zcall.h"
|
||||||
|
|
||||||
z_err_t ThreadCreate(ZThreadCreateReq* req);
|
glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req);
|
||||||
z_err_t ThreadStart(ZThreadStartReq* req);
|
glcr::ErrorCode ThreadStart(ZThreadStartReq* req);
|
||||||
z_err_t ThreadExit(ZThreadExitReq*);
|
glcr::ErrorCode ThreadExit(ZThreadExitReq*);
|
||||||
|
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req);
|
||||||
|
|
Loading…
Reference in New Issue