716e811d64ea6ea4153572d6d9c485762b0988ef
[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         void addEdges(SnapList<ModelAction *> * edgeset, ModelAction *to);
27         void addEdge(ModelAction *from, ModelAction *to);
28         void addEdge(ModelAction *from, ModelAction *to, bool forceedge);
29         void addRMWEdge(ModelAction *from, ModelAction *rmw);
30         bool checkReachable(const ModelAction *from, const ModelAction *to) const;
31         void freeAction(const ModelAction * act);
32 #if SUPPORT_MOD_ORDER_DUMP
33         void dumpNodes(FILE *file) const;
34         void dumpGraphToFile(const char *filename) const;
35         void dot_print_node(FILE *file, const ModelAction *act);
36         void dot_print_edge(FILE *file, const ModelAction *from, const ModelAction *to, const char *prop);
37 #endif
38
39         CycleNode * getNode_noCreate(const ModelAction *act) const;
40         SNAPSHOTALLOC
41 private:
42         void addNodeEdge(CycleNode *fromnode, CycleNode *tonode, bool forceedge);
43         void putNode(const ModelAction *act, CycleNode *node);
44         CycleNode * getNode(ModelAction *act);
45
46         /** @brief A table for mapping ModelActions to CycleNodes */
47         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
48         SnapVector<const CycleNode *> * queue;
49
50 #if SUPPORT_MOD_ORDER_DUMP
51         SnapVector<CycleNode *> nodeList;
52 #endif
53
54         bool checkReachable(const CycleNode *from, const CycleNode *to) const;
55 };
56
57 /**
58  * @brief A node within a CycleGraph; corresponds either to one ModelAction
59  */
60 class CycleNode {
61 public:
62         CycleNode(ModelAction *act);
63         void addEdge(CycleNode *node);
64         CycleNode * getEdge(unsigned int i) const;
65         unsigned int getNumEdges() const;
66         CycleNode * getInEdge(unsigned int i) const;
67         unsigned int getNumInEdges() const;
68         bool setRMW(CycleNode *);
69         CycleNode * getRMW() const;
70         void clearRMW() { hasRMW = NULL; }
71         ModelAction * getAction() const { return action; }
72         void removeInEdge(CycleNode *src);
73         void removeEdge(CycleNode *dst);
74         ~CycleNode();
75
76         SNAPSHOTALLOC
77 private:
78         /** @brief The ModelAction that this node represents */
79         ModelAction *action;
80
81         /** @brief The edges leading out from this node */
82         SnapVector<CycleNode *> edges;
83
84         /** @brief The edges leading in from this node */
85         SnapVector<CycleNode *> inedges;
86
87         /** Pointer to a RMW node that reads from this node, or NULL, if none
88          * exists */
89         CycleNode *hasRMW;
90
91         /** ClockVector for this Node. */
92         ClockVector *cv;
93         friend class CycleGraph;
94 };
95
96 #endif  /* __CYCLEGRAPH_H__ */