]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
added LockGuard
authorRazvan Becheriu <razvan@isc.org>
Mon, 25 Mar 2019 16:42:51 +0000 (18:42 +0200)
committerRazvan Becheriu <razvan@isc.org>
Fri, 12 Apr 2019 12:07:34 +0000 (15:07 +0300)
src/lib/util/threads/lock_guard.h [new file with mode: 0644]

diff --git a/src/lib/util/threads/lock_guard.h b/src/lib/util/threads/lock_guard.h
new file mode 100644 (file)
index 0000000..6c949a2
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef LOCK_GUARD_H
+#define LOCK_GUARD_H
+
+#include <memory>
+
+namespace isc {
+namespace util {
+namespace thread {
+
+template <typename Lock>
+class LockGuard {
+public:
+    LockGuard(Lock* lock) : lk_(lock) {
+        if (lk_) {
+            lk_->lock();
+        }
+    }
+
+    ~LockGuard() {
+        if (lk_) {
+            lk_->unlock();
+        }
+    }
+
+    LockGuard(const LockGuard&) = delete;
+    LockGuard& operator=(const LockGuard&) = delete;
+
+    LockGuard(LockGuard&&) = delete;
+    LockGuard& operator=(LockGuard&&) = delete;
+
+private:
+    Lock* lk_;
+};
+
+}  // namespace thread
+}  // namespace util
+}  // namespace isc
+
+#endif  // LOCK_GUARD_H