From 665f0575164899bfec3c5c1558d579b7f309d421 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Wed, 28 Dec 2022 15:43:57 -0600 Subject: [PATCH] feat(thread): add SCritSect constructor --- storm/thread/SCritSect.cpp | 14 ++++++++++++++ storm/thread/SCritSect.hpp | 1 + test/Thread.cpp | 17 +++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/storm/thread/SCritSect.cpp b/storm/thread/SCritSect.cpp index 360fa19..2dc6e9d 100644 --- a/storm/thread/SCritSect.cpp +++ b/storm/thread/SCritSect.cpp @@ -1,5 +1,19 @@ #include "storm/thread/SCritSect.hpp" +SCritSect::SCritSect() { +#if defined(WHOA_SYSTEM_WIN) + InitializeCriticalSection(&this->m_opaqueData); +#endif + +#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + + pthread_mutex_init(&this->m_mutex, &attr); +#endif +} + void SCritSect::Enter() { #if defined(WHOA_SYSTEM_WIN) EnterCriticalSection(&this->m_opaqueData); diff --git a/storm/thread/SCritSect.hpp b/storm/thread/SCritSect.hpp index 6adae04..552a871 100644 --- a/storm/thread/SCritSect.hpp +++ b/storm/thread/SCritSect.hpp @@ -21,6 +21,7 @@ class SCritSect { #endif // Member functions + SCritSect(); void Enter(void); void Leave(void); }; diff --git a/test/Thread.cpp b/test/Thread.cpp index 5e2fa04..f8f5e2d 100644 --- a/test/Thread.cpp +++ b/test/Thread.cpp @@ -7,3 +7,20 @@ TEST_CASE("SGetCurrentThreadId", "[thread]") { CHECK(threadId > 0); } } + +TEST_CASE("SCritSect::Enter", "[thread]") { + SECTION("locks critical section") { + SCritSect critSect; + critSect.Enter(); + SUCCEED(); + } +} + +TEST_CASE("SCritSect::Leave", "[thread]") { + SECTION("unlocks critical section") { + SCritSect critSect; + critSect.Enter(); + critSect.Leave(); + SUCCEED(); + } +}