model: add promise printing to execution summaries
[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
20 class Promise;
21 class CycleNode;
22 class ModelAction;
23
24 typedef std::vector< const Promise *, ModelAlloc<const Promise *> > promise_list_t;
25
26 /** @brief A graph of Model Actions for tracking cycles. */
27 class CycleGraph {
28  public:
29         CycleGraph();
30         ~CycleGraph();
31
32         template <typename T, typename U>
33         bool addEdge(const T *from, const U *to);
34
35         template <typename T>
36         void addRMWEdge(const T *from, const ModelAction *rmw);
37
38         bool checkForCycles() const;
39         bool checkPromise(const ModelAction *from, Promise *p) const;
40
41         template <typename T, typename U>
42         bool checkReachable(const T *from, const U *to) const;
43
44         void startChanges();
45         void commitChanges();
46         void rollbackChanges();
47 #if SUPPORT_MOD_ORDER_DUMP
48         void dumpNodes(FILE *file) const;
49         void dumpGraphToFile(const char *filename) const;
50
51         void dot_print_node(FILE *file, const ModelAction *act);
52         template <typename T, typename U>
53         void dot_print_edge(FILE *file, const T *from, const U *to, const char *prop);
54 #endif
55
56         bool resolvePromise(const Promise *promise, ModelAction *writer);
57
58         SNAPSHOTALLOC
59  private:
60         bool addNodeEdge(CycleNode *fromnode, CycleNode *tonode);
61         void putNode(const ModelAction *act, CycleNode *node);
62         void putNode(const Promise *promise, CycleNode *node);
63         void erasePromiseNode(const Promise *promise);
64         CycleNode * getNode(const ModelAction *act);
65         CycleNode * getNode(const Promise *promise);
66         CycleNode * getNode_noCreate(const ModelAction *act) const;
67         CycleNode * getNode_noCreate(const Promise *promise) const;
68         bool mergeNodes(CycleNode *node1, CycleNode *node2);
69
70         HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> *discovered;
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         std::vector< CycleNode *, SnapshotAlloc<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         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > rollbackvector;
88         std::vector< CycleNode *, SnapshotAlloc<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         std::vector< CycleNode *, SnapshotAlloc<CycleNode *> > edges;
126
127         /** @brief The edges leading into this node */
128         std::vector< CycleNode *, SnapshotAlloc<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__ */