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 *to, const ModelAction *from) {
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 *rmw, const ModelAction *from) {
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);
86 * Checks whether one CycleNode can reach another.
87 * @param from The CycleNode from which to begin exploration
88 * @param to The CycleNode to reach
89 * @return True, @a from can reach @a to; otherwise, false
91 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
92 std::vector<CycleNode *> queue;
93 HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
95 queue.push_back(from);
96 discovered.put(from, from);
97 while(!queue.empty()) {
98 CycleNode * node=queue.back();
103 for(unsigned int i=0;i<node->getEdges()->size();i++) {
104 CycleNode *next=(*node->getEdges())[i];
105 if (!discovered.contains(next)) {
106 discovered.put(next,next);
107 queue.push_back(next);
114 /** @returns whether a CycleGraph contains cycles. */
115 bool CycleGraph::checkForCycles() {
120 * Constructor for a CycleNode.
121 * @param modelaction The ModelAction for this node
123 CycleNode::CycleNode(const ModelAction *modelaction) :
129 /** @returns a vector of the edges from a CycleNode. */
130 std::vector<CycleNode *> * CycleNode::getEdges() {
135 * Adds an edge from this CycleNode to another CycleNode.
136 * @param node The node to which we add a directed edge
138 void CycleNode::addEdge(CycleNode *node) {
139 edges.push_back(node);
142 /** @returns the RMW CycleNode that reads from the current CycleNode */
143 CycleNode * CycleNode::getRMW() {
148 * Set a RMW action node that reads from the current CycleNode.
149 * @param node The RMW that reads from the current node
150 * @return True, if this node already was read by another RMW; false otherwise
151 * @see CycleGraph::addRMWEdge
153 bool CycleNode::setRMW(CycleNode *node) {
154 CycleNode * oldhasRMW=hasRMW;
156 return (oldhasRMW!=NULL);