From 4c2492e9854c220694b20d7b6b06048b089c80ec Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 22 Nov 2023 10:55:40 -0800 Subject: [PATCH] [Mammoth] Add a user-space semaphore object. --- lib/mammoth/CMakeLists.txt | 1 + lib/mammoth/include/mammoth/semaphore.h | 15 +++++++++++++++ lib/mammoth/src/semaphore.cpp | 11 +++++++++++ 3 files changed, 27 insertions(+) create mode 100644 lib/mammoth/include/mammoth/semaphore.h create mode 100644 lib/mammoth/src/semaphore.cpp diff --git a/lib/mammoth/CMakeLists.txt b/lib/mammoth/CMakeLists.txt index f8ce74a..b33c991 100644 --- a/lib/mammoth/CMakeLists.txt +++ b/lib/mammoth/CMakeLists.txt @@ -10,6 +10,7 @@ add_library(mammoth STATIC src/process.cpp src/port_client.cpp src/port_server.cpp + src/semaphore.cpp src/thread.cpp ) diff --git a/lib/mammoth/include/mammoth/semaphore.h b/lib/mammoth/include/mammoth/semaphore.h new file mode 100644 index 0000000..b22c124 --- /dev/null +++ b/lib/mammoth/include/mammoth/semaphore.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +class Semaphore { + public: + Semaphore(); + ~Semaphore(); + + void Wait(); + void Signal(); + + private: + z_cap_t semaphore_cap_; +}; diff --git a/lib/mammoth/src/semaphore.cpp b/lib/mammoth/src/semaphore.cpp new file mode 100644 index 0000000..f6e348d --- /dev/null +++ b/lib/mammoth/src/semaphore.cpp @@ -0,0 +1,11 @@ +#include "mammoth/semaphore.h" + +#include + +#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_)); }