update TODO's
[model-checker.git] / cyclegraph.cc
index c1fea4f3d0a96f14de433b6aac8b61e6f6479131..6676f2f0a7ea4a000f2555810cbc4f31f0112263 100644 (file)
@@ -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,53 @@ CycleNode * CycleGraph::getNode(ModelAction * action) {
        return node;
 }
 
-void CycleGraph::addEdge(ModelAction *from, ModelAction *to) {
+/** Adds an edge between two ModelActions. */
+void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
        CycleNode *fromnode=getNode(from);
        CycleNode *tonode=getNode(to);
        if (!hasCycles) {
                // Check for Cycles
-               hasCycles=checkReachable(fromnode, tonode);
+               hasCycles=checkReachable(tonode, fromnode);
        }
        fromnode->addEdge(tonode);
 }
 
+void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
+       CycleNode *fromnode=getNode(from);
+       CycleNode *rmwnode=getNode(rmw);
+
+       /* Two RMW actions cannot read from the same write. */
+       if (fromnode->setRMW())
+               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<CycleNode *> * edges=fromnode->getEdges();
+       for(unsigned int i=0;edges->size();i++) {
+               CycleNode * tonode=(*edges)[i];
+               rmwnode->addEdge(tonode);
+       }
+       if (!hasCycles) {
+               hasCycles=checkReachable(rmwnode, fromnode);
+       }
+       fromnode->addEdge(rmwnode);
+}
+
+
+/** Checks whether the first CycleNode can reach the second one. */
 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
-       std::vector<class CycleNode *> queue;
-       HashTable<class CycleNode *, class CycleNode *, uintptr_t, 4> discovered;
-       
+       std::vector<CycleNode *> queue;
+       HashTable<CycleNode *, CycleNode *, uintptr_t, 4> 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;i<node->getEdges()->size();i++) {
                        CycleNode *next=(*node->getEdges())[i];
                        if (!discovered.contains(next)) {
@@ -46,14 +77,28 @@ 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;
 }
 
-std::vector<class CycleNode *> * CycleNode::getEdges() {
+/** Returns a vector of the edges from a CycleNode. */
+std::vector<CycleNode *> * CycleNode::getEdges() {
        return &edges;
 }
 
+/** Adds an edge to a CycleNode. */
 void CycleNode::addEdge(CycleNode * node) {
        edges.push_back(node);
 }
+
+bool CycleNode::setRMW() {
+       bool oldhasRMW=hasRMW;
+       hasRMW=true;
+       return oldhasRMW;
+}