cyclegraph: add full promise resolution, node merging
[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         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         bool checkReachable(const ModelAction *from, const ModelAction *to) const;
37         void startChanges();
38         void commitChanges();
39         void rollbackChanges();
40 #if SUPPORT_MOD_ORDER_DUMP
41         void dumpNodes(FILE *file) const;
42         void dumpGraphToFile(const char *filename) const;
43 #endif
44
45         bool resolvePromise(ModelAction *reader, ModelAction *writer,
46                         promise_list_t *mustResolve);
47
48         SNAPSHOTALLOC
49  private:
50         void addNodeEdge(CycleNode *fromnode, CycleNode *tonode);
51         void putNode(const ModelAction *act, CycleNode *node);
52         CycleNode * getNode(const ModelAction *);
53         CycleNode * getNode(const Promise *promise);
54         bool mergeNodes(CycleNode *node1, CycleNode *node2,
55                         promise_list_t *mustMerge);
56
57         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
58
59         /** @brief A table for mapping ModelActions to CycleNodes */
60         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
61         /** @brief A table for mapping reader ModelActions to Promise
62          *  CycleNodes */
63         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> readerToPromiseNode;
64
65 #if SUPPORT_MOD_ORDER_DUMP
66         std::vector<CycleNode *> nodeList;
67 #endif
68
69         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
70
71         /** @brief A flag: true if this graph contains cycles */
72         bool hasCycles;
73         bool oldCycles;
74
75         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rollbackvector;
76         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rmwrollbackvector;
77 };
78
79 /**
80  * @brief A node within a CycleGraph; corresponds either to one ModelAction or
81  * to a promised future value
82  */
83 class CycleNode {
84  public:
85         CycleNode(const ModelAction *act);
86         CycleNode(const Promise *promise);
87         bool addEdge(CycleNode *node);
88         CycleNode * getEdge(unsigned int i) const;
89         unsigned int getNumEdges() const;
90         CycleNode * getBackEdge(unsigned int i) const;
91         unsigned int getNumBackEdges() const;
92         CycleNode * removeEdge();
93         CycleNode * removeBackEdge();
94
95         bool setRMW(CycleNode *);
96         CycleNode * getRMW() const;
97         void clearRMW() { hasRMW = NULL; }
98         const ModelAction * getAction() const { return action; }
99         const Promise * getPromise() const { return promise; }
100
101         void popEdge() {
102                 edges.pop_back();
103         }
104
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 #endif /* __CYCLEGRAPH_H__ */