cyclegraph: add edgeCreatesCycle() function
[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         void addEdge(const ModelAction *from, const ModelAction *to);
28         bool checkForCycles() const;
29         bool checkForRMWViolation() const;
30         void addRMWEdge(const ModelAction *from, const ModelAction *rmw);
31         bool checkPromise(const ModelAction *from, Promise *p) const;
32         bool checkReachable(const ModelAction *from, const ModelAction *to) const;
33         void startChanges();
34         void commitChanges();
35         void rollbackChanges();
36 #if SUPPORT_MOD_ORDER_DUMP
37         void dumpNodes(FILE *file) const;
38         void dumpGraphToFile(const char *filename) const;
39 #endif
40
41         SNAPSHOTALLOC
42  private:
43         void putNode(const ModelAction *act, CycleNode *node);
44         CycleNode * getNode(const ModelAction *);
45         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
46
47         /** @brief A table for mapping ModelActions to CycleNodes */
48         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
49 #if SUPPORT_MOD_ORDER_DUMP
50         std::vector<CycleNode *> nodeList;
51 #endif
52
53         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
54
55         bool edgeCreatesCycle(const CycleNode *from, const CycleNode *to) const;
56
57         /** @brief A flag: true if this graph contains cycles */
58         bool hasCycles;
59         bool oldCycles;
60
61         bool hasRMWViolation;
62         bool oldRMWViolation;
63
64         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rollbackvector;
65         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rmwrollbackvector;
66 };
67
68 /** @brief A node within a CycleGraph; corresponds to one ModelAction */
69 class CycleNode {
70  public:
71         CycleNode(const ModelAction *act);
72         bool addEdge(CycleNode *node);
73         CycleNode * getEdge(unsigned int i) const;
74         unsigned int getNumEdges() const;
75         CycleNode * getBackEdge(unsigned int i) const;
76         unsigned int getNumBackEdges() const;
77         bool setRMW(CycleNode *);
78         CycleNode * getRMW() const;
79         const ModelAction * getAction() const { return action; }
80
81         void popEdge() {
82                 edges.pop_back();
83         }
84         void clearRMW() {
85                 hasRMW = NULL;
86         }
87
88         SNAPSHOTALLOC
89  private:
90         /** @brief The ModelAction that this node represents */
91         const ModelAction *action;
92
93         /** @brief The edges leading out from this node */
94         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > edges;
95
96         /** @brief The edges leading into this node */
97         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > back_edges;
98
99         /** Pointer to a RMW node that reads from this node, or NULL, if none
100          * exists */
101         CycleNode *hasRMW;
102 };
103
104 #endif /* __CYCLEGRAPH_H__ */