benchmark silo added
[c11concurrency-benchmarks.git] / silo / lockguard.h
diff --git a/silo/lockguard.h b/silo/lockguard.h
new file mode 100644 (file)
index 0000000..71e5f9f
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef _LOCK_GUARD_H_
+#define _LOCK_GUARD_H_
+
+#include <utility>
+
+#include "macros.h"
+
+// this exists in C++11, but we have a richer constructor
+template <typename BasicLockable>
+class lock_guard {
+public:
+
+  template <class... Args>
+  lock_guard(BasicLockable *l, Args &&... args)
+    : l(l)
+  {
+    if (likely(l))
+      l->lock(std::forward<Args>(args)...);
+  }
+
+  template <class... Args>
+  lock_guard(BasicLockable &l, Args &&... args)
+    : l(&l)
+  {
+    l.lock(std::forward<Args>(args)...);
+  }
+
+  ~lock_guard()
+  {
+    if (likely(l))
+      l->unlock();
+  }
+private:
+  BasicLockable *l;
+};
+
+#endif /* _LOCK_GUARD_H_ */