Skip to content

Commit aa8084c

Browse files
authored
enh(ScopedLockWithUnlock): make it more alike std::unique_lock (#4652)
1 parent 6d2b266 commit aa8084c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Foundation/include/Poco/Mutex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Foundation_API Mutex: private MutexImpl
5252
{
5353
public:
5454
using ScopedLock = Poco::ScopedLock<Mutex>;
55+
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<Mutex>;
5556

5657
Mutex();
5758
/// creates the Mutex.
@@ -107,6 +108,7 @@ class Foundation_API FastMutex: private FastMutexImpl
107108
{
108109
public:
109110
using ScopedLock = Poco::ScopedLock<FastMutex>;
111+
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<FastMutex>;
110112

111113
FastMutex();
112114
/// creates the Mutex.
@@ -165,6 +167,7 @@ class Foundation_API SpinlockMutex
165167
{
166168
public:
167169
using ScopedLock = Poco::ScopedLock<SpinlockMutex>;
170+
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<SpinlockMutex>;
168171

169172
SpinlockMutex();
170173
/// Creates the SpinlockMutex.
@@ -209,6 +212,7 @@ class Foundation_API NullMutex
209212
{
210213
public:
211214
using ScopedLock = Poco::ScopedLock<NullMutex>;
215+
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<NullMutex>;
212216

213217
NullMutex()
214218
/// Creates the NullMutex.

Foundation/include/Poco/NamedMutex.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Foundation_API NamedMutex: private NamedMutexImpl
5454
{
5555
public:
5656
using ScopedLock = Poco::ScopedLock<NamedMutex>;
57+
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<NamedMutex>;
5758

5859
NamedMutex(const std::string& name);
5960
/// creates the Mutex.

Foundation/include/Poco/ScopedLock.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@ class ScopedLockWithUnlock
7777
public:
7878
explicit ScopedLockWithUnlock(M& mutex): _pMutex(&mutex)
7979
{
80+
poco_assert(_pMutex != nullptr);
81+
8082
_pMutex->lock();
83+
_locked = true;
8184
}
8285

8386
ScopedLockWithUnlock(M& mutex, long milliseconds): _pMutex(&mutex)
8487
{
88+
poco_assert(_pMutex != nullptr);
89+
8590
_pMutex->lock(milliseconds);
91+
_locked = true;
8692
}
8793

8894
~ScopedLockWithUnlock()
@@ -97,17 +103,29 @@ class ScopedLockWithUnlock
97103
}
98104
}
99105

106+
void lock()
107+
{
108+
poco_assert(_pMutex != nullptr);
109+
poco_assert(_locked == false);
110+
111+
_pMutex->lock();
112+
_locked = true;
113+
}
114+
100115
void unlock()
101116
{
102-
if (_pMutex)
117+
if (_locked)
103118
{
119+
poco_assert(_pMutex != nullptr);
120+
104121
_pMutex->unlock();
105-
_pMutex = 0;
122+
_locked = false;
106123
}
107124
}
108125

109126
private:
110127
M* _pMutex;
128+
bool _locked = false;
111129

112130
ScopedLockWithUnlock();
113131
ScopedLockWithUnlock(const ScopedLockWithUnlock&);

0 commit comments

Comments
 (0)