From ac1eaff6ecb48b54ceafbddcc90e2b23ebb8039d Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Dec 2015 06:13:49 +0100 Subject: [PATCH] thread/Mutex: add class ScopeUnlock() --- src/thread/Mutex.hxx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/thread/Mutex.hxx b/src/thread/Mutex.hxx index bc4deebdd..1756bddf3 100644 --- a/src/thread/Mutex.hxx +++ b/src/thread/Mutex.hxx @@ -58,4 +58,24 @@ public: ScopeLock &operator=(const ScopeLock &other) = delete; }; +/** + * Within the scope of an instance, this class will keep a #Mutex + * unlocked. + */ +class ScopeUnlock { + Mutex &mutex; + +public: + explicit ScopeUnlock(Mutex &_mutex):mutex(_mutex) { + mutex.unlock(); + }; + + ~ScopeUnlock() { + mutex.lock(); + } + + ScopeUnlock(const ScopeUnlock &other) = delete; + ScopeUnlock &operator=(const ScopeUnlock &other) = delete; +}; + #endif