diff options
Diffstat (limited to 'src/zencore')
| -rw-r--r-- | src/zencore/include/zencore/thread.h | 24 | ||||
| -rw-r--r-- | src/zencore/thread.cpp | 8 |
2 files changed, 22 insertions, 10 deletions
diff --git a/src/zencore/include/zencore/thread.h b/src/zencore/include/zencore/thread.h index a9c96d422..09c25996f 100644 --- a/src/zencore/include/zencore/thread.h +++ b/src/zencore/include/zencore/thread.h @@ -25,18 +25,18 @@ void SetCurrentThreadName(std::string_view ThreadName); class RwLock { public: - ZENCORE_API void AcquireShared(); - ZENCORE_API void ReleaseShared(); + ZENCORE_API void AcquireShared() noexcept; + ZENCORE_API void ReleaseShared() noexcept; - ZENCORE_API void AcquireExclusive(); - ZENCORE_API void ReleaseExclusive(); + ZENCORE_API void AcquireExclusive() noexcept; + ZENCORE_API void ReleaseExclusive() noexcept; struct SharedLockScope { SharedLockScope(RwLock& Lock) : m_Lock(&Lock) { Lock.AcquireShared(); } ~SharedLockScope() { ReleaseNow(); } - void ReleaseNow() + void ReleaseNow() noexcept { if (m_Lock) { @@ -49,12 +49,18 @@ public: RwLock* m_Lock; }; + inline void WithSharedLock(auto&& Fun) + { + SharedLockScope $(*this); + Fun(); + } + struct ExclusiveLockScope { ExclusiveLockScope(RwLock& Lock) : m_Lock(&Lock) { Lock.AcquireExclusive(); } ~ExclusiveLockScope() { ReleaseNow(); } - void ReleaseNow() + void ReleaseNow() noexcept { if (m_Lock) { @@ -67,6 +73,12 @@ public: RwLock* m_Lock; }; + inline void WithExclusiveLock(auto&& Fun) + { + ExclusiveLockScope $(*this); + Fun(); + } + private: std::shared_mutex m_Mutex; }; diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp index 1597a7dd9..86609c210 100644 --- a/src/zencore/thread.cpp +++ b/src/zencore/thread.cpp @@ -121,25 +121,25 @@ SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName) } // namespace zen void -RwLock::AcquireShared() +RwLock::AcquireShared() noexcept { m_Mutex.lock_shared(); } void -RwLock::ReleaseShared() +RwLock::ReleaseShared() noexcept { m_Mutex.unlock_shared(); } void -RwLock::AcquireExclusive() +RwLock::AcquireExclusive() noexcept { m_Mutex.lock(); } void -RwLock::ReleaseExclusive() +RwLock::ReleaseExclusive() noexcept { m_Mutex.unlock(); } |