9fc789c1913edd590f59c497f6cee1ff0b59eff2
[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         void 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         void 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
106         void popEdge() {
107                 edges.pop_back();
108         }
109
110         bool is_promise() const { return !action; }
111         void resolvePromise(const ModelAction *writer);
112
113         SNAPSHOTALLOC
114  private:
115         /** @brief The ModelAction that this node represents */
116         const ModelAction *action;
117
118         /** @brief The promise represented by this node; only valid when action
119          * is NULL */
120         const Promise *promise;
121
122         /** @brief The edges leading out from this node */
123         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > edges;
124
125         /** @brief The edges leading into this node */
126         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > back_edges;
127
128         /** Pointer to a RMW node that reads from this node, or NULL, if none
129          * exists */
130         CycleNode *hasRMW;
131 };
132
133 /*
134  * @brief Adds an edge between objects
135  *
136  * This function will add an edge between any two objects which can be
137  * associated with a CycleNode. That is, if they have a CycleGraph::getNode
138  * implementation.
139  *
140  * The object to is ordered after the object from.
141  *
142  * @param to The edge points to this object, of type T
143  * @param from The edge comes from this object, of type U
144  */
145 template <typename T, typename U>
146 void CycleGraph::addEdge(const T from, const U to)
147 {
148         ASSERT(from);
149         ASSERT(to);
150
151         CycleNode *fromnode = getNode(from);
152         CycleNode *tonode = getNode(to);
153
154         addNodeEdge(fromnode, tonode);
155 }
156
157 #endif /* __CYCLEGRAPH_H__ */