threads: move circular wait check into Threads::is_waiting_on
[model-checker.git] / threads.cc
index 762bbff34c2463a3dbaf6179e3af3e1eb977ca8b..e4b46561a0e7be13016661a541f09146b9726f28 100644 (file)
@@ -5,6 +5,7 @@
 #include <string.h>
 
 #include <threads.h>
+#include <mutex>
 #include "common.h"
 #include "threads-model.h"
 #include "action.h"
@@ -179,7 +180,6 @@ Thread::~Thread()
 {
        if (!is_complete())
                complete();
-       model->remove_thread(this);
 }
 
 /** @return The thread_id_t corresponding to this Thread object. */
@@ -197,3 +197,35 @@ void Thread::set_state(thread_state s)
        ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
        state = s;
 }
+
+/**
+ * Get the Thread that this Thread is immediately waiting on
+ * @return The thread we are waiting on, if any; otherwise NULL
+ */
+Thread * Thread::waiting_on() const
+{
+       if (!pending)
+               return NULL;
+
+       if (pending->get_type() == THREAD_JOIN)
+               return pending->get_thread_operand();
+       else if (pending->is_lock())
+               return (Thread *)pending->get_mutex()->get_state()->locked;
+       return NULL;
+}
+
+/**
+ * Check if this Thread is waiting (blocking) on a given Thread, directly or
+ * indirectly (via a chain of waiting threads)
+ *
+ * @param t The Thread on which we may be waiting
+ * @return True if we are waiting on Thread t; false otherwise
+ */
+bool Thread::is_waiting_on(const Thread *t) const
+{
+       Thread *wait;
+       for (wait = waiting_on(); wait != NULL; wait = wait->waiting_on())
+               if (wait == t)
+                       return true;
+       return false;
+}