mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-01 09:24:55 +02:00
enh(ScopedLockWithUnlock): make it more alike std::unique_lock (#4652)
This commit is contained in:
parent
6d2b26645a
commit
aa8084c6a0
@ -52,6 +52,7 @@ class Foundation_API Mutex: private MutexImpl
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using ScopedLock = Poco::ScopedLock<Mutex>;
|
using ScopedLock = Poco::ScopedLock<Mutex>;
|
||||||
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<Mutex>;
|
||||||
|
|
||||||
Mutex();
|
Mutex();
|
||||||
/// creates the Mutex.
|
/// creates the Mutex.
|
||||||
@ -107,6 +108,7 @@ class Foundation_API FastMutex: private FastMutexImpl
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using ScopedLock = Poco::ScopedLock<FastMutex>;
|
using ScopedLock = Poco::ScopedLock<FastMutex>;
|
||||||
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<FastMutex>;
|
||||||
|
|
||||||
FastMutex();
|
FastMutex();
|
||||||
/// creates the Mutex.
|
/// creates the Mutex.
|
||||||
@ -165,6 +167,7 @@ class Foundation_API SpinlockMutex
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using ScopedLock = Poco::ScopedLock<SpinlockMutex>;
|
using ScopedLock = Poco::ScopedLock<SpinlockMutex>;
|
||||||
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<SpinlockMutex>;
|
||||||
|
|
||||||
SpinlockMutex();
|
SpinlockMutex();
|
||||||
/// Creates the SpinlockMutex.
|
/// Creates the SpinlockMutex.
|
||||||
@ -209,6 +212,7 @@ class Foundation_API NullMutex
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using ScopedLock = Poco::ScopedLock<NullMutex>;
|
using ScopedLock = Poco::ScopedLock<NullMutex>;
|
||||||
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<NullMutex>;
|
||||||
|
|
||||||
NullMutex()
|
NullMutex()
|
||||||
/// Creates the NullMutex.
|
/// Creates the NullMutex.
|
||||||
|
@ -54,6 +54,7 @@ class Foundation_API NamedMutex: private NamedMutexImpl
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using ScopedLock = Poco::ScopedLock<NamedMutex>;
|
using ScopedLock = Poco::ScopedLock<NamedMutex>;
|
||||||
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<NamedMutex>;
|
||||||
|
|
||||||
NamedMutex(const std::string& name);
|
NamedMutex(const std::string& name);
|
||||||
/// creates the Mutex.
|
/// creates the Mutex.
|
||||||
|
@ -77,12 +77,18 @@ class ScopedLockWithUnlock
|
|||||||
public:
|
public:
|
||||||
explicit ScopedLockWithUnlock(M& mutex): _pMutex(&mutex)
|
explicit ScopedLockWithUnlock(M& mutex): _pMutex(&mutex)
|
||||||
{
|
{
|
||||||
|
poco_assert(_pMutex != nullptr);
|
||||||
|
|
||||||
_pMutex->lock();
|
_pMutex->lock();
|
||||||
|
_locked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScopedLockWithUnlock(M& mutex, long milliseconds): _pMutex(&mutex)
|
ScopedLockWithUnlock(M& mutex, long milliseconds): _pMutex(&mutex)
|
||||||
{
|
{
|
||||||
|
poco_assert(_pMutex != nullptr);
|
||||||
|
|
||||||
_pMutex->lock(milliseconds);
|
_pMutex->lock(milliseconds);
|
||||||
|
_locked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
~ScopedLockWithUnlock()
|
~ScopedLockWithUnlock()
|
||||||
@ -97,17 +103,29 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lock()
|
||||||
|
{
|
||||||
|
poco_assert(_pMutex != nullptr);
|
||||||
|
poco_assert(_locked == false);
|
||||||
|
|
||||||
|
_pMutex->lock();
|
||||||
|
_locked = true;
|
||||||
|
}
|
||||||
|
|
||||||
void unlock()
|
void unlock()
|
||||||
{
|
{
|
||||||
if (_pMutex)
|
if (_locked)
|
||||||
{
|
{
|
||||||
|
poco_assert(_pMutex != nullptr);
|
||||||
|
|
||||||
_pMutex->unlock();
|
_pMutex->unlock();
|
||||||
_pMutex = 0;
|
_locked = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
M* _pMutex;
|
M* _pMutex;
|
||||||
|
bool _locked = false;
|
||||||
|
|
||||||
ScopedLockWithUnlock();
|
ScopedLockWithUnlock();
|
||||||
ScopedLockWithUnlock(const ScopedLockWithUnlock&);
|
ScopedLockWithUnlock(const ScopedLockWithUnlock&);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user