From a500c34d45f74af47658935c14dcaee741015f3e Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sun, 26 Mar 2023 23:07:48 -0500 Subject: [PATCH] feat(thread): add CCritSect --- storm/Thread.hpp | 1 + storm/thread/CCritSect.cpp | 41 ++++++++++++++++++++++++++++++++++++++ storm/thread/CCritSect.hpp | 31 ++++++++++++++++++++++++++++ test/Thread.cpp | 17 ++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 storm/thread/CCritSect.cpp create mode 100644 storm/thread/CCritSect.hpp diff --git a/storm/Thread.hpp b/storm/Thread.hpp index ed94567..4c34d8d 100644 --- a/storm/Thread.hpp +++ b/storm/Thread.hpp @@ -1,6 +1,7 @@ #ifndef STORM_THREAD_HPP #define STORM_THREAD_HPP +#include "storm/thread/CCritSect.hpp" #include "storm/thread/CSRWLock.hpp" #include "storm/thread/SCritSect.hpp" #include "storm/thread/SEvent.hpp" diff --git a/storm/thread/CCritSect.cpp b/storm/thread/CCritSect.cpp new file mode 100644 index 0000000..592dd76 --- /dev/null +++ b/storm/thread/CCritSect.cpp @@ -0,0 +1,41 @@ +#include "storm/thread/CCritSect.hpp" + +CCritSect::CCritSect() { +#if defined(WHOA_SYSTEM_WIN) + InitializeCriticalSection(&this->m_critsect); +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) + pthread_mutex_init(&this->m_critsect, nullptr); +#endif +} + +CCritSect::~CCritSect() { +#if defined(WHOA_SYSTEM_WIN) + DeleteCriticalSection(&this->m_critsect); +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) + pthread_mutex_destroy(&this->m_critsect); +#endif +} + +void CCritSect::Enter() { +#if defined(WHOA_SYSTEM_WIN) + EnterCriticalSection(&this->m_critsect); +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) + pthread_mutex_lock(&this->m_critsect); +#endif +} + +void CCritSect::Leave() { +#if defined(WHOA_SYSTEM_WIN) + LeaveCriticalSection(&this->m_critsect); +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) + pthread_mutex_unlock(&this->m_critsect); +#endif +} diff --git a/storm/thread/CCritSect.hpp b/storm/thread/CCritSect.hpp new file mode 100644 index 0000000..662eda9 --- /dev/null +++ b/storm/thread/CCritSect.hpp @@ -0,0 +1,31 @@ +#ifndef STORM_THREAD_C_CRIT_SECT_HPP +#define STORM_THREAD_C_CRIT_SECT_HPP + +#if defined(WHOA_SYSTEM_WIN) +#include +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) +#include +#endif + +class CCritSect { + public: + // Member functions + CCritSect(); + ~CCritSect(); + void Enter(); + void Leave(); + + private: + // Member variables +#if defined(WHOA_SYSTEM_WIN) + CRITICAL_SECTION m_critsect; +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) + pthread_mutex_t m_critsect; +#endif +}; + +#endif diff --git a/test/Thread.cpp b/test/Thread.cpp index 3055aed..4cd8c6d 100644 --- a/test/Thread.cpp +++ b/test/Thread.cpp @@ -6,6 +6,23 @@ uint32_t threadProc(void* param) { return 0; }; +TEST_CASE("CCritSect::Enter", "[thread]") { + SECTION("locks critical section") { + CCritSect critSect; + critSect.Enter(); + SUCCEED(); + } +} + +TEST_CASE("CCritSect::Leave", "[thread]") { + SECTION("unlocks critical section") { + CCritSect critSect; + critSect.Enter(); + critSect.Leave(); + SUCCEED(); + } +} + TEST_CASE("SGetCurrentThreadId", "[thread]") { SECTION("returns thread id") { uintptr_t threadId = SGetCurrentThreadId();