[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

View file

@ -6,7 +6,6 @@
#include "capability/capability.h"
#include "include/ztypes.h"
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/ipc_object.h"
#include "object/kernel_object.h"
#include "usr/zcall_internal.h"

View file

@ -5,7 +5,6 @@
#include <glacier/status/error.h>
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/ipc_object.h"
#include "object/kernel_object.h"

View file

@ -14,6 +14,7 @@ class KernelObject : public glcr::RefCounted<KernelObject> {
PORT = 0x6,
ENDPOINT = 0x7,
REPLY_PORT = 0x8,
MUTEX = 0x9,
};
virtual uint64_t TypeTag() = 0;

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);
}
}

55
zion/object/mutex.h Normal file
View file

@ -0,0 +1,55 @@
#pragma once
#include <glacier/container/intrusive_list.h>
#include <glacier/memory/ref_ptr.h>
#include "include/ztypes.h"
#include "object/kernel_object.h"
#include "object/thread.h"
class Mutex;
template <>
struct KernelObjectTag<Mutex> {
static const uint64_t type = KernelObject::MUTEX;
};
class Mutex : public KernelObject {
public:
uint64_t TypeTag() override { return KernelObject::MUTEX; }
static uint64_t DefaultPermissions() {
return kZionPerm_Lock | kZionPerm_Release;
}
static glcr::RefPtr<Mutex> Create();
// Attempts to lock the mutex, suspends the current thread
// until the lock is acquired.
void Lock();
// Releases the mutex and activates the next thread in the
// blocked queue if it exists.
void Release();
private:
uint8_t lock_ = 0;
glcr::IntrusiveList<Thread> blocked_threads_;
Mutex() {}
};
class MutexHolder {
public:
MutexHolder(const glcr::RefPtr<Mutex>& mutex) : mutex_(mutex) {
mutex_->Lock();
}
~MutexHolder() { mutex_->Release(); }
MutexHolder(MutexHolder&) = delete;
MutexHolder(MutexHolder&&) = delete;
private:
glcr::RefPtr<Mutex> mutex_;
};

View file

@ -5,7 +5,6 @@
#include "capability/capability.h"
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/ipc_object.h"
#include "object/kernel_object.h"
#include "object/thread.h"

View file

@ -6,9 +6,9 @@
#include "capability/capability.h"
#include "capability/capability_table.h"
#include "lib/mutex.h"
#include "object/address_space.h"
#include "object/channel.h"
#include "object/mutex.h"
#include "object/port.h"
// Forward decl due to cyclic dependency.
@ -66,7 +66,7 @@ class Process : public KernelObject {
Process();
Process(uint64_t id) : id_(id), vmas_(AddressSpace::ForRoot()) {}
Mutex mutex_{"Process"};
glcr::RefPtr<Mutex> mutex_ = Mutex::Create();
uint64_t id_;
glcr::RefPtr<AddressSpace> vmas_;

View file

@ -3,7 +3,6 @@
#include <glacier/memory/ref_ptr.h>
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "object/ipc_object.h"
#include "object/kernel_object.h"