556af1cfdd04627c8b9a5d70d05c3078b01c3469
[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 #include "classlist.h"
20
21 /** @brief A graph of Model Actions for tracking cycles. */
22 class CycleGraph {
23  public:
24         CycleGraph();
25         ~CycleGraph();
26
27         template <typename T, typename U>
28         bool addEdge(const T *from, const U *to);
29
30         template <typename T>
31         void addRMWEdge(const T *from, const ModelAction *rmw);
32
33         bool checkForCycles() const;
34
35         template <typename T, typename U>
36         bool checkReachable(const T *from, const U *to) const;
37
38         void startChanges();
39         void commitChanges();
40         void rollbackChanges();
41 #if SUPPORT_MOD_ORDER_DUMP
42         void dumpNodes(FILE *file) const;
43         void dumpGraphToFile(const char *filename) const;
44
45         void dot_print_node(FILE *file, const ModelAction *act);
46         template <typename T, typename U>
47         void dot_print_edge(FILE *file, const T *from, const U *to, const char *prop);
48 #endif
49         CycleNode * getNode_noCreate(const ModelAction *act) const;
50
51         SNAPSHOTALLOC
52  private:
53         bool addNodeEdge(CycleNode *fromnode, CycleNode *tonode);
54         void putNode(const ModelAction *act, CycleNode *node);
55         CycleNode * getNode(const ModelAction *act);
56         bool mergeNodes(CycleNode *node1, CycleNode *node2);
57
58         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
59         ModelVector<const CycleNode *> * queue;
60
61
62         /** @brief A table for mapping ModelActions to CycleNodes */
63         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
64
65 #if SUPPORT_MOD_ORDER_DUMP
66         SnapVector<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         /** @brief The previous value of CycleGraph::hasCycles, for rollback */
74         bool oldCycles;
75
76         SnapVector<CycleNode *> rollbackvector;
77         SnapVector<CycleNode *> rmwrollbackvector;
78 };
79
80 /**
81  * @brief A node within a CycleGraph; corresponds either to one ModelAction
82  */
83 class CycleNode {
84  public:
85         CycleNode(const ModelAction *act);
86         bool addEdge(CycleNode *node);
87         CycleNode * getEdge(unsigned int i) const;
88         unsigned int getNumEdges() const;
89         CycleNode * getBackEdge(unsigned int i) const;
90         unsigned int getNumBackEdges() const;
91         CycleNode * removeEdge();
92         CycleNode * removeBackEdge();
93
94         bool setRMW(CycleNode *);
95         CycleNode * getRMW() const;
96         void clearRMW() { hasRMW = NULL; }
97         const ModelAction * getAction() const { return action; }
98
99         SNAPSHOTALLOC
100  private:
101         /** @brief The ModelAction that this node represents */
102         const ModelAction *action;
103
104         /** @brief The edges leading out from this node */
105         SnapVector<CycleNode *> edges;
106
107         /** @brief The edges leading into this node */
108         SnapVector<CycleNode *> back_edges;
109
110         /** Pointer to a RMW node that reads from this node, or NULL, if none
111          * exists */
112         CycleNode *hasRMW;
113 };
114
115 #endif /* __CYCLEGRAPH_H__ */