cyclegraph: template for addEdge()
[c11tester.git] / cyclegraph.h
1 /**
2  * @file cyclegraph.h
3  * @brief Data structure to track ordering constraints on modification order
4  *
5  * Used to determine whether a total order exists that satisfies the ordering
6  * constraints.
7  */
8
9 #ifndef __CYCLEGRAPH_H__
10 #define __CYCLEGRAPH_H__
11
12 #include "hashtable.h"
13 #include <vector>
14 #include <inttypes.h>
15 #include "config.h"
16 #include "mymemory.h"
17
18 class Promise;
19 class CycleNode;
20 class ModelAction;
21
22 /** @brief A graph of Model Actions for tracking cycles. */
23 class CycleGraph {
24  public:
25         CycleGraph();
26         ~CycleGraph();
27
28         template <typename T, typename U>
29         void addEdge(const T from, const U to);
30
31         bool checkForCycles() const;
32         void addRMWEdge(const ModelAction *from, const ModelAction *rmw);
33         bool checkPromise(const ModelAction *from, Promise *p) const;
34         bool checkReachable(const ModelAction *from, const ModelAction *to) const;
35         void startChanges();
36         void commitChanges();
37         void rollbackChanges();
38 #if SUPPORT_MOD_ORDER_DUMP
39         void dumpNodes(FILE *file) const;
40         void dumpGraphToFile(const char *filename) const;
41 #endif
42
43         SNAPSHOTALLOC
44  private:
45         void addNodeEdge(CycleNode *fromnode, CycleNode *tonode);
46         void putNode(const ModelAction *act, CycleNode *node);
47         CycleNode * getNode(const ModelAction *);
48         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
49
50         /** @brief A table for mapping ModelActions to CycleNodes */
51         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
52 #if SUPPORT_MOD_ORDER_DUMP
53         std::vector<CycleNode *> nodeList;
54 #endif
55
56         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
57
58         /** @brief A flag: true if this graph contains cycles */
59         bool hasCycles;
60         bool oldCycles;
61
62         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rollbackvector;
63         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rmwrollbackvector;
64 };
65
66 /**
67  * @brief A node within a CycleGraph; corresponds either to one ModelAction or
68  * to a promised future value
69  */
70 class CycleNode {
71  public:
72         CycleNode(const ModelAction *act);
73         CycleNode(const Promise *promise);
74         bool addEdge(CycleNode *node);
75         CycleNode * getEdge(unsigned int i) const;
76         unsigned int getNumEdges() const;
77         CycleNode * getBackEdge(unsigned int i) const;
78         unsigned int getNumBackEdges() const;
79         CycleNode * removeEdge();
80         CycleNode * removeBackEdge();
81
82         bool setRMW(CycleNode *);
83         CycleNode * getRMW() const;
84         void clearRMW() { hasRMW = NULL; }
85         const ModelAction * getAction() const { return action; }
86
87         void popEdge() {
88                 edges.pop_back();
89         }
90
91         bool is_promise() const { return !action; }
92
93         SNAPSHOTALLOC
94  private:
95         /** @brief The ModelAction that this node represents */
96         const ModelAction *action;
97
98         /** @brief The promise represented by this node; only valid when action
99          * is NULL */
100         const Promise *promise;
101
102         /** @brief The edges leading out from this node */
103         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > edges;
104
105         /** @brief The edges leading into this node */
106         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > back_edges;
107
108         /** Pointer to a RMW node that reads from this node, or NULL, if none
109          * exists */
110         CycleNode *hasRMW;
111 };
112
113 #endif /* __CYCLEGRAPH_H__ */