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