model: schedule appropriate fence backtracking points
[c11tester.git] / threads.cc
index 6b4e2c740ab50b7b55f624b7bda05a4c9ad72678..1de548ccdb93ccd41a2a0248a49be2937dd87f01 100644 (file)
@@ -7,6 +7,7 @@
 #include <threads.h>
 #include "common.h"
 #include "threads-model.h"
+#include "action.h"
 
 /* global "model" object */
 #include "model.h"
@@ -23,7 +24,13 @@ static void stack_free(void *stack)
        snapshot_free(stack);
 }
 
-/** Return the currently executing thread. */
+/**
+ * @brief Get the current Thread
+ *
+ * Must be called from a user context
+ *
+ * @return The currently executing thread
+ */
 Thread * thread_current(void)
 {
        ASSERT(model);
@@ -84,6 +91,7 @@ int Thread::create_context()
  */
 int Thread::swap(Thread *t, ucontext_t *ctxt)
 {
+       t->set_state(THREAD_READY);
        return swapcontext(&t->context, ctxt);
 }
 
@@ -97,6 +105,7 @@ int Thread::swap(Thread *t, ucontext_t *ctxt)
  */
 int Thread::swap(ucontext_t *ctxt, Thread *t)
 {
+       t->set_state(THREAD_RUNNING);
        return swapcontext(ctxt, &t->context);
 }
 
@@ -104,12 +113,11 @@ int Thread::swap(ucontext_t *ctxt, Thread *t)
 /** Terminate a thread and free its stack. */
 void Thread::complete()
 {
-       if (!is_complete()) {
-               DEBUG("completed thread %d\n", id_to_int(get_id()));
-               state = THREAD_COMPLETED;
-               if (stack)
-                       stack_free(stack);
-       }
+       ASSERT(!is_complete());
+       DEBUG("completed thread %d\n", id_to_int(get_id()));
+       state = THREAD_COMPLETED;
+       if (stack)
+               stack_free(stack);
 }
 
 /**
@@ -162,14 +170,15 @@ Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
                model_print("Error in create_context\n");
 
        id = model->get_next_id();
-       *user_thread = id;
+       user_thread->priv = this;
        parent = thread_current();
 }
 
 /** Destructor */
 Thread::~Thread()
 {
-       complete();
+       if (!is_complete())
+               complete();
        model->remove_thread(this);
 }
 
@@ -178,3 +187,13 @@ thread_id_t Thread::get_id() const
 {
        return id;
 }
+
+/**
+ * Set a thread's THREAD_* state (@see thread_state)
+ * @param s The state to enter
+ */
+void Thread::set_state(thread_state s)
+{
+       ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
+       state = s;
+}