1 #include "cyclegraph.h"
5 /** Initializes a CycleGraph object. */
6 CycleGraph::CycleGraph() :
12 /** CycleGraph destructor */
13 CycleGraph::~CycleGraph() {
17 * @brief Returns the CycleNode corresponding to a given ModelAction
18 * @param action The ModelAction to find a node for
19 * @return The CycleNode paired with this action
21 CycleNode * CycleGraph::getNode(const ModelAction *action) {
22 CycleNode *node=actionToNode.get(action);
24 node=new CycleNode(action);
25 actionToNode.put(action, node);
31 * Adds an edge between two ModelActions. The ModelAction @a to is ordered
32 * after the ModelAction @a from.
33 * @param to The edge points to this ModelAction
34 * @param from The edge comes from this ModelAction
36 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
40 CycleNode *fromnode=getNode(from);
41 CycleNode *tonode=getNode(to);
45 hasCycles=checkReachable(tonode, fromnode);
48 rollbackvector.push_back(fromnode);
49 fromnode->addEdge(tonode);
51 CycleNode * rmwnode=fromnode->getRMW();
53 //If the fromnode has a rmwnode that is not the tonode, we
54 //should add an edge between its rmwnode and the tonode
56 if (rmwnode!=NULL&&rmwnode!=tonode) {
59 hasCycles=checkReachable(tonode, rmwnode);
61 rollbackvector.push_back(rmwnode);
62 rmwnode->addEdge(tonode);
66 /** Handles special case of a RMW action. The ModelAction rmw reads
67 * from the ModelAction from. The key differences are: (1) no write
68 * can occur in between the rmw and the from action. Only one RMW
69 * action can read from a given write.
71 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
75 CycleNode *fromnode=getNode(from);
76 CycleNode *rmwnode=getNode(rmw);
78 /* Two RMW actions cannot read from the same write. */
79 if (fromnode->setRMW(rmwnode)) {
82 rmwrollbackvector.push_back(fromnode);
85 /* Transfer all outgoing edges from the from node to the rmw node */
86 /* This process cannot add a cycle because rmw should not have any
88 std::vector<CycleNode *> * edges=fromnode->getEdges();
89 for(unsigned int i=0;i<edges->size();i++) {
90 CycleNode * tonode=(*edges)[i];
91 rollbackvector.push_back(rmwnode);
92 rmwnode->addEdge(tonode);
94 rollbackvector.push_back(fromnode);
95 fromnode->addEdge(rmwnode);
99 * Checks whether one ModelAction can reach another.
100 * @param from The ModelAction from which to begin exploration
101 * @param to The ModelAction to reach
102 * @return True, @a from can reach @a to; otherwise, false
104 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) {
105 CycleNode *fromnode = actionToNode.get(from);
106 CycleNode *tonode = actionToNode.get(to);
108 if (!fromnode || !tonode)
111 return checkReachable(fromnode, tonode);
115 * Checks whether one CycleNode can reach another.
116 * @param from The CycleNode from which to begin exploration
117 * @param to The CycleNode to reach
118 * @return True, @a from can reach @a to; otherwise, false
120 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
121 std::vector<CycleNode *> queue;
122 HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
124 queue.push_back(from);
125 discovered.put(from, from);
126 while(!queue.empty()) {
127 CycleNode * node=queue.back();
132 for(unsigned int i=0;i<node->getEdges()->size();i++) {
133 CycleNode *next=(*node->getEdges())[i];
134 if (!discovered.contains(next)) {
135 discovered.put(next,next);
136 queue.push_back(next);
143 void CycleGraph::startChanges() {
144 ASSERT(rollbackvector.size()==0);
145 ASSERT(rmwrollbackvector.size()==0);
146 ASSERT(oldCycles==hasCycles);
149 /** Commit changes to the cyclegraph. */
150 void CycleGraph::commitChanges() {
151 rollbackvector.resize(0);
152 rmwrollbackvector.resize(0);
156 /** Rollback changes to the previous commit. */
157 void CycleGraph::rollbackChanges() {
158 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
159 rollbackvector[i]->popEdge();
162 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
163 rmwrollbackvector[i]->clearRMW();
166 hasCycles = oldCycles;
167 rollbackvector.resize(0);
168 rmwrollbackvector.resize(0);
171 /** @returns whether a CycleGraph contains cycles. */
172 bool CycleGraph::checkForCycles() {
177 * Constructor for a CycleNode.
178 * @param modelaction The ModelAction for this node
180 CycleNode::CycleNode(const ModelAction *modelaction) :
186 /** @returns a vector of the edges from a CycleNode. */
187 std::vector<CycleNode *> * CycleNode::getEdges() {
192 * Adds an edge from this CycleNode to another CycleNode.
193 * @param node The node to which we add a directed edge
195 void CycleNode::addEdge(CycleNode *node) {
196 edges.push_back(node);
199 /** @returns the RMW CycleNode that reads from the current CycleNode */
200 CycleNode * CycleNode::getRMW() {
205 * Set a RMW action node that reads from the current CycleNode.
206 * @param node The RMW that reads from the current node
207 * @return True, if this node already was read by another RMW; false otherwise
208 * @see CycleGraph::addRMWEdge
210 bool CycleNode::setRMW(CycleNode *node) {