Merge remote-tracking branch 'origin/master'
[model-checker.git] / model.cc
index 4f600a91f5eab4bb65420974e78651f0e53cfcfb..39a7c2170f59fb0ac9b3a7d2160ecb80027283ff 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
 
@@ -99,6 +100,12 @@ int ModelChecker::get_num_threads()
        return priv->next_thread_id;
 }
 
+/** @return The currently executing Thread. */
+Thread * ModelChecker::get_current_thread()
+{
+       return scheduler->get_current_thread();
+}
+
 /** @return a sequence number for a new ModelAction */
 modelclock_t ModelChecker::get_next_seq_num()
 {
@@ -188,6 +195,7 @@ bool ModelChecker::next_execution()
        DBG();
 
        num_executions++;
+
        if (isfinalfeasible()) {
                printf("Earliest divergence point since last feasible execution:\n");
                if (earliest_diverge)
@@ -199,6 +207,9 @@ bool ModelChecker::next_execution()
                num_feasible_executions++;
        }
 
+       DEBUG("Number of acquires waiting on pending release sequences: %lu\n",
+                       pending_acq_rel_seq->size());
+
        if (isfinalfeasible() || DBG_ENABLED())
                print_summary();
 
@@ -475,11 +486,11 @@ bool ModelChecker::process_write(ModelAction *curr)
  * (e.g., ATOMIC_{READ,WRITE,RMW,LOCK}, etc.)
  *
  * @param curr The current action
- * @return True if synchronization was updated
+ * @return True if synchronization was updated or a thread completed
  */
 bool ModelChecker::process_thread_action(ModelAction *curr)
 {
-       bool synchronized = false;
+       bool updated = false;
 
        switch (curr->get_type()) {
        case THREAD_CREATE: {
@@ -488,19 +499,29 @@ bool ModelChecker::process_thread_action(ModelAction *curr)
                break;
        }
        case THREAD_JOIN: {
-               Thread *blocking = (Thread *)curr->get_location();
-               ModelAction *act = get_last_action(blocking->get_id());
-               curr->synchronize_with(act);
-               synchronized = true;
+               Thread *waiting, *blocking;
+               waiting = get_thread(curr);
+               blocking = (Thread *)curr->get_location();
+               if (!blocking->is_complete()) {
+                       blocking->push_wait_list(curr);
+                       scheduler->sleep(waiting);
+               } else {
+                       do_complete_join(curr);
+                       updated = true; /* trigger rel-seq checks */
+               }
                break;
        }
        case THREAD_FINISH: {
                Thread *th = get_thread(curr);
                while (!th->wait_list_empty()) {
                        ModelAction *act = th->pop_wait_list();
-                       scheduler->wake(get_thread(act));
+                       Thread *wake = get_thread(act);
+                       scheduler->wake(wake);
+                       do_complete_join(act);
+                       updated = true; /* trigger rel-seq checks */
                }
                th->complete();
+               updated = true; /* trigger rel-seq checks */
                break;
        }
        case THREAD_START: {
@@ -511,7 +532,7 @@ bool ModelChecker::process_thread_action(ModelAction *curr)
                break;
        }
 
-       return synchronized;
+       return updated;
 }
 
 /**
@@ -570,12 +591,9 @@ ModelAction * ModelChecker::initialize_curr_action(ModelAction *curr)
 }
 
 /**
- * @brief Check whether a model action is enabled.
- *
- * Checks whether a lock or join operation would be successful (i.e., is the
- * lock already locked, or is the joined thread already complete). If not, put
- * the action in a waiter list.
- *
+ * This method checks whether a model action is enabled at the given point.
+ * At this point, it checks whether a lock operation would be successful at this point.
+ * If not, it puts the thread in a waiter list.
  * @param curr is the ModelAction to check whether it is enabled.
  * @return a bool that indicates whether the action is enabled.
  */
@@ -588,12 +606,6 @@ bool ModelChecker::check_action_enabled(ModelAction *curr) {
                        lock_waiters_map->get_safe_ptr(curr->get_location())->push_back(curr);
                        return false;
                }
-       } else if (curr->get_type() == THREAD_JOIN) {
-               Thread *blocking = (Thread *)curr->get_location();
-               if (!blocking->is_complete()) {
-                       blocking->push_wait_list(curr);
-                       return false;
-               }
        }
 
        return true;
@@ -619,7 +631,7 @@ Thread * ModelChecker::check_current_action(ModelAction *curr)
 
        if (!check_action_enabled(curr)) {
                /* Make the execution look like we chose to run this action
-                * much later, when a lock/join can succeed */
+                * much later, when a lock is actually available to release */
                get_current_thread()->set_pending(curr);
                scheduler->sleep(get_current_thread());
                return get_next_thread(NULL);
@@ -703,6 +715,19 @@ Thread * ModelChecker::check_current_action(ModelAction *curr)
        return get_next_thread(curr);
 }
 
+/**
+ * Complete a THREAD_JOIN operation, by synchronizing with the THREAD_FINISH
+ * operation from the Thread it is joining with. Must be called after the
+ * completion of the Thread in question.
+ * @param join The THREAD_JOIN action
+ */
+void ModelChecker::do_complete_join(ModelAction *join)
+{
+       Thread *blocking = (Thread *)join->get_location();
+       ModelAction *act = get_last_action(blocking->get_id());
+       join->synchronize_with(act);
+}
+
 void ModelChecker::check_curr_backtracking(ModelAction * curr) {
        Node *currnode = curr->get_node();
        Node *parnode = currnode->get_parent();
@@ -916,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;
                                        }
@@ -1041,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;
                        }
 
@@ -1065,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) &&
@@ -1310,6 +1351,8 @@ bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_
                }
 
                if (updated) {
+                       /* Re-check all pending release sequences */
+                       work_queue->push_back(CheckRelSeqWorkEntry(NULL));
                        /* Re-check act for mo_graph edges */
                        work_queue->push_back(MOEdgeWorkEntry(act));
 
@@ -1461,6 +1504,7 @@ bool ModelChecker::resolve_promises(ModelAction *write)
                        //Make sure the promise's value matches the write's value
                        ASSERT(promise->get_value() == write->get_value());
 
+                       delete(promise);
                        promises->erase(promises->begin() + promise_index);
                        resolved = true;
                } else
@@ -1607,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());
@@ -1663,12 +1708,35 @@ 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)
+{
+       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)
+{
+       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
  * model-checking action (described by a ModelAction object). Must be called
  * from a user-thread context.
- * @param act The current action that will be explored. Must not be NULL.
+ *
+ * @param act The current action that will be explored. May be NULL only if
+ * trace is exiting via an assertion (see ModelChecker::set_assert and
+ * ModelChecker::has_asserted).
  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
  */
 int ModelChecker::switch_to_master(ModelAction *act)