cyclegraph: propagate RMW atomicity edges down the chain
[model-checker.git] / cyclegraph.cc
index d2cac49c50913f8d81538160c854cd9fd60d3ad6..26ea215d434921f2b27fd69e07ef18d1e6029f54 100644 (file)
@@ -8,6 +8,7 @@
 /** Initializes a CycleGraph object. */
 CycleGraph::CycleGraph() :
        discovered(new HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free>(16)),
+       queue(new ModelVector<const CycleNode *>()),
        hasCycles(false),
        oldCycles(false)
 {
@@ -16,6 +17,7 @@ CycleGraph::CycleGraph() :
 /** CycleGraph destructor */
 CycleGraph::~CycleGraph()
 {
+       delete queue;
        delete discovered;
 }
 
@@ -196,29 +198,33 @@ bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node)
  */
 bool CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
 {
-       bool added;
-
-       if (!hasCycles)
-               hasCycles = checkReachable(tonode, fromnode);
-
-       if ((added = fromnode->addEdge(tonode)))
+       if (fromnode->addEdge(tonode)) {
                rollbackvector.push_back(fromnode);
+               if (!hasCycles)
+                       hasCycles = checkReachable(tonode, fromnode);
+       } else
+               return false; /* No new edge */
 
        /*
-        * If the fromnode has a rmwnode that is not the tonode, we should add
-        * an edge between its rmwnode and the tonode
+        * If the fromnode has a rmwnode that is not the tonode, we should
+        * follow its RMW chain to add an edge at the end, unless we encounter
+        * tonode along the way
         */
        CycleNode *rmwnode = fromnode->getRMW();
-       if (rmwnode && rmwnode != tonode) {
-               if (!hasCycles)
-                       hasCycles = checkReachable(tonode, rmwnode);
+       if (rmwnode) {
+               while (rmwnode != tonode && rmwnode->getRMW())
+                       rmwnode = rmwnode->getRMW();
+
+               if (rmwnode != tonode) {
+                       if (rmwnode->addEdge(tonode)) {
+                               if (!hasCycles)
+                                       hasCycles = checkReachable(tonode, rmwnode);
 
-               if (rmwnode->addEdge(tonode)) {
-                       rollbackvector.push_back(rmwnode);
-                       added = true;
+                               rollbackvector.push_back(rmwnode);
+                       }
                }
        }
-       return added;
+       return true;
 }
 
 /**
@@ -242,6 +248,9 @@ void CycleGraph::addRMWEdge(const T *from, const ModelAction *rmw)
        CycleNode *fromnode = getNode(from);
        CycleNode *rmwnode = getNode(rmw);
 
+       /* We assume that this RMW has no RMW reading from it yet */
+       ASSERT(!rmwnode->getRMW());
+
        /* Two RMW actions cannot read from the same write. */
        if (fromnode->setRMW(rmwnode))
                hasCycles = true;
@@ -386,22 +395,20 @@ void CycleGraph::dumpGraphToFile(const char *filename) const
  */
 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
 {
-       std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
        discovered->reset();
-
-       queue.push_back(from);
+       queue->clear();
+       queue->push_back(from);
        discovered->put(from, from);
-       while (!queue.empty()) {
-               const CycleNode *node = queue.back();
-               queue.pop_back();
+       while (!queue->empty()) {
+               const CycleNode *node = queue->back();
+               queue->pop_back();
                if (node == to)
                        return true;
-
                for (unsigned int i = 0; i < node->getNumEdges(); i++) {
                        CycleNode *next = node->getEdge(i);
                        if (!discovered->contains(next)) {
                                discovered->put(next, next);
-                               queue.push_back(next);
+                               queue->push_back(next);
                        }
                }
        }
@@ -438,15 +445,15 @@ template bool CycleGraph::checkReachable(const Promise *from,
 /** @return True, if the promise has failed; false otherwise */
 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
 {
-       std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
        discovered->reset();
+       queue->clear();
        CycleNode *from = actionToNode.get(fromact);
 
-       queue.push_back(from);
+       queue->push_back(from);
        discovered->put(from, from);
-       while (!queue.empty()) {
-               CycleNode *node = queue.back();
-               queue.pop_back();
+       while (!queue->empty()) {
+               const CycleNode *node = queue->back();
+               queue->pop_back();
 
                if (node->getPromise() == promise)
                        return true;
@@ -458,7 +465,7 @@ bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) cons
                        CycleNode *next = node->getEdge(i);
                        if (!discovered->contains(next)) {
                                discovered->put(next, next);
-                               queue.push_back(next);
+                               queue->push_back(next);
                        }
                }
        }
@@ -554,7 +561,7 @@ unsigned int CycleNode::getNumBackEdges() const
  * @return True if the element was found; false otherwise
  */
 template <typename T>
-static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
+static bool vector_remove_node(SnapVector<T>& v, const T n)
 {
        for (unsigned int i = 0; i < v.size(); i++) {
                if (v[i] == n) {