test/Makefile: remove pointless variable
[model-checker.git] / cyclegraph.cc
1 #include "cyclegraph.h"
2 #include "action.h"
3
4 /** Initializes a CycleGraph object. */
5 CycleGraph::CycleGraph() :
6         hasCycles(false)
7 {
8 }
9
10 /** CycleGraph destructor */
11 CycleGraph::~CycleGraph() {
12 }
13
14 /**
15  * @brief Returns the CycleNode corresponding to a given ModelAction
16  * @param action The ModelAction to find a node for
17  * @return The CycleNode paired with this action
18  */
19 CycleNode * CycleGraph::getNode(const ModelAction *action) {
20         CycleNode *node=actionToNode.get(action);
21         if (node==NULL) {
22                 node=new CycleNode(action);
23                 actionToNode.put(action, node);
24         }
25         return node;
26 }
27
28 /**
29  * Adds an edge between two ModelActions. The ModelAction @a to is ordered
30  * after the ModelAction @a from.
31  * @param to The edge points to this ModelAction
32  * @param from The edge comes from this ModelAction
33  */
34 void CycleGraph::addEdge(const ModelAction *to, const ModelAction *from) {
35         CycleNode *fromnode=getNode(from);
36         CycleNode *tonode=getNode(to);
37
38         if (!hasCycles) {
39                 // Check for Cycles
40                 hasCycles=checkReachable(tonode, fromnode);
41         }
42         fromnode->addEdge(tonode);
43
44         CycleNode * rmwnode=fromnode->getRMW();
45
46         //If the fromnode has a rmwnode that is not the tonode, we
47         //should add an edge between its rmwnode and the tonode
48
49         if (rmwnode!=NULL&&rmwnode!=tonode) {
50                 if (!hasCycles) {
51                         // Check for Cycles
52                         hasCycles=checkReachable(tonode, rmwnode);
53                 }
54                 rmwnode->addEdge(tonode);
55         }
56 }
57
58 /** Handles special case of a RMW action.  The ModelAction rmw reads
59  *  from the ModelAction from.  The key differences are: (1) no write
60  *  can occur in between the rmw and the from action.  Only one RMW
61  *  action can read from a given write.
62  */
63 void CycleGraph::addRMWEdge(const ModelAction *rmw, const ModelAction *from) {
64         CycleNode *fromnode=getNode(from);
65         CycleNode *rmwnode=getNode(rmw);
66
67         /* Two RMW actions cannot read from the same write. */
68         if (fromnode->setRMW(rmwnode)) {
69                 hasCycles=true;
70         }
71
72         /* Transfer all outgoing edges from the from node to the rmw node */
73         /* This process cannot add a cycle because rmw should not have any
74                  incoming edges yet.*/
75         std::vector<CycleNode *> * edges=fromnode->getEdges();
76         for(unsigned int i=0;i<edges->size();i++) {
77                 CycleNode * tonode=(*edges)[i];
78                 rmwnode->addEdge(tonode);
79         }
80
81         fromnode->addEdge(rmwnode);
82 }
83
84
85 /**
86  * Checks whether one CycleNode can reach another.
87  * @param from The CycleNode from which to begin exploration
88  * @param to The CycleNode to reach
89  * @return True, @a from can reach @a to; otherwise, false
90  */
91 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
92         std::vector<CycleNode *> queue;
93         HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
94
95         queue.push_back(from);
96         discovered.put(from, from);
97         while(!queue.empty()) {
98                 CycleNode * node=queue.back();
99                 queue.pop_back();
100                 if (node==to)
101                         return true;
102
103                 for(unsigned int i=0;i<node->getEdges()->size();i++) {
104                         CycleNode *next=(*node->getEdges())[i];
105                         if (!discovered.contains(next)) {
106                                 discovered.put(next,next);
107                                 queue.push_back(next);
108                         }
109                 }
110         }
111         return false;
112 }
113
114 /** @returns whether a CycleGraph contains cycles. */
115 bool CycleGraph::checkForCycles() {
116         return hasCycles;
117 }
118
119 /**
120  * Constructor for a CycleNode.
121  * @param modelaction The ModelAction for this node
122  */
123 CycleNode::CycleNode(const ModelAction *modelaction) :
124         action(modelaction),
125         hasRMW(NULL)
126 {
127 }
128
129 /** @returns a vector of the edges from a CycleNode. */
130 std::vector<CycleNode *> * CycleNode::getEdges() {
131         return &edges;
132 }
133
134 /**
135  * Adds an edge from this CycleNode to another CycleNode.
136  * @param node The node to which we add a directed edge
137  */
138 void CycleNode::addEdge(CycleNode *node) {
139         edges.push_back(node);
140 }
141
142 /** @returns the RMW CycleNode that reads from the current CycleNode */
143 CycleNode * CycleNode::getRMW() {
144         return hasRMW;
145 }
146
147 /**
148  * Set a RMW action node that reads from the current CycleNode.
149  * @param node The RMW that reads from the current node
150  * @return True, if this node already was read by another RMW; false otherwise
151  * @see CycleGraph::addRMWEdge
152  */
153 bool CycleNode::setRMW(CycleNode *node) {
154         CycleNode * oldhasRMW=hasRMW;
155         hasRMW=node;
156         return (oldhasRMW!=NULL);
157 }