diff options
Diffstat (limited to 'src/zencore/include')
| -rw-r--r-- | src/zencore/include/zencore/thread.h | 24 |
1 files changed, 18 insertions, 6 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; }; |