9819cf8763ef2eaa5e0b72cb22989bc116fb1059
[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 <vector>
13 #include <inttypes.h>
14 #include <stdio.h>
15
16 #include "hashtable.h"
17 #include "config.h"
18 #include "mymemory.h"
19 #include "stl-model.h"
20
21 class Promise;
22 class CycleNode;
23 class ModelAction;
24
25 typedef ModelVector<const Promise *> promise_list_t;
26
27 /** @brief A graph of Model Actions for tracking cycles. */
28 class CycleGraph {
29  public:
30         CycleGraph();
31         ~CycleGraph();
32
33         template <typename T, typename U>
34         bool addEdge(const T *from, const U *to);
35
36         template <typename T>
37         void addRMWEdge(const T *from, const ModelAction *rmw);
38
39         bool checkForCycles() const;
40         bool checkPromise(const ModelAction *from, Promise *p) const;
41
42         template <typename T, typename U>
43         bool checkReachable(const T *from, const U *to) const;
44
45         void startChanges();
46         void commitChanges();
47         void rollbackChanges();
48 #if SUPPORT_MOD_ORDER_DUMP
49         void dumpNodes(FILE *file) const;
50         void dumpGraphToFile(const char *filename) const;
51
52         void dot_print_node(FILE *file, const ModelAction *act);
53         template <typename T, typename U>
54         void dot_print_edge(FILE *file, const T *from, const U *to, const char *prop);
55 #endif
56
57         bool resolvePromise(const Promise *promise, ModelAction *writer);
58
59         SNAPSHOTALLOC
60  private:
61         bool addNodeEdge(CycleNode *fromnode, CycleNode *tonode);
62         void putNode(const ModelAction *act, CycleNode *node);
63         void putNode(const Promise *promise, CycleNode *node);
64         void erasePromiseNode(const Promise *promise);
65         CycleNode * getNode(const ModelAction *act);
66         CycleNode * getNode(const Promise *promise);
67         CycleNode * getNode_noCreate(const ModelAction *act) const;
68         CycleNode * getNode_noCreate(const Promise *promise) const;
69         bool mergeNodes(CycleNode *node1, CycleNode *node2);
70
71         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
72         ModelVector<const CycleNode *> * queue;
73
74
75         /** @brief A table for mapping ModelActions to CycleNodes */
76         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
77         /** @brief A table for mapping Promises to CycleNodes */
78         HashTable<const Promise *, CycleNode *, uintptr_t, 4> promiseToNode;
79
80 #if SUPPORT_MOD_ORDER_DUMP
81         SnapVector<CycleNode *> nodeList;
82 #endif
83
84         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
85
86         /** @brief A flag: true if this graph contains cycles */
87         bool hasCycles;
88         bool oldCycles;
89
90         SnapVector<CycleNode *> rollbackvector;
91         SnapVector<CycleNode *> rmwrollbackvector;
92 };
93
94 /**
95  * @brief A node within a CycleGraph; corresponds either to one ModelAction or
96  * to a promised future value
97  */
98 class CycleNode {
99  public:
100         CycleNode(const ModelAction *act);
101         CycleNode(const Promise *promise);
102         bool addEdge(CycleNode *node);
103         CycleNode * getEdge(unsigned int i) const;
104         unsigned int getNumEdges() const;
105         CycleNode * getBackEdge(unsigned int i) const;
106         unsigned int getNumBackEdges() const;
107         CycleNode * removeEdge();
108         CycleNode * removeBackEdge();
109
110         bool setRMW(CycleNode *);
111         CycleNode * getRMW() const;
112         void clearRMW() { hasRMW = NULL; }
113         const ModelAction * getAction() const { return action; }
114         const Promise * getPromise() const { return promise; }
115         bool is_promise() const { return !action; }
116         void resolvePromise(const ModelAction *writer);
117
118         SNAPSHOTALLOC
119  private:
120         /** @brief The ModelAction that this node represents */
121         const ModelAction *action;
122
123         /** @brief The promise represented by this node; only valid when action
124          * is NULL */
125         const Promise *promise;
126
127         /** @brief The edges leading out from this node */
128         SnapVector<CycleNode *> edges;
129
130         /** @brief The edges leading into this node */
131         SnapVector<CycleNode *> back_edges;
132
133         /** Pointer to a RMW node that reads from this node, or NULL, if none
134          * exists */
135         CycleNode *hasRMW;
136 };
137
138 #endif /* __CYCLEGRAPH_H__ */