1 #include "cyclegraph.h"
4 /** Initializes a CycleGraph object. */
5 CycleGraph::CycleGraph() :
11 /** CycleGraph destructor */
12 CycleGraph::~CycleGraph() {
16 * @brief Returns the CycleNode corresponding to a given ModelAction
17 * @param action The ModelAction to find a node for
18 * @return The CycleNode paired with this action
20 CycleNode * CycleGraph::getNode(const ModelAction *action) {
21 CycleNode *node=actionToNode.get(action);
23 node=new CycleNode(action);
24 actionToNode.put(action, node);
30 * Adds an edge between two ModelActions. The ModelAction @a to is ordered
31 * after the ModelAction @a from.
32 * @param to The edge points to this ModelAction
33 * @param from The edge comes from this ModelAction
35 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
36 CycleNode *fromnode=getNode(from);
37 CycleNode *tonode=getNode(to);
41 hasCycles=checkReachable(tonode, fromnode);
44 rollbackvector.push_back(fromnode);
45 fromnode->addEdge(tonode);
47 CycleNode * rmwnode=fromnode->getRMW();
49 //If the fromnode has a rmwnode that is not the tonode, we
50 //should add an edge between its rmwnode and the tonode
52 if (rmwnode!=NULL&&rmwnode!=tonode) {
55 hasCycles=checkReachable(tonode, rmwnode);
57 rollbackvector.push_back(rmwnode);
58 rmwnode->addEdge(tonode);
62 /** Handles special case of a RMW action. The ModelAction rmw reads
63 * from the ModelAction from. The key differences are: (1) no write
64 * can occur in between the rmw and the from action. Only one RMW
65 * action can read from a given write.
67 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
68 CycleNode *fromnode=getNode(from);
69 CycleNode *rmwnode=getNode(rmw);
71 /* Two RMW actions cannot read from the same write. */
72 if (fromnode->setRMW(rmwnode)) {
75 rmwrollbackvector.push_back(fromnode);
78 /* Transfer all outgoing edges from the from node to the rmw node */
79 /* This process cannot add a cycle because rmw should not have any
81 std::vector<CycleNode *> * edges=fromnode->getEdges();
82 for(unsigned int i=0;i<edges->size();i++) {
83 CycleNode * tonode=(*edges)[i];
84 rollbackvector.push_back(rmwnode);
85 rmwnode->addEdge(tonode);
87 rollbackvector.push_back(fromnode);
88 fromnode->addEdge(rmwnode);
92 * Checks whether one ModelAction can reach another.
93 * @param from The ModelAction from which to begin exploration
94 * @param to The ModelAction to reach
95 * @return True, @a from can reach @a to; otherwise, false
97 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) {
98 CycleNode *fromnode = actionToNode.get(from);
99 CycleNode *tonode = actionToNode.get(to);
101 if (!fromnode || !tonode)
104 return checkReachable(fromnode, tonode);
108 * Checks whether one CycleNode can reach another.
109 * @param from The CycleNode from which to begin exploration
110 * @param to The CycleNode to reach
111 * @return True, @a from can reach @a to; otherwise, false
113 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
114 std::vector<CycleNode *> queue;
115 HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
117 queue.push_back(from);
118 discovered.put(from, from);
119 while(!queue.empty()) {
120 CycleNode * node=queue.back();
125 for(unsigned int i=0;i<node->getEdges()->size();i++) {
126 CycleNode *next=(*node->getEdges())[i];
127 if (!discovered.contains(next)) {
128 discovered.put(next,next);
129 queue.push_back(next);
136 /** Commit changes to the cyclegraph. */
137 void CycleGraph::commitChanges() {
138 rollbackvector.resize(0);
139 rmwrollbackvector.resize(0);
143 /** Rollback changes to the previous commit. */
144 void CycleGraph::rollbackChanges() {
145 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
146 rollbackvector[i]->popEdge();
149 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
150 rmwrollbackvector[i]->clearRMW();
153 hasCycles = oldCycles;
154 rollbackvector.resize(0);
155 rmwrollbackvector.resize(0);
158 /** @returns whether a CycleGraph contains cycles. */
159 bool CycleGraph::checkForCycles() {
164 * Constructor for a CycleNode.
165 * @param modelaction The ModelAction for this node
167 CycleNode::CycleNode(const ModelAction *modelaction) :
173 /** @returns a vector of the edges from a CycleNode. */
174 std::vector<CycleNode *> * CycleNode::getEdges() {
179 * Adds an edge from this CycleNode to another CycleNode.
180 * @param node The node to which we add a directed edge
182 void CycleNode::addEdge(CycleNode *node) {
183 edges.push_back(node);
186 /** @returns the RMW CycleNode that reads from the current CycleNode */
187 CycleNode * CycleNode::getRMW() {
192 * Set a RMW action node that reads from the current CycleNode.
193 * @param node The RMW that reads from the current node
194 * @return True, if this node already was read by another RMW; false otherwise
195 * @see CycleGraph::addRMWEdge
197 bool CycleNode::setRMW(CycleNode *node) {