model: maintain a count of the pending lazy synchronizations
[c11tester.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 *from, const ModelAction *to) {
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 *from, const ModelAction *rmw) {
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  * Checks whether one ModelAction can reach another.
86  * @param from The ModelAction from which to begin exploration
87  * @param to The ModelAction to reach
88  * @return True, @a from can reach @a to; otherwise, false
89  */
90 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) {
91         CycleNode *fromnode = actionToNode.get(from);
92         CycleNode *tonode = actionToNode.get(to);
93
94         if (!fromnode || !tonode)
95                 return false;
96
97         return checkReachable(fromnode, tonode);
98 }
99
100 /**
101  * Checks whether one CycleNode can reach another.
102  * @param from The CycleNode from which to begin exploration
103  * @param to The CycleNode to reach
104  * @return True, @a from can reach @a to; otherwise, false
105  */
106 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
107         std::vector<CycleNode *> queue;
108         HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
109
110         queue.push_back(from);
111         discovered.put(from, from);
112         while(!queue.empty()) {
113                 CycleNode * node=queue.back();
114                 queue.pop_back();
115                 if (node==to)
116                         return true;
117
118                 for(unsigned int i=0;i<node->getEdges()->size();i++) {
119                         CycleNode *next=(*node->getEdges())[i];
120                         if (!discovered.contains(next)) {
121                                 discovered.put(next,next);
122                                 queue.push_back(next);
123                         }
124                 }
125         }
126         return false;
127 }
128
129 /** @returns whether a CycleGraph contains cycles. */
130 bool CycleGraph::checkForCycles() {
131         return hasCycles;
132 }
133
134 /**
135  * Constructor for a CycleNode.
136  * @param modelaction The ModelAction for this node
137  */
138 CycleNode::CycleNode(const ModelAction *modelaction) :
139         action(modelaction),
140         hasRMW(NULL)
141 {
142 }
143
144 /** @returns a vector of the edges from a CycleNode. */
145 std::vector<CycleNode *> * CycleNode::getEdges() {
146         return &edges;
147 }
148
149 /**
150  * Adds an edge from this CycleNode to another CycleNode.
151  * @param node The node to which we add a directed edge
152  */
153 void CycleNode::addEdge(CycleNode *node) {
154         edges.push_back(node);
155 }
156
157 /** @returns the RMW CycleNode that reads from the current CycleNode */
158 CycleNode * CycleNode::getRMW() {
159         return hasRMW;
160 }
161
162 /**
163  * Set a RMW action node that reads from the current CycleNode.
164  * @param node The RMW that reads from the current node
165  * @return True, if this node already was read by another RMW; false otherwise
166  * @see CycleGraph::addRMWEdge
167  */
168 bool CycleNode::setRMW(CycleNode *node) {
169         CycleNode * oldhasRMW=hasRMW;
170         hasRMW=node;
171         return (oldhasRMW!=NULL);
172 }