(1) structure code a little better
[c11tester.git] / cyclegraph.h
1 /** @file cyclegraph.h @brief Data structure to track ordering
2  *  constraints on modification order.  The idea is to see whether a
3  *  total order exists that satisfies the ordering constriants.*/
4
5 #ifndef CYCLEGRAPH_H
6 #define CYCLEGRAPH_H
7
8 #include "hashtable.h"
9 #include <vector>
10 #include <inttypes.h>
11
12 class CycleNode;
13 class ModelAction;
14
15 /** @brief A graph of Model Actions for tracking cycles. */
16 class CycleGraph {
17  public:
18         CycleGraph();
19         ~CycleGraph();
20         void addEdge(const ModelAction *from, const ModelAction *to);
21         bool checkForCycles();
22         void addRMWEdge(const ModelAction *from, const ModelAction *rmw);
23
24         bool checkReachable(const ModelAction *from, const ModelAction *to);
25         void startChanges();
26         void commitChanges();
27         void rollbackChanges();
28
29  private:
30         CycleNode * getNode(const ModelAction *);
31
32         /** @brief A table for mapping ModelActions to CycleNodes */
33         HashTable<const ModelAction *, CycleNode *, uintptr_t, 4> actionToNode;
34
35         bool checkReachable(CycleNode *from, CycleNode *to);
36
37         /** @brief A flag: true if this graph contains cycles */
38         bool hasCycles;
39
40         bool oldCycles;
41
42         std::vector<CycleNode *> rollbackvector;
43         std::vector<CycleNode *> rmwrollbackvector;
44 };
45
46 /** @brief A node within a CycleGraph; corresponds to one ModelAction */
47 class CycleNode {
48  public:
49         CycleNode(const ModelAction *action);
50         void addEdge(CycleNode * node);
51         std::vector<CycleNode *> * getEdges();
52         bool setRMW(CycleNode *);
53         CycleNode* getRMW();
54         void popEdge() {
55                 edges.pop_back();
56         };
57         void clearRMW() {
58                 hasRMW=NULL;
59         }
60
61  private:
62         /** @brief The ModelAction that this node represents */
63         const ModelAction *action;
64
65         /** @brief The edges leading out from this node */
66         std::vector<CycleNode *> edges;
67
68         /** Pointer to a RMW node that reads from this node, or NULL, if none
69          * exists */
70         CycleNode * hasRMW;
71 };
72
73 #endif