optimize hashtable so we don't always build new ones for cyclegraph check
[model-checker.git] / cyclegraph.cc
index ecf8a781c2770ebd09766cff47030e3e094bd2d7..ead70ae6b38c2c687ab34c30db9659d57b68e110 100644 (file)
@@ -1,9 +1,12 @@
 #include "cyclegraph.h"
 #include "action.h"
 #include "common.h"
+#include "promise.h"
+#include "model.h"
 
 /** Initializes a CycleGraph object. */
 CycleGraph::CycleGraph() :
+       discovered(new HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free>(16)),
        hasCycles(false),
        oldCycles(false),
        hasRMWViolation(false),
@@ -45,6 +48,10 @@ void CycleGraph::addEdge(const ModelAction *from, const ModelAction *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);
@@ -110,22 +117,22 @@ 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..
                hasCycles=checkReachable(rmwnode, fromnode);
        }
-       if(fromnode->addEdge(rmwnode))
+       if (fromnode->addEdge(rmwnode))
                rollbackvector.push_back(fromnode);
 }
 
 #if SUPPORT_MOD_ORDER_DUMP
-void CycleGraph::dumpGraphToFile(const char *filename) {
-       char buffer[200];
-  sprintf(buffer, "%s.dot",filename);
-  FILE *file=fopen(buffer, "w");
-  fprintf(file, "digraph %s {\n",filename);
-  for(unsigned int i=0;i<nodeList.size();i++) {
+void CycleGraph::dumpNodes(FILE *file) {
+       for (unsigned int i=0;i<nodeList.size();i++) {
                CycleNode *cn=nodeList[i];
                std::vector<CycleNode *> * edges=cn->getEdges();
                const ModelAction *action=cn->getAction();
@@ -133,14 +140,22 @@ void CycleGraph::dumpGraphToFile(const char *filename) {
                if (cn->getRMW()!=NULL) {
                        fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
                }
-               for(unsigned int j=0;j<edges->size();j++) {
-                 CycleNode *dst=(*edges)[j];
+               for (unsigned int j=0;j<edges->size();j++) {
+                       CycleNode *dst=(*edges)[j];
                        const ModelAction *dstaction=dst->getAction();
-      fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
-         }
+                       fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
+               }
        }
-  fprintf(file,"}\n");
-  fclose(file);        
+}
+
+void CycleGraph::dumpGraphToFile(const char *filename) {
+       char buffer[200];
+       sprintf(buffer, "%s.dot",filename);
+       FILE *file=fopen(buffer, "w");
+       fprintf(file, "digraph %s {\n",filename);
+       dumpNodes(file);
+       fprintf(file,"}\n");
+       fclose(file);   
 }
 #endif
 
@@ -167,11 +182,11 @@ bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to)
  * @return True, @a from can reach @a to; otherwise, false
  */
 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
-       std::vector<CycleNode *, MyAlloc<CycleNode *> > queue;
-       HashTable<CycleNode *, CycleNode *, uintptr_t, 4, MYMALLOC, MYCALLOC, MYFREE> discovered;
+       std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
+       discovered->reset();
 
        queue.push_back(from);
-       discovered.put(from, from);
+       discovered->put(from, from);
        while(!queue.empty()) {
                CycleNode * node=queue.back();
                queue.pop_back();
@@ -180,8 +195,35 @@ bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
 
                for(unsigned int i=0;i<node->getEdges()->size();i++) {
                        CycleNode *next=(*node->getEdges())[i];
-                       if (!discovered.contains(next)) {
-                               discovered.put(next,next);
+                       if (!discovered->contains(next)) {
+                               discovered->put(next,next);
+                               queue.push_back(next);
+                       }
+               }
+       }
+       return false;
+}
+
+bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) {
+       std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
+       discovered->reset();
+       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);
                        }
                }