From 34e01acca5639f0d64212d8f20b253958451d243 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Mon, 9 Aug 2021 01:03:37 -0500 Subject: [PATCH] feat(thread): add SSemaphore::Signal --- storm/thread/SSemaphore.cpp | 28 ++++++++++++++++++++++++++++ storm/thread/SSemaphore.hpp | 1 + 2 files changed, 29 insertions(+) diff --git a/storm/thread/SSemaphore.cpp b/storm/thread/SSemaphore.cpp index 4111e76..2d7b677 100644 --- a/storm/thread/SSemaphore.cpp +++ b/storm/thread/SSemaphore.cpp @@ -13,3 +13,31 @@ SSemaphore::SSemaphore(uint32_t initialCount, uint32_t maximumCount) pthread_cond_init(&this->m_cond, 0); #endif } + +int32_t SSemaphore::Signal(uint32_t count) { +#if defined(WHOA_SYSTEM_WIN) + return ReleaseSemaphore(this->m_opaqueData, count, nullptr); +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) + pthread_mutex_lock(&this->m_mutex); + + if (this->m_value1 + count > this->m_value2) { + pthread_mutex_unlock(&this->m_mutex); + + return 0; + } + + this->m_value1 += count; + + if (count <= 1) { + pthread_cond_signal(&this->m_cond); + } else { + pthread_cond_broadcast(&this->m_cond); + } + + pthread_mutex_unlock(&this->m_mutex); + + return 1; +#endif +} diff --git a/storm/thread/SSemaphore.hpp b/storm/thread/SSemaphore.hpp index a9b064d..380ad26 100644 --- a/storm/thread/SSemaphore.hpp +++ b/storm/thread/SSemaphore.hpp @@ -8,6 +8,7 @@ class SSemaphore : public SSyncObject { public: // Member functions SSemaphore(uint32_t initialCount, uint32_t maximumCount); + int32_t Signal(uint32_t count); }; #endif