Remove promises
[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 CycleNode;
21 class ModelAction;
22
23 /** @brief A graph of Model Actions for tracking cycles. */
24 class CycleGraph {
25  public:
26         CycleGraph();
27         ~CycleGraph();
28
29         template <typename T, typename U>
30         bool addEdge(const T *from, const U *to);
31
32         template <typename T>
33         void addRMWEdge(const T *from, const ModelAction *rmw);
34
35         bool checkForCycles() const;
36
37         template <typename T, typename U>
38         bool checkReachable(const T *from, const U *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
47         void dot_print_node(FILE *file, const ModelAction *act);
48         template <typename T, typename U>
49         void dot_print_edge(FILE *file, const T *from, const U *to, const char *prop);
50 #endif
51
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_noCreate(const ModelAction *act) const;
59         bool mergeNodes(CycleNode *node1, CycleNode *node2);
60
61         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
62         ModelVector<const CycleNode *> * queue;
63
64
65         /** @brief A table for mapping ModelActions to CycleNodes */
66         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
67
68 #if SUPPORT_MOD_ORDER_DUMP
69         SnapVector<CycleNode *> nodeList;
70 #endif
71
72         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
73
74         /** @brief A flag: true if this graph contains cycles */
75         bool hasCycles;
76         /** @brief The previous value of CycleGraph::hasCycles, for rollback */
77         bool oldCycles;
78
79         SnapVector<CycleNode *> rollbackvector;
80         SnapVector<CycleNode *> rmwrollbackvector;
81 };
82
83 /**
84  * @brief A node within a CycleGraph; corresponds either to one ModelAction
85  */
86 class CycleNode {
87  public:
88         CycleNode(const ModelAction *act);
89         bool addEdge(CycleNode *node);
90         CycleNode * getEdge(unsigned int i) const;
91         unsigned int getNumEdges() const;
92         CycleNode * getBackEdge(unsigned int i) const;
93         unsigned int getNumBackEdges() const;
94         CycleNode * removeEdge();
95         CycleNode * removeBackEdge();
96
97         bool setRMW(CycleNode *);
98         CycleNode * getRMW() const;
99         void clearRMW() { hasRMW = NULL; }
100         const ModelAction * getAction() const { return action; }
101
102         SNAPSHOTALLOC
103  private:
104         /** @brief The ModelAction that this node represents */
105         const ModelAction *action;
106
107         /** @brief The edges leading out from this node */
108         SnapVector<CycleNode *> edges;
109
110         /** @brief The edges leading into this node */
111         SnapVector<CycleNode *> back_edges;
112
113         /** Pointer to a RMW node that reads from this node, or NULL, if none
114          * exists */
115         CycleNode *hasRMW;
116 };
117
118 #endif /* __CYCLEGRAPH_H__ */