action: remove clockvector flag from print() method
[model-checker.git] / model.cc
index 9371b89676846d13d310412fb9ac4840c61862fb..e3d9203eea60ec53010d67bb87da20331cfbf642 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -12,6 +12,7 @@
 #include "promise.h"
 #include "datarace.h"
 #include "mutex.h"
+#include "threads.h"
 
 #define INITIAL_THREAD_ID      0
 
@@ -177,7 +178,7 @@ Thread * ModelChecker::get_next_thread(ModelAction *curr)
        } else {
                tid = next->get_tid();
        }
-       DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
+       DEBUG("*** ModelChecker chose next thread = %d ***\n", id_to_int(tid));
        ASSERT(tid != THREAD_ID_T_NONE);
        return thread_map->get(id_to_int(tid));
 }
@@ -198,7 +199,7 @@ bool ModelChecker::next_execution()
        if (isfinalfeasible()) {
                printf("Earliest divergence point since last feasible execution:\n");
                if (earliest_diverge)
-                       earliest_diverge->print(false);
+                       earliest_diverge->print();
                else
                        printf("(Not set)\n");
 
@@ -323,7 +324,8 @@ void ModelChecker::set_backtracking(ModelAction *act)
                if (!node->set_backtrack(tid))
                        continue;
                DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
-                                       prev->get_tid(), t->get_id());
+                                       id_to_int(prev->get_tid()),
+                                       id_to_int(t->get_id()));
                if (DBG_ENABLED()) {
                        prev->print();
                        act->print();
@@ -793,8 +795,7 @@ bool ModelChecker::isfinalfeasible() {
 
 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
-       int tid = id_to_int(act->get_tid());
-       ModelAction *lastread = get_last_action(tid);
+       ModelAction *lastread = get_last_action(act->get_tid());
        lastread->process_rmw(act);
        if (act->is_rmw() && lastread->get_reads_from()!=NULL) {
                mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
@@ -940,7 +941,11 @@ bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf
                                        }
                                } else {
                                        const ModelAction *prevreadfrom = act->get_reads_from();
-                                       if (prevreadfrom != NULL && rf != prevreadfrom) {
+                                       //if the previous read is unresolved, keep going...
+                                       if (prevreadfrom == NULL)
+                                               continue;
+
+                                       if (rf != prevreadfrom) {
                                                mo_graph->addEdge(prevreadfrom, rf);
                                                added = true;
                                        }
@@ -1065,14 +1070,22 @@ bool ModelChecker::w_modification_order(ModelAction *curr)
                        ModelAction *act = *rit;
                        if (act == curr) {
                                /*
-                                * If RMW, we already have all relevant edges,
-                                * so just skip to next thread.
-                                * If normal write, we need to look at earlier
-                                * actions, so continue processing list.
+                                * 1) If RMW and it actually read from something, then we
+                                * already have all relevant edges, so just skip to next
+                                * thread.
+                                * 
+                                * 2) If RMW and it didn't read from anything, we should
+                                * whatever edge we can get to speed up convergence.
+                                *
+                                * 3) If normal write, we need to look at earlier actions, so
+                                * continue processing list.
                                 */
-                               if (curr->is_rmw())
-                                       break;
-                               else
+                               if (curr->is_rmw()) {
+                                       if (curr->get_reads_from()!=NULL)
+                                               break;
+                                       else 
+                                               continue;
+                               } else
                                        continue;
                        }
 
@@ -1089,8 +1102,12 @@ bool ModelChecker::w_modification_order(ModelAction *curr)
                                 */
                                if (act->is_write())
                                        mo_graph->addEdge(act, curr);
-                               else if (act->is_read() && act->get_reads_from() != NULL)
+                               else if (act->is_read()) { 
+                                       //if previous read accessed a null, just keep going
+                                       if (act->get_reads_from() == NULL)
+                                               continue;
                                        mo_graph->addEdge(act->get_reads_from(), curr);
+                               }
                                added = true;
                                break;
                        } else if (act->is_read() && !act->is_synchronizing(curr) &&
@@ -1159,7 +1176,7 @@ bool ModelChecker::thin_air_constraint_may_allow(const ModelAction * writer, con
  * @return true, if the ModelChecker is certain that release_heads is complete;
  * false otherwise
  */
-bool ModelChecker::release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const
+bool ModelChecker::release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads) const
 {
        /* Only check for release sequences if there are no cycles */
        if (mo_graph->checkForCycles())
@@ -1229,7 +1246,7 @@ bool ModelChecker::release_seq_head(const ModelAction *rf, rel_heads_list_t *rel
 
                ModelAction *last = get_last_action(int_to_id(i));
                if (last && (rf->happens_before(last) ||
-                               last->get_type() == THREAD_FINISH))
+                               get_thread(int_to_id(i))->is_complete()))
                        future_ordered = true;
 
                for (rit = list->rbegin(); rit != list->rend(); rit++) {
@@ -1281,13 +1298,13 @@ bool ModelChecker::release_seq_head(const ModelAction *rf, rel_heads_list_t *rel
  * @param act The 'acquire' action that may read from a release sequence
  * @param release_heads A pass-by-reference return parameter. Will be filled
  * with the head(s) of the release sequence(s), if they exists with certainty.
- * @see ModelChecker::release_seq_head
+ * @see ModelChecker::release_seq_heads
  */
 void ModelChecker::get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads)
 {
        const ModelAction *rf = act->get_reads_from();
        bool complete;
-       complete = release_seq_head(rf, release_heads);
+       complete = release_seq_heads(rf, release_heads);
        if (!complete) {
                /* add act to 'lazy checking' list */
                pending_acq_rel_seq->push_back(act);
@@ -1323,7 +1340,7 @@ bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_
                const ModelAction *rf = act->get_reads_from();
                rel_heads_list_t release_heads;
                bool complete;
-               complete = release_seq_head(rf, &release_heads);
+               complete = release_seq_heads(rf, &release_heads);
                for (unsigned int i = 0; i < release_heads.size(); i++) {
                        if (!act->has_synchronized_with(release_heads[i])) {
                                if (act->synchronize_with(release_heads[i]))
@@ -1634,7 +1651,8 @@ void ModelChecker::dumpGraph(char *filename) {
                ModelAction *action=*it;
                if (action->is_read()) {
                        fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(),action->get_seq_number(), action->get_tid());
-                       fprintf(file, "N%u -> N%u[label=\"rf\", color=red];\n", action->get_seq_number(), action->get_reads_from()->get_seq_number());
+                       if (action->get_reads_from()!=NULL)
+                               fprintf(file, "N%u -> N%u[label=\"rf\", color=red];\n", action->get_seq_number(), action->get_reads_from()->get_seq_number());
                }
                if (thread_array[action->get_tid()] != NULL) {
                        fprintf(file, "N%u -> N%u[label=\"sb\", color=blue];\n", thread_array[action->get_tid()]->get_seq_number(), action->get_seq_number());
@@ -1690,6 +1708,26 @@ void ModelChecker::remove_thread(Thread *t)
        scheduler->remove_thread(t);
 }
 
+/**
+ * @brief Get a Thread reference by its ID
+ * @param tid The Thread's ID
+ * @return A Thread reference
+ */
+Thread * ModelChecker::get_thread(thread_id_t tid) const
+{
+       return thread_map->get(id_to_int(tid));
+}
+
+/**
+ * @brief Get a reference to the Thread in which a ModelAction was executed
+ * @param act The ModelAction
+ * @return A Thread reference
+ */
+Thread * ModelChecker::get_thread(ModelAction *act) const
+{
+       return get_thread(act->get_tid());
+}
+
 /**
  * Switch from a user-context to the "master thread" context (a.k.a. system
  * context). This switch is made with the intention of exploring a particular
@@ -1718,7 +1756,7 @@ bool ModelChecker::take_step() {
        if (has_asserted())
                return false;
 
-       Thread * curr = thread_current();
+       Thread *curr = thread_current();
        if (curr) {
                if (curr->get_state() == THREAD_READY) {
                        ASSERT(priv->current_action);
@@ -1732,22 +1770,23 @@ bool ModelChecker::take_step() {
                        ASSERT(false);
                }
        }
-       Thread * next = scheduler->next_thread(priv->nextThread);
+       Thread *next = scheduler->next_thread(priv->nextThread);
 
        /* Infeasible -> don't take any more steps */
        if (!isfeasible())
                return false;
 
-       if (next)
-               next->set_state(THREAD_RUNNING);
-       DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
+       DEBUG("(%d, %d)\n", curr ? id_to_int(curr->get_id()) : -1,
+                       next ? id_to_int(next->get_id()) : -1);
 
        /* next == NULL -> don't take any more steps */
        if (!next)
                return false;
 
-       if ( next->get_pending() != NULL ) {
-               //restart a pending action
+       next->set_state(THREAD_RUNNING);
+
+       if (next->get_pending() != NULL) {
+               /* restart a pending action */
                set_current_action(next->get_pending());
                next->set_pending(NULL);
                next->set_state(THREAD_READY);