1 #include "cyclegraph.h"
4 /** Initializes a CycleGraph object. */
5 CycleGraph::CycleGraph() :
10 /** CycleGraph destructor */
11 CycleGraph::~CycleGraph() {
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
19 CycleNode * CycleGraph::getNode(const ModelAction *action) {
20 CycleNode *node=actionToNode.get(action);
22 node=new CycleNode(action);
23 actionToNode.put(action, node);
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
34 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
35 CycleNode *fromnode=getNode(from);
36 CycleNode *tonode=getNode(to);
40 hasCycles=checkReachable(tonode, fromnode);
42 fromnode->addEdge(tonode);
44 CycleNode * rmwnode=fromnode->getRMW();
46 //If the fromnode has a rmwnode that is not the tonode, we
47 //should add an edge between its rmwnode and the tonode
49 if (rmwnode!=NULL&&rmwnode!=tonode) {
52 hasCycles=checkReachable(tonode, rmwnode);
54 rmwnode->addEdge(tonode);
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.
63 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
64 CycleNode *fromnode=getNode(from);
65 CycleNode *rmwnode=getNode(rmw);
67 /* Two RMW actions cannot read from the same write. */
68 if (fromnode->setRMW(rmwnode)) {
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
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);
81 fromnode->addEdge(rmwnode);
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
90 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) {
91 CycleNode *fromnode = actionToNode.get(from);
92 CycleNode *tonode = actionToNode.get(to);
94 if (!fromnode || !tonode)
97 return checkReachable(fromnode, tonode);
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
106 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
107 std::vector<CycleNode *> queue;
108 HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
110 queue.push_back(from);
111 discovered.put(from, from);
112 while(!queue.empty()) {
113 CycleNode * node=queue.back();
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);
129 /** @returns whether a CycleGraph contains cycles. */
130 bool CycleGraph::checkForCycles() {
135 * Constructor for a CycleNode.
136 * @param modelaction The ModelAction for this node
138 CycleNode::CycleNode(const ModelAction *modelaction) :
144 /** @returns a vector of the edges from a CycleNode. */
145 std::vector<CycleNode *> * CycleNode::getEdges() {
150 * Adds an edge from this CycleNode to another CycleNode.
151 * @param node The node to which we add a directed edge
153 void CycleNode::addEdge(CycleNode *node) {
154 edges.push_back(node);
157 /** @returns the RMW CycleNode that reads from the current CycleNode */
158 CycleNode * CycleNode::getRMW() {
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
168 bool CycleNode::setRMW(CycleNode *node) {
169 CycleNode * oldhasRMW=hasRMW;
171 return (oldhasRMW!=NULL);