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 /** Commit changes to the cyclegraph. */
144 void CycleGraph::commitChanges() {
145 rollbackvector.resize(0);
146 rmwrollbackvector.resize(0);
150 /** Rollback changes to the previous commit. */
151 void CycleGraph::rollbackChanges() {
152 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
153 rollbackvector[i]->popEdge();
156 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
157 rmwrollbackvector[i]->clearRMW();
160 hasCycles = oldCycles;
161 rollbackvector.resize(0);
162 rmwrollbackvector.resize(0);
165 /** @returns whether a CycleGraph contains cycles. */
166 bool CycleGraph::checkForCycles() {
171 * Constructor for a CycleNode.
172 * @param modelaction The ModelAction for this node
174 CycleNode::CycleNode(const ModelAction *modelaction) :
180 /** @returns a vector of the edges from a CycleNode. */
181 std::vector<CycleNode *> * CycleNode::getEdges() {
186 * Adds an edge from this CycleNode to another CycleNode.
187 * @param node The node to which we add a directed edge
189 void CycleNode::addEdge(CycleNode *node) {
190 edges.push_back(node);
193 /** @returns the RMW CycleNode that reads from the current CycleNode */
194 CycleNode * CycleNode::getRMW() {
199 * Set a RMW action node that reads from the current CycleNode.
200 * @param node The RMW that reads from the current node
201 * @return True, if this node already was read by another RMW; false otherwise
202 * @see CycleGraph::addRMWEdge
204 bool CycleNode::setRMW(CycleNode *node) {