model/schedule: revise 'nextThread' data flow
[model-checker.git] / model.cc
index fc93296a2524f4d2edc741e36f54dff24eb881e2..c306f0e70869de9362ea83af601f6d94bda30ef9 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -25,12 +25,13 @@ ModelChecker::ModelChecker(struct model_params params) :
        params(params),
        current_action(NULL),
        diverge(NULL),
-       nextThread(THREAD_ID_T_NONE),
+       nextThread(NULL),
        action_trace(new action_list_t()),
        thread_map(new HashTable<int, Thread *, int>()),
        obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
        obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
        promises(new std::vector<Promise *>()),
+       lazy_sync_with_release(new HashTable<void *, std::list<ModelAction *>, uintptr_t, 4>()),
        thrd_last_action(new std::vector<ModelAction *>(1)),
        node_stack(new NodeStack()),
        next_backtrack(NULL),
@@ -54,6 +55,8 @@ ModelChecker::~ModelChecker()
                delete (*promises)[i];
        delete promises;
 
+       delete lazy_sync_with_release;
+
        delete thrd_last_action;
        delete node_stack;
        delete scheduler;
@@ -71,7 +74,7 @@ void ModelChecker::reset_to_initial_state()
        current_action = NULL;
        next_thread_id = INITIAL_THREAD_ID;
        used_sequence_numbers = 0;
-       nextThread = 0;
+       nextThread = NULL;
        next_backtrack = NULL;
        failed_promise = false;
        snapshotObject->backTrackBeforeStep(0);
@@ -95,27 +98,6 @@ modelclock_t ModelChecker::get_next_seq_num()
        return ++used_sequence_numbers;
 }
 
-/**
- * Performs the "scheduling" for the model-checker. That is, it checks if the
- * model-checker has selected a "next thread to run" and returns it, if
- * available. This function should be called from the Scheduler routine, where
- * the Scheduler falls back to a default scheduling routine if needed.
- *
- * @return The next thread chosen by the model-checker. If the model-checker
- * makes no selection, retuns NULL.
- */
-Thread * ModelChecker::schedule_next_thread()
-{
-       Thread *t;
-       if (nextThread == THREAD_ID_T_NONE)
-               return NULL;
-       t = thread_map->get(id_to_int(nextThread));
-
-       ASSERT(t != NULL);
-
-       return t;
-}
-
 /**
  * Choose the next thread in the replay sequence.
  *
@@ -123,13 +105,13 @@ Thread * ModelChecker::schedule_next_thread()
  * from the backtracking set. Otherwise, simply returns the next thread in the
  * sequence that is being replayed.
  */
-thread_id_t ModelChecker::get_next_replay_thread()
+Thread * ModelChecker::get_next_replay_thread()
 {
        thread_id_t tid;
 
        /* Have we completed exploring the preselected path? */
        if (diverge == NULL)
-               return THREAD_ID_T_NONE;
+               return NULL;
 
        /* Else, we are trying to replay an execution */
        ModelAction *next = node_stack->get_next()->get_action();
@@ -161,7 +143,8 @@ thread_id_t ModelChecker::get_next_replay_thread()
                tid = next->get_tid();
        }
        DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
-       return tid;
+       ASSERT(tid != THREAD_ID_T_NONE);
+       return thread_map->get(id_to_int(tid));
 }
 
 /**
@@ -317,13 +300,15 @@ void ModelChecker::check_current_action(void)
        /* Assign reads_from values */
        Thread *th = get_thread(curr->get_tid());
        uint64_t value = VALUE_NONE;
+       bool updated = false;
        if (curr->is_read()) {
                const ModelAction *reads_from = curr->get_node()->get_read_from();
                if (reads_from != NULL) {
                        value = reads_from->get_value();
                        /* Assign reads_from, perform release/acquire synchronization */
                        curr->read_from(reads_from);
-                       r_modification_order(curr,reads_from);
+                       if (r_modification_order(curr,reads_from))
+                               updated = true;
                } else {
                        /* Read from future value */
                        value = curr->get_node()->get_future_value();
@@ -332,10 +317,15 @@ void ModelChecker::check_current_action(void)
                        promises->push_back(valuepromise);
                }
        } else if (curr->is_write()) {
-               w_modification_order(curr);
-               resolve_promises(curr);
+               if (w_modification_order(curr))
+                       updated = true;;
+               if (resolve_promises(curr))
+                       updated = true;
        }
 
+       if (updated)
+               resolve_release_sequences(curr->get_location());
+
        th->set_return_value(value);
 
        /* Add action to list.  */
@@ -348,7 +338,7 @@ void ModelChecker::check_current_action(void)
 
        /* Do not split atomic actions. */
        if (curr->is_rmwr())
-               nextThread = thread_current()->get_id();
+               nextThread = thread_current();
        else
                nextThread = get_next_replay_thread();
 
@@ -637,10 +627,65 @@ void ModelChecker::get_release_seq_heads(ModelAction *act,
        bool complete;
        complete = release_seq_head(rf, release_heads);
        if (!complete) {
-               /** @todo complete lazy checking */
+               /* add act to 'lazy checking' list */
+               std::list<ModelAction *> *list;
+               list = lazy_sync_with_release->get_safe_ptr(act->get_location());
+               list->push_back(act);
        }
 }
 
+/**
+ * Attempt to resolve all stashed operations that might synchronize with a
+ * release sequence for a given location. This implements the "lazy" portion of
+ * determining whether or not a release sequence was contiguous, since not all
+ * modification order information is present at the time an action occurs.
+ *
+ * @param location The location/object that should be checked for release
+ * sequence resolutions
+ * @return True if any updates occurred (new synchronization, new mo_graph edges)
+ */
+bool ModelChecker::resolve_release_sequences(void *location)
+{
+       std::list<ModelAction *> *list;
+       list = lazy_sync_with_release->getptr(location);
+       if (!list)
+               return false;
+
+       bool updated = false;
+       std::list<ModelAction *>::iterator it = list->begin();
+       while (it != list->end()) {
+               ModelAction *act = *it;
+               const ModelAction *rf = act->get_reads_from();
+               std::vector<const ModelAction *> release_heads;
+               bool complete;
+               complete = release_seq_head(rf, &release_heads);
+               for (unsigned int i = 0; i < release_heads.size(); i++) {
+                       if (!act->has_synchronized_with(release_heads[i])) {
+                               updated = true;
+                               act->synchronize_with(release_heads[i]);
+                       }
+               }
+
+               if (updated) {
+                       /* propagate synchronization to later actions */
+                       action_list_t::reverse_iterator it = action_trace->rbegin();
+                       while ((*it) != act) {
+                               ModelAction *propagate = *it;
+                               if (act->happens_before(propagate))
+                                       /** @todo new mo_graph edges along with
+                                        * this synchronization? */
+                                       propagate->synchronize_with(act);
+                       }
+               }
+               if (complete)
+                       it = list->erase(it);
+               else
+                       it++;
+       }
+
+       return updated;
+}
+
 /**
  * Performs various bookkeeping operations for the current ModelAction. For
  * instance, adds action to the per-object, per-thread action vector and to the
@@ -926,7 +971,7 @@ bool ModelChecker::take_step() {
                        ASSERT(false);
                }
        }
-       next = scheduler->next_thread();
+       next = scheduler->next_thread(nextThread);
 
        /* Infeasible -> don't take any more steps */
        if (!isfeasible())