benchmark silo added
[c11concurrency-benchmarks.git] / silo / ownership_checker.h
1 #pragma once
2
3 #include <vector>
4 #include "macros.h"
5
6 /**
7  * T - node type. needs to implement is_lock_owner()
8  */
9 template <typename Scope, typename T>
10 class ownership_checker {
11 public:
12   static void
13   NodeLockRegionBegin()
14   {
15     MyLockedNodes(true)->clear();
16   }
17
18   // is used to signal the end of a tuple lock region
19   static void
20   AssertAllNodeLocksReleased()
21   {
22     std::vector<const T *> *nodes = MyLockedNodes(false);
23     ALWAYS_ASSERT(nodes);
24     for (auto p : *nodes)
25       ALWAYS_ASSERT(!p->is_lock_owner());
26     nodes->clear();
27   }
28
29   static void
30   AddNodeToLockRegion(const T *n)
31   {
32     ALWAYS_ASSERT(n->is_locked());
33     ALWAYS_ASSERT(n->is_lock_owner());
34     std::vector<const T *> *nodes = MyLockedNodes(false);
35     if (nodes)
36       nodes->push_back(n);
37   }
38
39 private:
40   static std::vector<const T *> *
41   MyLockedNodes(bool create)
42   {
43     static __thread std::vector<const T *> *tl_locked_nodes = nullptr;
44     if (unlikely(!tl_locked_nodes) && create)
45       tl_locked_nodes = new std::vector<const T *>;
46     return tl_locked_nodes;
47   }
48 };