From aa8084c6a02c17f64dc706f9e0b98f73d793fd0f Mon Sep 17 00:00:00 2001 From: siren186 <765495939@qq.com> Date: Fri, 30 Aug 2024 04:03:33 +0800 Subject: [PATCH] enh(ScopedLockWithUnlock): make it more alike std::unique_lock (#4652) --- Foundation/include/Poco/Mutex.h | 4 ++++ Foundation/include/Poco/NamedMutex.h | 1 + Foundation/include/Poco/ScopedLock.h | 22 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Foundation/include/Poco/Mutex.h b/Foundation/include/Poco/Mutex.h index a7a4100fa..512cdbd6a 100644 --- a/Foundation/include/Poco/Mutex.h +++ b/Foundation/include/Poco/Mutex.h @@ -52,6 +52,7 @@ class Foundation_API Mutex: private MutexImpl { public: using ScopedLock = Poco::ScopedLock; + using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock; Mutex(); /// creates the Mutex. @@ -107,6 +108,7 @@ class Foundation_API FastMutex: private FastMutexImpl { public: using ScopedLock = Poco::ScopedLock; + using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock; FastMutex(); /// creates the Mutex. @@ -165,6 +167,7 @@ class Foundation_API SpinlockMutex { public: using ScopedLock = Poco::ScopedLock; + using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock; SpinlockMutex(); /// Creates the SpinlockMutex. @@ -209,6 +212,7 @@ class Foundation_API NullMutex { public: using ScopedLock = Poco::ScopedLock; + using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock; NullMutex() /// Creates the NullMutex. diff --git a/Foundation/include/Poco/NamedMutex.h b/Foundation/include/Poco/NamedMutex.h index 197fe38fb..f6d1c9c18 100644 --- a/Foundation/include/Poco/NamedMutex.h +++ b/Foundation/include/Poco/NamedMutex.h @@ -54,6 +54,7 @@ class Foundation_API NamedMutex: private NamedMutexImpl { public: using ScopedLock = Poco::ScopedLock; + using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock; NamedMutex(const std::string& name); /// creates the Mutex. diff --git a/Foundation/include/Poco/ScopedLock.h b/Foundation/include/Poco/ScopedLock.h index 4b1f5ce71..eef753635 100644 --- a/Foundation/include/Poco/ScopedLock.h +++ b/Foundation/include/Poco/ScopedLock.h @@ -77,12 +77,18 @@ class ScopedLockWithUnlock public: explicit ScopedLockWithUnlock(M& mutex): _pMutex(&mutex) { + poco_assert(_pMutex != nullptr); + _pMutex->lock(); + _locked = true; } ScopedLockWithUnlock(M& mutex, long milliseconds): _pMutex(&mutex) { + poco_assert(_pMutex != nullptr); + _pMutex->lock(milliseconds); + _locked = true; } ~ScopedLockWithUnlock() @@ -97,17 +103,29 @@ public: } } + void lock() + { + poco_assert(_pMutex != nullptr); + poco_assert(_locked == false); + + _pMutex->lock(); + _locked = true; + } + void unlock() { - if (_pMutex) + if (_locked) { + poco_assert(_pMutex != nullptr); + _pMutex->unlock(); - _pMutex = 0; + _locked = false; } } private: M* _pMutex; + bool _locked = false; ScopedLockWithUnlock(); ScopedLockWithUnlock(const ScopedLockWithUnlock&);