clean up compile and run scripts for silo
[c11concurrency-benchmarks.git] / silo / lockguard.h
1 #ifndef _LOCK_GUARD_H_
2 #define _LOCK_GUARD_H_
3
4 #include <utility>
5
6 #include "macros.h"
7
8 // this exists in C++11, but we have a richer constructor
9 template <typename BasicLockable>
10 class lock_guard {
11 public:
12
13   template <class... Args>
14   lock_guard(BasicLockable *l, Args &&... args)
15     : l(l)
16   {
17     if (likely(l))
18       l->lock(std::forward<Args>(args)...);
19   }
20
21   template <class... Args>
22   lock_guard(BasicLockable &l, Args &&... args)
23     : l(&l)
24   {
25     l.lock(std::forward<Args>(args)...);
26   }
27
28   ~lock_guard()
29   {
30     if (likely(l))
31       l->unlock();
32   }
33 private:
34   BasicLockable *l;
35 };
36
37 #endif /* _LOCK_GUARD_H_ */