nodestack: bugfix - rewrite 'may-read-from' and 'future values' as the same set
[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         template <typename T>
34         void addRMWEdge(const T *from, const ModelAction *rmw);
35
36         bool checkForCycles() const;
37         bool checkPromise(const ModelAction *from, Promise *p) const;
38
39         template <typename T, typename U>
40         bool checkReachable(const T *from, const U *to) const;
41
42         void startChanges();
43         void commitChanges();
44         void rollbackChanges();
45 #if SUPPORT_MOD_ORDER_DUMP
46         void dumpNodes(FILE *file) const;
47         void dumpGraphToFile(const char *filename) const;
48 #endif
49
50         bool resolvePromise(const Promise *promise, ModelAction *writer,
51                         promise_list_t *mustResolve);
52
53         SNAPSHOTALLOC
54  private:
55         bool addNodeEdge(CycleNode *fromnode, CycleNode *tonode);
56         void putNode(const ModelAction *act, CycleNode *node);
57         void putNode(const Promise *promise, CycleNode *node);
58         void erasePromiseNode(const Promise *promise);
59         CycleNode * getNode(const ModelAction *act);
60         CycleNode * getNode(const Promise *promise);
61         CycleNode * getNode_noCreate(const ModelAction *act) const;
62         CycleNode * getNode_noCreate(const Promise *promise) const;
63         bool mergeNodes(CycleNode *node1, CycleNode *node2,
64                         promise_list_t *mustMerge);
65
66         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
67
68         /** @brief A table for mapping ModelActions to CycleNodes */
69         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
70         /** @brief A table for mapping Promises to CycleNodes */
71         HashTable<const Promise *, CycleNode *, uintptr_t, 4> promiseToNode;
72
73 #if SUPPORT_MOD_ORDER_DUMP
74         std::vector<CycleNode *> nodeList;
75 #endif
76
77         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
78
79         /** @brief A flag: true if this graph contains cycles */
80         bool hasCycles;
81         bool oldCycles;
82
83         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rollbackvector;
84         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rmwrollbackvector;
85 };
86
87 /**
88  * @brief A node within a CycleGraph; corresponds either to one ModelAction or
89  * to a promised future value
90  */
91 class CycleNode {
92  public:
93         CycleNode(const ModelAction *act);
94         CycleNode(const Promise *promise);
95         bool addEdge(CycleNode *node);
96         CycleNode * getEdge(unsigned int i) const;
97         unsigned int getNumEdges() const;
98         CycleNode * getBackEdge(unsigned int i) const;
99         unsigned int getNumBackEdges() const;
100         CycleNode * removeEdge();
101         CycleNode * removeBackEdge();
102
103         bool setRMW(CycleNode *);
104         CycleNode * getRMW() const;
105         void clearRMW() { hasRMW = NULL; }
106         const ModelAction * getAction() const { return action; }
107         const Promise * getPromise() const { return promise; }
108         bool is_promise() const { return !action; }
109         void resolvePromise(const ModelAction *writer);
110
111         SNAPSHOTALLOC
112  private:
113         /** @brief The ModelAction that this node represents */
114         const ModelAction *action;
115
116         /** @brief The promise represented by this node; only valid when action
117          * is NULL */
118         const Promise *promise;
119
120         /** @brief The edges leading out from this node */
121         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > edges;
122
123         /** @brief The edges leading into this node */
124         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > back_edges;
125
126         /** Pointer to a RMW node that reads from this node, or NULL, if none
127          * exists */
128         CycleNode *hasRMW;
129 };
130
131 #endif /* __CYCLEGRAPH_H__ */