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