[Mammoth] Move Mutex and Semaphore to a separate folder.

This commit is contained in:
Drew Galbraith 2023-11-22 13:45:04 -08:00
parent ad5b55bf37
commit f1cbfd18b7
7 changed files with 6 additions and 6 deletions

View file

@ -0,0 +1,27 @@
#include "sync/mutex.h"
#include <zcall.h>
Mutex::Mutex(Mutex&& other) : mutex_cap_(other.mutex_cap_) {
other.mutex_cap_ = 0;
}
Mutex& Mutex::operator=(Mutex&& other) {
// TODO: Release existing mutex if it exists.
mutex_cap_ = other.mutex_cap_;
other.mutex_cap_ = 0;
return *this;
}
glcr::ErrorOr<Mutex> Mutex::Create() {
z_cap_t mutex_cap;
RET_ERR(ZMutexCreate(&mutex_cap));
return Mutex(mutex_cap);
}
glcr::ErrorCode Mutex::Lock() {
return static_cast<glcr::ErrorCode>(ZMutexLock(mutex_cap_));
}
glcr::ErrorCode Mutex::Release() {
return static_cast<glcr::ErrorCode>(ZMutexRelease(mutex_cap_));
}

21
lib/mammoth/sync/mutex.h Normal file
View file

@ -0,0 +1,21 @@
#pragma once
#include <glacier/status/error_or.h>
#include <ztypes.h>
class Mutex {
public:
Mutex(const Mutex&) = delete;
Mutex(Mutex&&);
Mutex& operator=(Mutex&&);
static glcr::ErrorOr<Mutex> Create();
glcr::ErrorCode Lock();
glcr::ErrorCode Release();
private:
z_cap_t mutex_cap_;
Mutex(z_cap_t mutex_cap) : mutex_cap_(mutex_cap) {}
};

View file

@ -0,0 +1,11 @@
#include "sync/semaphore.h"
#include <zcall.h>
#include "mammoth/debug.h"
Semaphore::Semaphore() { check(ZSemaphoreCreate(&semaphore_cap_)); }
Semaphore::~Semaphore() { check(ZCapRelease(semaphore_cap_)); }
void Semaphore::Wait() { check(ZSemaphoreWait(semaphore_cap_)); }
void Semaphore::Signal() { check(ZSemaphoreSignal(semaphore_cap_)); }

View file

@ -0,0 +1,15 @@
#pragma once
#include <ztypes.h>
class Semaphore {
public:
Semaphore();
~Semaphore();
void Wait();
void Signal();
private:
z_cap_t semaphore_cap_;
};