Do not allow a thread to stash the next pending action if its last action was a SLEEP...
authorweiyu <weiyuluo1232@gmail.com>
Fri, 1 Nov 2019 20:58:16 +0000 (13:58 -0700)
committerweiyu <weiyuluo1232@gmail.com>
Fri, 1 Nov 2019 20:58:16 +0000 (13:58 -0700)
execution.cc
model.cc
schedule.cc
threads-model.h

index e6203cb..e7ca575 100644 (file)
@@ -161,6 +161,7 @@ bool ModelExecution::should_wake_up(const ModelAction *curr, const Thread *threa
                if (fence_release && *(get_last_action(thread->get_id())) < *fence_release)
                        return true;
        }
+       /* The sleep is literally sleeping */
        if (asleep->is_sleep()) {
                if (fuzzer->shouldWake(asleep))
                        return true;
@@ -178,7 +179,7 @@ void ModelExecution::wake_up_sleeping_actions(ModelAction *curr)
                                /* Remove this thread from sleep set */
                                scheduler->remove_sleep(thr);
                                if (thr->get_pending()->is_sleep())
-                                       thr->set_pending(NULL);
+                                       thr->set_wakeup_state(true);
                        }
                }
        }
@@ -827,7 +828,8 @@ ModelAction * ModelExecution::process_rmw(ModelAction *act) {
  * @return True if modification order edges were added; false otherwise
  */
 
-bool ModelExecution::r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<const ModelAction *> * priorset, bool * canprune, bool check_only)
+bool ModelExecution::r_modification_order(ModelAction *curr, const ModelAction *rf,
+       SnapVector<const ModelAction *> * priorset, bool * canprune, bool check_only)
 {
        SnapVector<action_list_t> *thrd_lists = obj_thrd_map.get(curr->get_location());
        unsigned int i;
index 4c4b47a..1e3dd2d 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -413,7 +413,7 @@ void ModelChecker::run()
                        for (unsigned int i = 0;i < get_num_threads();i++) {
                                thread_id_t tid = int_to_id(i);
                                Thread *thr = get_thread(tid);
-                               if (!thr->is_model_thread() && !thr->is_complete() && (!thr->get_pending())) {
+                               if (!thr->is_model_thread() && !thr->is_complete() && !thr->get_pending()) {
                                        switch_from_master(thr);
                                        if (thr->is_waiting_on(thr))
                                                assert_bug("Deadlock detected (thread %u)", i);
@@ -459,6 +459,11 @@ void ModelChecker::run()
                                t = get_next_thread();
                        if (!t || t->is_model_thread())
                                break;
+                       if (t->just_woken_up()) {
+                               t->set_wakeup_state(false);
+                               t->set_pending(NULL);
+                               continue;       // Allow this thread to stash the next pending action
+                       }
 
                        /* Consume the next action for a Thread */
                        ModelAction *curr = t->get_pending();
index 4a9752a..f68786a 100644 (file)
@@ -108,7 +108,7 @@ bool Scheduler::is_sleep_set(const Thread *t) const
 /**
  * @brief Check if execution is stuck with no enabled threads and some sleeping
  * thread
- * @return True if no threads are enabled an some thread is in the sleep set;
+ * @return True if no threads are enabled and some thread is in the sleep set;
  * false otherwise
  */
 bool Scheduler::all_threads_sleeping() const
@@ -202,16 +202,31 @@ void Scheduler::wake(Thread *t)
 Thread * Scheduler::select_next_thread()
 {
        int avail_threads = 0;
-       int thread_list[enabled_len];
-       for (int i = 0;i< enabled_len;i++) {
+       int sleep_threads = 0;
+       int thread_list[enabled_len], sleep_list[enabled_len];
+       Thread * thread;
+
+       for (int i = 0; i < enabled_len; i++) {
                if (enabled[i] == THREAD_ENABLED)
                        thread_list[avail_threads++] = i;
+               else if (enabled[i] == THREAD_SLEEP_SET)
+                       sleep_list[sleep_threads++] = i;
        }
 
-       if (avail_threads == 0 && !execution->getFuzzer()->has_paused_threads())
-               return NULL;    // No threads available
+       if (avail_threads == 0 && !execution->getFuzzer()->has_paused_threads()) {
+               if (sleep_threads != 0) {
+                       // No threads available, but some threads sleeping. Wake up one of them
+                       thread = execution->getFuzzer()->selectThread(sleep_list, sleep_threads);
+                       remove_sleep(thread);
+                       thread->set_wakeup_state(true);
+               } else {
+                       return NULL;    // No threads available and no threads sleeping.
+               }
+       } else {
+               // Some threads are available
+               thread = execution->getFuzzer()->selectThread(thread_list, avail_threads);
+       }
 
-       Thread * thread = execution->getFuzzer()->selectThread(thread_list, avail_threads);
        curr_thread_index = id_to_int(thread->get_id());
        return thread;
 }
index e2084ab..f2162a8 100644 (file)
@@ -93,6 +93,9 @@ public:
         *  @see Thread::pending */
        void set_pending(ModelAction *act) { pending = act; }
 
+       bool just_woken_up() { return wakeup_state; }
+       void set_wakeup_state(bool state) { wakeup_state = state; }
+
        Thread * waiting_on() const;
        bool is_waiting_on(const Thread *t) const;
 
@@ -144,6 +147,9 @@ private:
         */
        ModelAction *pending;
 
+       /** @brief True if this thread was just woken up */
+       bool wakeup_state;
+
        void (*start_routine)(void *);
        void *(*pstart_routine)(void *);