cyclegraph: flag cycles for reflexive edges
[model-checker.git] / cyclegraph.cc
index f4d3b93314b4ad22dba40d4d56043fd5643c01d1..e1d5b3c50ef1a243eac7c2955e55d0b4a6b0e67d 100644 (file)
@@ -1,6 +1,8 @@
 #include "cyclegraph.h"
 #include "action.h"
 #include "common.h"
+#include "promise.h"
+#include "model.h"
 
 /** Initializes a CycleGraph object. */
 CycleGraph::CycleGraph() :
@@ -41,11 +43,14 @@ CycleNode * CycleGraph::getNode(const ModelAction *action) {
 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
        ASSERT(from);
        ASSERT(to);
-       ASSERT(from != to);
 
        CycleNode *fromnode=getNode(from);
        CycleNode *tonode=getNode(to);
 
+       if (!hasCycles) {
+               // Reflexive edges are cycles
+               hasCycles = (from == to);
+       }
        if (!hasCycles) {
                // Check for Cycles
                hasCycles=checkReachable(tonode, fromnode);
@@ -83,7 +88,6 @@ void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
        ASSERT(from);
        ASSERT(rmw);
-       ASSERT(from != rmw);
 
        CycleNode *fromnode=getNode(from);
        CycleNode *rmwnode=getNode(rmw);
@@ -112,6 +116,10 @@ void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
        }
 
 
+       if (!hasCycles) {
+               // Reflexive edges are cycles
+               hasCycles = (from == rmw);
+       }
        if (!hasCycles) {
                // With promises we could be setting up a cycle here if we aren't
                // careful...avoid it..
@@ -195,6 +203,33 @@ bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
        return false;
 }
 
+bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) {
+       std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
+       HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> discovered(64);
+       CycleNode *from = actionToNode.get(fromact);
+
+
+       queue.push_back(from);
+       discovered.put(from, from);
+       while(!queue.empty()) {
+               CycleNode * node=queue.back();
+               queue.pop_back();
+
+               if (promise->increment_threads(node->getAction()->get_tid())) {
+                       return true;
+               }
+
+               for(unsigned int i=0;i<node->getEdges()->size();i++) {
+                       CycleNode *next=(*node->getEdges())[i];
+                       if (!discovered.contains(next)) {
+                               discovered.put(next,next);
+                               queue.push_back(next);
+                       }
+               }
+       }
+       return false;
+}
+
 void CycleGraph::startChanges() {
        ASSERT(rollbackvector.size()==0);
        ASSERT(rmwrollbackvector.size()==0);