diff --git a/lib/mammoth/CMakeLists.txt b/lib/mammoth/CMakeLists.txt index 6a1e348..f8ce74a 100644 --- a/lib/mammoth/CMakeLists.txt +++ b/lib/mammoth/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(mammoth STATIC src/endpoint_server.cpp src/init.cpp src/memory_region.cpp + src/mutex.cpp src/new.cpp src/process.cpp src/port_client.cpp diff --git a/lib/mammoth/include/mammoth/mutex.h b/lib/mammoth/include/mammoth/mutex.h new file mode 100644 index 0000000..1c21efd --- /dev/null +++ b/lib/mammoth/include/mammoth/mutex.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +class Mutex { + public: + Mutex(const Mutex&) = delete; + Mutex(Mutex&&); + Mutex& operator=(Mutex&&); + + static glcr::ErrorOr Create(); + + glcr::ErrorCode Lock(); + glcr::ErrorCode Release(); + + private: + z_cap_t mutex_cap_; + + Mutex(z_cap_t mutex_cap) : mutex_cap_(mutex_cap) {} +}; diff --git a/lib/mammoth/src/mutex.cpp b/lib/mammoth/src/mutex.cpp new file mode 100644 index 0000000..cca7223 --- /dev/null +++ b/lib/mammoth/src/mutex.cpp @@ -0,0 +1,23 @@ +#include "mammoth/mutex.h" + +#include + +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::Create() { + z_cap_t mutex_cap; + RET_ERR(ZMutexCreate(&mutex_cap)); + return Mutex(mutex_cap); +} + +glcr::ErrorCode Mutex::Lock() { return ZMutexLock(mutex_cap_); } +glcr::ErrorCode Mutex::Release() { return ZMutexRelease(mutex_cap_); }