changes
[model-checker.git] / model.cc
index e3d9203eea60ec53010d67bb87da20331cfbf642..dd72fdbd5e35d84579de1d197c2ce2a0d550b242 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -52,7 +52,7 @@ ModelChecker::ModelChecker(struct model_params params) :
 /** @brief Destructor */
 ModelChecker::~ModelChecker()
 {
-       for (int i = 0; i < get_num_threads(); i++)
+       for (unsigned int i = 0; i < get_num_threads(); i++)
                delete thread_map->get(i);
        delete thread_map;
 
@@ -95,7 +95,7 @@ thread_id_t ModelChecker::get_next_id()
 }
 
 /** @return the number of user threads created during this execution */
-int ModelChecker::get_num_threads()
+unsigned int ModelChecker::get_num_threads()
 {
        return priv->next_thread_id;
 }
@@ -377,6 +377,8 @@ bool ModelChecker::process_read(ModelAction *curr, bool second_part_of_rmw)
 
                        curr->read_from(reads_from);
                        mo_graph->commitChanges();
+                       mo_check_promises(curr->get_tid(), reads_from);
+
                        updated |= r_status;
                } else if (!second_part_of_rmw) {
                        /* Read from future value */
@@ -474,6 +476,8 @@ bool ModelChecker::process_write(ModelAction *curr)
        }
 
        mo_graph->commitChanges();
+       mo_check_promises(curr->get_tid(), curr);
+
        get_thread(curr)->set_return_value(VALUE_NONE);
        return updated_mod_order || updated_promises;
 }
@@ -526,7 +530,7 @@ bool ModelChecker::process_thread_action(ModelAction *curr)
                break;
        }
        case THREAD_START: {
-               check_promises(NULL, curr->get_cv());
+               check_promises(curr->get_tid(), NULL, curr->get_cv());
                break;
        }
        default:
@@ -1303,9 +1307,8 @@ bool ModelChecker::release_seq_heads(const ModelAction *rf, rel_heads_list_t *re
 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_heads(rf, release_heads);
-       if (!complete) {
+
+       if (!release_seq_heads(rf, release_heads)) {
                /* add act to 'lazy checking' list */
                pending_acq_rel_seq->push_back(act);
        }
@@ -1486,6 +1489,7 @@ ClockVector * ModelChecker::get_cv(thread_id_t tid)
 bool ModelChecker::resolve_promises(ModelAction *write)
 {
        bool resolved = false;
+  std::vector<thread_id_t> threads_to_check;
 
        for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
                Promise *promise = (*promises)[promise_index];
@@ -1503,13 +1507,22 @@ bool ModelChecker::resolve_promises(ModelAction *write)
                        post_r_modification_order(read, 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);
+                       threads_to_check.push_back(read->get_tid());
+
                        resolved = true;
                } else
                        promise_index++;
        }
+
+       //Check whether reading these writes has made threads unable to
+       //resolve promises
+
+       for(unsigned int i=0;i<threads_to_check.size();i++)
+               mo_check_promises(threads_to_check[i], write);
+
        return resolved;
 }
 
@@ -1535,16 +1548,14 @@ void ModelChecker::compute_promises(ModelAction *curr)
 }
 
 /** Checks promises in response to change in ClockVector Threads. */
-void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
+void ModelChecker::check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv)
 {
        for (unsigned int i = 0; i < promises->size(); i++) {
                Promise *promise = (*promises)[i];
                const ModelAction *act = promise->get_action();
                if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
                                merge_cv->synchronized_since(act)) {
-                       //This thread is no longer able to send values back to satisfy the promise
-                       int num_synchronized_threads = promise->increment_threads();
-                       if (num_synchronized_threads == get_num_threads()) {
+                       if (promise->increment_threads(tid)) {
                                //Promise has failed
                                failed_promise = true;
                                return;
@@ -1553,6 +1564,69 @@ void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
        }
 }
 
+/** Checks promises in response to addition to modification order for threads.
+ * Definitions:
+ * pthread is the thread that performed the read that created the promise
+ * 
+ * pread is the read that created the promise
+ *
+ * pwrite is either the first write to same location as pread by
+ * pthread that is sequenced after pread or the value read by the
+ * first read to the same lcoation as pread by pthread that is
+ * sequenced after pread..
+ *
+ *     1. If tid=pthread, then we check what other threads are reachable
+ * through the mode order starting with pwrite.  Those threads cannot
+ * perform a write that will resolve the promise due to modification
+ * order constraints.
+ *
+ * 2. If the tid is not pthread, we check whether pwrite can reach the
+ * action write through the modification order.  If so, that thread
+ * cannot perform a future write that will resolve the promise due to
+ * modificatin order constraints.
+ *
+ *     @parem tid The thread that either read from the model action
+ *     write, or actually did the model action write.
+ *
+ *     @parem write The ModelAction representing the relevant write.
+ */
+
+void ModelChecker::mo_check_promises(thread_id_t tid, const ModelAction *write) {
+       void * location = write->get_location();
+       for (unsigned int i = 0; i < promises->size(); i++) {
+               Promise *promise = (*promises)[i];
+               const ModelAction *act = promise->get_action();
+               
+               //Is this promise on the same location?
+               if ( act->get_location() != location )
+                       continue;
+
+               //same thread as the promise
+               if ( act->get_tid()==tid ) {
+
+                       //do we have a pwrite for the promise, if not, set it
+                       if (promise->get_write() == NULL ) {
+                               promise->set_write(write);
+                       }
+                       if (mo_graph->checkPromise(write, promise)) {
+                               failed_promise = true;
+                               return;
+                       }
+               }
+               
+               //Don't do any lookups twice for the same thread
+               if (promise->has_sync_thread(tid))
+                       continue;
+               
+               if (mo_graph->checkReachable(promise->get_write(), write)) {
+                       if (promise->increment_threads(tid)) {
+                               failed_promise = true;
+                               return;
+                       }
+               }
+       }
+}
+
 /**
  * Build up an initial set of all past writes that this 'read' action may read
  * from. This set is determined by the clock vector's "happens before"