[Zion] Add a mutex object with appropriate syscalls.

This commit is contained in:
Drew Galbraith 2023-10-25 14:47:45 -07:00
parent 4c2237fa72
commit 4c04f9d561
19 changed files with 160 additions and 60 deletions

30
zion/object/mutex.cpp Normal file
View file

@ -0,0 +1,30 @@
#include "object/mutex.h"
#include "scheduler/scheduler.h"
glcr::RefPtr<Mutex> Mutex::Create() {
return glcr::AdoptPtr<Mutex>(new Mutex());
}
// FIXME: We almost certainly have some race conditions
// between this and unlock where we could end up with
// a thread in the blocked_threads_ queue while noone is holding the lock.
void Mutex::Lock() {
while (__atomic_fetch_or(&lock_, 0x1, __ATOMIC_SEQ_CST) == 0x1) {
auto thread = gScheduler->CurrentThread();
thread->SetState(Thread::BLOCKED);
blocked_threads_.PushBack(thread);
gScheduler->Yield();
}
}
// FIXME: Check that this thread is the one that is holding the mutex before
// releasing it.
void Mutex::Release() {
lock_ = 0;
if (blocked_threads_.size() > 0) {
auto thread = blocked_threads_.PopFront();
thread->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(thread);
}
}