X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=cyclegraph.cc;h=f060a00e74362b6f9df99649ffd844f7e416d4b9;hb=bdef0741b8a01e16946d261bc2a657af5a683b3e;hp=c1fea4f3d0a96f14de433b6aac8b61e6f6479131;hpb=f7e1577b22ac26c12dc57a43de2761aa9249227c;p=model-checker.git diff --git a/cyclegraph.cc b/cyclegraph.cc index c1fea4f..f060a00 100644 --- a/cyclegraph.cc +++ b/cyclegraph.cc @@ -1,10 +1,16 @@ #include "cyclegraph.h" +#include "action.h" +/** Initializes a CycleGraph object. */ CycleGraph::CycleGraph() { hasCycles=false; } -CycleNode * CycleGraph::getNode(ModelAction * action) { +CycleGraph::~CycleGraph() { +} + +/** Returns the CycleNode for a given ModelAction. */ +CycleNode * CycleGraph::getNode(const ModelAction * action) { CycleNode *node=actionToNode.get(action); if (node==NULL) { node=new CycleNode(action); @@ -13,28 +19,74 @@ CycleNode * CycleGraph::getNode(ModelAction * action) { return node; } -void CycleGraph::addEdge(ModelAction *from, ModelAction *to) { +/** + * Adds an edge between two ModelActions. The ModelAction to happens after the + * ModelAction from. + */ +void CycleGraph::addEdge(const ModelAction *to, const ModelAction *from) { CycleNode *fromnode=getNode(from); CycleNode *tonode=getNode(to); + if (!hasCycles) { // Check for Cycles - hasCycles=checkReachable(fromnode, tonode); + hasCycles=checkReachable(tonode, fromnode); } fromnode->addEdge(tonode); + + CycleNode * rmwnode=fromnode->getRMW(); + + //If the fromnode has a rmwnode that is not the tonode, we + //should add an edge between its rmwnode and the tonode + + if (rmwnode!=NULL&&rmwnode!=tonode) { + if (!hasCycles) { + // Check for Cycles + hasCycles=checkReachable(tonode, rmwnode); + } + rmwnode->addEdge(tonode); + } } +/** Handles special case of a RMW action. The ModelAction rmw reads + * from the ModelAction from. The key differences are: (1) no write + * can occur in between the rmw and the from action. Only one RMW + * action can read from a given write. + */ +void CycleGraph::addRMWEdge(const ModelAction *rmw, const ModelAction * from) { + CycleNode *fromnode=getNode(from); + CycleNode *rmwnode=getNode(rmw); + + /* Two RMW actions cannot read from the same write. */ + if (fromnode->setRMW(rmwnode)) { + hasCycles=true; + } + + /* Transfer all outgoing edges from the from node to the rmw node */ + /* This process cannot add a cycle because rmw should not have any + incoming edges yet.*/ + std::vector * edges=fromnode->getEdges(); + for(unsigned int i=0;isize();i++) { + CycleNode * tonode=(*edges)[i]; + rmwnode->addEdge(tonode); + } + + fromnode->addEdge(rmwnode); +} + + +/** Checks whether the first CycleNode can reach the second one. */ bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) { - std::vector queue; - HashTable discovered; - + std::vector queue; + HashTable discovered; + queue.push_back(from); discovered.put(from, from); while(!queue.empty()) { - class CycleNode * node=queue.back(); + CycleNode * node=queue.back(); queue.pop_back(); if (node==to) return true; - + for(unsigned int i=0;igetEdges()->size();i++) { CycleNode *next=(*node->getEdges())[i]; if (!discovered.contains(next)) { @@ -46,14 +98,35 @@ bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) { return false; } -CycleNode::CycleNode(ModelAction *modelaction) { +/** Returns whether a CycleGraph contains cycles. */ +bool CycleGraph::checkForCycles() { + return hasCycles; +} + +/** Constructor for a CycleNode. */ +CycleNode::CycleNode(const ModelAction *modelaction) { action=modelaction; + hasRMW=NULL; } -std::vector * CycleNode::getEdges() { +/** Returns a vector of the edges from a CycleNode. */ +std::vector * CycleNode::getEdges() { return &edges; } +/** Adds an edge to a CycleNode. */ void CycleNode::addEdge(CycleNode * node) { edges.push_back(node); } + +/** Get the RMW CycleNode that reads from the current CycleNode. */ +CycleNode* CycleNode::getRMW() { + return hasRMW; +} + +/** Set a RMW action node that reads from the current CycleNode. */ +bool CycleNode::setRMW(CycleNode * node) { + CycleNode * oldhasRMW=hasRMW; + hasRMW=node; + return (oldhasRMW!=NULL); +}