fix the case where no waiter is waiting
[c11tester.git] / execution.cc
index 01a5fa442b0905b522f05714d95dc7eaf5fa56a2..8fdc9fbe59be737ed849582ac77b35c124d41665 100644 (file)
@@ -1,6 +1,5 @@
 #include <stdio.h>
 #include <algorithm>
-#include <mutex>
 #include <new>
 #include <stdarg.h>
 
@@ -34,6 +33,7 @@ struct model_snapshot_members {
                too_many_reads(false),
                no_valid_reads(false),
                bad_synchronization(false),
+               bad_sc_read(false),
                asserted(false)
        { }
 
@@ -53,6 +53,7 @@ struct model_snapshot_members {
        bool no_valid_reads;
        /** @brief Incorrectly-ordered synchronization was made */
        bool bad_synchronization;
+       bool bad_sc_read;
        bool asserted;
 
        SNAPSHOTALLOC
@@ -68,9 +69,12 @@ ModelExecution::ModelExecution(ModelChecker *m,
        scheduler(scheduler),
        action_trace(),
        thread_map(2), /* We'll always need at least 2 threads */
+       pthread_map(0),
+       pthread_counter(0),
        obj_map(),
        condvar_waiters_map(),
        obj_thrd_map(),
+       mutex_map(),
        promises(),
        futurevalues(),
        pending_rel_seqs(),
@@ -81,8 +85,8 @@ ModelExecution::ModelExecution(ModelChecker *m,
        mo_graph(new CycleGraph())
 {
        /* Initialize a model-checker thread, for special ModelActions */
-       model_thread = new Thread(get_next_id());
-       add_thread(model_thread);
+       model_thread = new Thread(get_next_id());       // L: Create model thread
+       add_thread(model_thread);                       // L: Add model thread to scheduler
        scheduler->register_engine(this);
        node_stack->register_engine(this);
 }
@@ -202,6 +206,13 @@ void ModelExecution::set_bad_synchronization()
        priv->bad_synchronization = true;
 }
 
+/** @brief Alert the model-checker that an incorrectly-ordered
+ * synchronization was made */
+void ModelExecution::set_bad_sc_read()
+{
+       priv->bad_sc_read = true;
+}
+
 bool ModelExecution::assert_bug(const char *msg)
 {
        priv->bugs.push_back(new bug_message(msg));
@@ -676,8 +687,8 @@ bool ModelExecution::process_read(ModelAction *curr)
  */
 bool ModelExecution::process_mutex(ModelAction *curr)
 {
-       std::mutex *mutex = curr->get_mutex();
-       struct std::mutex_state *state = NULL;
+       cdsc::mutex *mutex = curr->get_mutex();
+       struct cdsc::mutex_state *state = NULL;
 
        if (mutex)
                state = mutex->get_state();
@@ -742,6 +753,11 @@ bool ModelExecution::process_mutex(ModelAction *curr)
                action_list_t *waiters = get_safe_ptr_action(&condvar_waiters_map, curr->get_location());
                int wakeupthread = curr->get_node()->get_misc();
                action_list_t::iterator it = waiters->begin();
+
+               // WL
+               if (it == waiters->end())
+                       break;
+
                advance(it, wakeupthread);
                scheduler->wake(get_thread(*it));
                waiters->erase(it);
@@ -938,14 +954,37 @@ bool ModelExecution::process_thread_action(ModelAction *curr)
                thrd_t *thrd = (thrd_t *)curr->get_location();
                struct thread_params *params = (struct thread_params *)curr->get_value();
                Thread *th = new Thread(get_next_id(), thrd, params->func, params->arg, get_thread(curr));
+               curr->set_thread_operand(th);
+               add_thread(th);
+               th->set_creation(curr);
+               /* Promises can be satisfied by children */
+               for (unsigned int i = 0; i < promises.size(); i++) {
+                       Promise *promise = promises[i];
+                       if (promise->thread_is_available(curr->get_tid()))
+                               promise->add_thread(th->get_id());
+               }
+               break;
+       }
+       case PTHREAD_CREATE: {
+               (*(pthread_t *)curr->get_location()) = pthread_counter++;       
+
+               struct pthread_params *params = (struct pthread_params *)curr->get_value();
+               Thread *th = new Thread(get_next_id(), NULL, params->func, params->arg, get_thread(curr));
+               curr->set_thread_operand(th);
                add_thread(th);
                th->set_creation(curr);
+
+               if ( pthread_map.size() < pthread_counter )
+                       pthread_map.resize( pthread_counter );
+               pthread_map[ pthread_counter-1 ] = th;
+
                /* Promises can be satisfied by children */
                for (unsigned int i = 0; i < promises.size(); i++) {
                        Promise *promise = promises[i];
                        if (promise->thread_is_available(curr->get_tid()))
                                promise->add_thread(th->get_id());
                }
+               
                break;
        }
        case THREAD_JOIN: {
@@ -955,6 +994,14 @@ bool ModelExecution::process_thread_action(ModelAction *curr)
                updated = true; /* trigger rel-seq checks */
                break;
        }
+       case PTHREAD_JOIN: {
+               Thread *blocking = curr->get_thread_operand();
+               ModelAction *act = get_last_action(blocking->get_id());
+               synchronize(act, curr);
+               updated = true; /* trigger rel-seq checks */
+               break; // WL: to be add (modified)
+       }
+
        case THREAD_FINISH: {
                Thread *th = get_thread(curr);
                /* Wake up any joining threads */
@@ -1123,6 +1170,7 @@ bool ModelExecution::initialize_curr_action(ModelAction **curr)
  *
  * @return True if this read established synchronization
  */
+
 bool ModelExecution::read_from(ModelAction *act, const ModelAction *rf)
 {
        ASSERT(rf);
@@ -1205,8 +1253,8 @@ void ModelExecution::thread_blocking_check_promises(Thread *blocker, Thread *wai
  */
 bool ModelExecution::check_action_enabled(ModelAction *curr) {
        if (curr->is_lock()) {
-               std::mutex *lock = curr->get_mutex();
-               struct std::mutex_state *state = lock->get_state();
+               cdsc::mutex *lock = curr->get_mutex();
+               struct cdsc::mutex_state *state = lock->get_state();
                if (state->locked)
                        return false;
        } else if (curr->is_thread_join()) {
@@ -1306,6 +1354,12 @@ ModelAction * ModelExecution::check_current_action(ModelAction *curr)
                                if (rf) {
                                        if (r_modification_order(act, rf))
                                                updated = true;
+                                       if (act->is_seqcst()) {
+                                               ModelAction *last_sc_write = get_last_seq_cst_write(act);
+                                               if (last_sc_write != NULL && rf->happens_before(last_sc_write)) {
+                                                       set_bad_sc_read();
+                                               }
+                                       }
                                } else if (promise) {
                                        if (r_modification_order(act, promise))
                                                updated = true;
@@ -1385,6 +1439,8 @@ void ModelExecution::print_infeasibility(const char *prefix) const
                ptr += sprintf(ptr, "[no valid reads-from]");
        if (priv->bad_synchronization)
                ptr += sprintf(ptr, "[bad sw ordering]");
+       if (priv->bad_sc_read)
+               ptr += sprintf(ptr, "[bad sc read]");
        if (promises_expired())
                ptr += sprintf(ptr, "[promise expired]");
        if (promises.size() != 0)
@@ -1415,6 +1471,7 @@ bool ModelExecution::is_infeasible() const
                priv->no_valid_reads ||
                priv->too_many_reads ||
                priv->bad_synchronization ||
+               priv->bad_sc_read ||
                priv->hard_failed_promise ||
                promises_expired();
 }
@@ -1616,12 +1673,6 @@ bool ModelExecution::r_modification_order(ModelAction *curr, const rf_type *rf)
                                }
                        }
 
-                       /* C++, Section 29.3 statement 3 (second subpoint) */
-                       if (curr->is_seqcst() && last_sc_write && act == last_sc_write) {
-                               added = mo_graph->addEdge(act, rf) || added;
-                               break;
-                       }
-
                        /*
                         * Include at most one act per-thread that "happens
                         * before" curr
@@ -2335,6 +2386,7 @@ ModelAction * ModelExecution::get_last_seq_cst_fence(thread_id_t tid, const Mode
 ModelAction * ModelExecution::get_last_unlock(ModelAction *curr) const
 {
        void *location = curr->get_location();
+
        action_list_t *list = obj_map.get(location);
        /* Find: max({i in dom(S) | isUnlock(t_i) && samevar(t_i, t)}) */
        action_list_t::reverse_iterator rit;
@@ -2809,6 +2861,16 @@ Thread * ModelExecution::get_thread(const ModelAction *act) const
        return get_thread(act->get_tid());
 }
 
+/**
+ * @brief Get a Thread reference by its pthread ID
+ * @param index The pthread's ID
+ * @return A Thread reference
+ */
+Thread * ModelExecution::get_pthread(pthread_t pid) {
+        if (pid < pthread_counter + 1) return pthread_map[pid];
+        else return NULL;
+}
+
 /**
  * @brief Get a Promise's "promise number"
  *
@@ -2865,9 +2927,26 @@ Thread * ModelExecution::action_select_next_thread(const ModelAction *curr) cons
        /* Do not split atomic RMW */
        if (curr->is_rmwr())
                return get_thread(curr);
+       if (curr->is_write()) {
+//             std::memory_order order = curr->get_mo(); 
+//             switch(order) {
+//                     case std::memory_order_relaxed: 
+//                             return get_thread(curr);
+//                     case std::memory_order_release:
+//                             return get_thread(curr);
+//                     defalut:
+//                             return NULL;
+//             }       
+               return NULL;
+       }
+
        /* Follow CREATE with the created thread */
+       /* which is not needed, because model.cc takes care of this */
        if (curr->get_type() == THREAD_CREATE)
+               return curr->get_thread_operand(); 
+       if (curr->get_type() == PTHREAD_CREATE) {
                return curr->get_thread_operand();
+       }
        return NULL;
 }