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