e7e9380bb7e67e195d3d2ab95b587a44aa0234aa
[model-checker.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         CycleNode * getNode(const Promise *promise);
49
50         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
51
52         /** @brief A table for mapping ModelActions to CycleNodes */
53         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
54         /** @brief A table for mapping reader ModelActions to Promise
55          *  CycleNodes */
56         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> readerToPromiseNode;
57
58 #if SUPPORT_MOD_ORDER_DUMP
59         std::vector<CycleNode *> nodeList;
60 #endif
61
62         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
63
64         /** @brief A flag: true if this graph contains cycles */
65         bool hasCycles;
66         bool oldCycles;
67
68         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rollbackvector;
69         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rmwrollbackvector;
70 };
71
72 /**
73  * @brief A node within a CycleGraph; corresponds either to one ModelAction or
74  * to a promised future value
75  */
76 class CycleNode {
77  public:
78         CycleNode(const ModelAction *act);
79         CycleNode(const Promise *promise);
80         bool addEdge(CycleNode *node);
81         CycleNode * getEdge(unsigned int i) const;
82         unsigned int getNumEdges() const;
83         CycleNode * getBackEdge(unsigned int i) const;
84         unsigned int getNumBackEdges() const;
85         CycleNode * removeEdge();
86         CycleNode * removeBackEdge();
87
88         bool setRMW(CycleNode *);
89         CycleNode * getRMW() const;
90         void clearRMW() { hasRMW = NULL; }
91         const ModelAction * getAction() const { return action; }
92         const Promise * getPromise() const { return promise; }
93
94         void popEdge() {
95                 edges.pop_back();
96         }
97
98         bool is_promise() const { return !action; }
99         void resolvePromise(const ModelAction *writer);
100
101         SNAPSHOTALLOC
102  private:
103         /** @brief The ModelAction that this node represents */
104         const ModelAction *action;
105
106         /** @brief The promise represented by this node; only valid when action
107          * is NULL */
108         const Promise *promise;
109
110         /** @brief The edges leading out from this node */
111         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > edges;
112
113         /** @brief The edges leading into this node */
114         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > back_edges;
115
116         /** Pointer to a RMW node that reads from this node, or NULL, if none
117          * exists */
118         CycleNode *hasRMW;
119 };
120
121 #endif /* __CYCLEGRAPH_H__ */