cyclegraph: bugfix - pop edges properly
[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 typedef std::vector< const Promise *, ModelAlloc<const Promise *> > promise_list_t;
23
24 /** @brief A graph of Model Actions for tracking cycles. */
25 class CycleGraph {
26  public:
27         CycleGraph();
28         ~CycleGraph();
29
30         template <typename T, typename U>
31         bool addEdge(const T from, const U to);
32
33         bool checkForCycles() const;
34         void addRMWEdge(const ModelAction *from, const ModelAction *rmw);
35         bool checkPromise(const ModelAction *from, Promise *p) const;
36
37         template <typename T>
38         bool checkReachable(const ModelAction *from, const T *to) const;
39
40         void startChanges();
41         void commitChanges();
42         void rollbackChanges();
43 #if SUPPORT_MOD_ORDER_DUMP
44         void dumpNodes(FILE *file) const;
45         void dumpGraphToFile(const char *filename) const;
46 #endif
47
48         bool resolvePromise(ModelAction *reader, ModelAction *writer,
49                         promise_list_t *mustResolve);
50
51         SNAPSHOTALLOC
52  private:
53         bool addNodeEdge(CycleNode *fromnode, CycleNode *tonode);
54         void putNode(const ModelAction *act, CycleNode *node);
55         CycleNode * getNode(const ModelAction *act);
56         CycleNode * getNode(const Promise *promise);
57         CycleNode * getNode_noCreate(const ModelAction *act) const;
58         CycleNode * getNode_noCreate(const Promise *promise) const;
59         bool mergeNodes(CycleNode *node1, CycleNode *node2,
60                         promise_list_t *mustMerge);
61
62         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
63
64         /** @brief A table for mapping ModelActions to CycleNodes */
65         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
66         /** @brief A table for mapping reader ModelActions to Promise
67          *  CycleNodes */
68         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> readerToPromiseNode;
69
70 #if SUPPORT_MOD_ORDER_DUMP
71         std::vector<CycleNode *> nodeList;
72 #endif
73
74         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
75
76         /** @brief A flag: true if this graph contains cycles */
77         bool hasCycles;
78         bool oldCycles;
79
80         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rollbackvector;
81         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rmwrollbackvector;
82 };
83
84 /**
85  * @brief A node within a CycleGraph; corresponds either to one ModelAction or
86  * to a promised future value
87  */
88 class CycleNode {
89  public:
90         CycleNode(const ModelAction *act);
91         CycleNode(const Promise *promise);
92         bool addEdge(CycleNode *node);
93         CycleNode * getEdge(unsigned int i) const;
94         unsigned int getNumEdges() const;
95         CycleNode * getBackEdge(unsigned int i) const;
96         unsigned int getNumBackEdges() const;
97         CycleNode * removeEdge();
98         CycleNode * removeBackEdge();
99
100         bool setRMW(CycleNode *);
101         CycleNode * getRMW() const;
102         void clearRMW() { hasRMW = NULL; }
103         const ModelAction * getAction() const { return action; }
104         const Promise * getPromise() const { return promise; }
105         bool is_promise() const { return !action; }
106         void resolvePromise(const ModelAction *writer);
107
108         SNAPSHOTALLOC
109  private:
110         /** @brief The ModelAction that this node represents */
111         const ModelAction *action;
112
113         /** @brief The promise represented by this node; only valid when action
114          * is NULL */
115         const Promise *promise;
116
117         /** @brief The edges leading out from this node */
118         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > edges;
119
120         /** @brief The edges leading into this node */
121         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > back_edges;
122
123         /** Pointer to a RMW node that reads from this node, or NULL, if none
124          * exists */
125         CycleNode *hasRMW;
126 };
127
128 /*
129  * @brief Adds an edge between objects
130  *
131  * This function will add an edge between any two objects which can be
132  * associated with a CycleNode. That is, if they have a CycleGraph::getNode
133  * implementation.
134  *
135  * The object to is ordered after the object from.
136  *
137  * @param to The edge points to this object, of type T
138  * @param from The edge comes from this object, of type U
139  * @return True, if new edge(s) are added; otherwise false
140  */
141 template <typename T, typename U>
142 bool CycleGraph::addEdge(const T from, const U to)
143 {
144         ASSERT(from);
145         ASSERT(to);
146
147         CycleNode *fromnode = getNode(from);
148         CycleNode *tonode = getNode(to);
149
150         return addNodeEdge(fromnode, tonode);
151 }
152
153 /**
154  * Checks whether one ModelAction can reach another ModelAction/Promise
155  * @param from The ModelAction from which to begin exploration
156  * @param to The ModelAction or Promise to reach
157  * @return True, @a from can reach @a to; otherwise, false
158  */
159 template <typename T>
160 bool CycleGraph::checkReachable(const ModelAction *from, const T *to) const
161 {
162         CycleNode *fromnode = getNode_noCreate(from);
163         CycleNode *tonode = getNode_noCreate(to);
164
165         if (!fromnode || !tonode)
166                 return false;
167
168         return checkReachable(fromnode, tonode);
169 }
170
171 #endif /* __CYCLEGRAPH_H__ */