Skip to content
Snippets Groups Projects
Commit 0c28f56c authored by Lofar test build account's avatar Lofar test build account
Browse files

Task #5385: [mol] Added inverse ScopedLock to temporarily unlock a mutex

parent 309c8b59
No related branches found
No related tags found
No related merge requests found
......@@ -70,7 +70,11 @@ class Mutex
class ScopedLock
{
public:
ScopedLock(Mutex &);
// Locks a mutex while this objects exists.
//
// If unlock = true, the working is reversed:
// the mutex is unlocked while this object exists.
ScopedLock(Mutex &, bool unlock = false);
~ScopedLock();
private:
......@@ -78,6 +82,7 @@ class ScopedLock
ScopedLock& operator=(const ScopedLock&);
Mutex &itsMutex;
const bool itsUnlock;
};
......@@ -159,18 +164,19 @@ inline bool Mutex::trylock()
}
inline ScopedLock::ScopedLock(Mutex &mutex)
inline ScopedLock::ScopedLock(Mutex &mutex, bool unlock)
:
itsMutex(mutex)
itsMutex(mutex),
itsUnlock(unlock)
{
itsMutex.lock();
itsUnlock ? itsMutex.unlock() : itsMutex.lock();
}
inline ScopedLock::~ScopedLock()
{
try {
itsMutex.unlock();
itsUnlock ? itsMutex.lock() : itsMutex.unlock();
} catch (std::exception &) {}
}
......
......@@ -58,6 +58,13 @@ void test_simple(Mutex::Type type) {
{
ScopedLock sl(mutex);
}
{
// scoped locking and unlocking should be stackable
ScopedLock sl1(mutex, false);
ScopedLock sl2(mutex, true);
ScopedLock sl3(mutex, false);
}
}
void test_trylock(Mutex::Type type) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment