threads_internal: pass the current 'action' to the internal thread system
[model-checker.git] / libthreads.cc
index 5a4c3a12f101f33252c042f7b5daf6c19d98cca4..fb828b18aa079e2d726d0cfc851600d7ec6d53d0 100644 (file)
@@ -4,6 +4,7 @@
 #include "libthreads.h"
 #include "schedule.h"
 #include "common.h"
+#include "threads_internal.h"
 
 /* global "model" object */
 #include "model.h"
@@ -97,6 +98,18 @@ static void thread_wait_finish(void)
        while (!thread_system_next());
 }
 
+int thread_switch_to_master(ModelAction *act)
+{
+       struct thread *old, *next;
+
+       DBG();
+       model->set_current_action(act);
+       old = thread_current();
+       old->state = THREAD_READY;
+       next = model->system_thread;
+       return thread_swap(old, next);
+}
+
 /*
  * User program API functions
  */
@@ -126,19 +139,16 @@ int thread_create(struct thread *t, void (*start_routine)(), void *arg)
 
 void thread_join(struct thread *t)
 {
-       while (t->state != THREAD_COMPLETED)
-               thread_yield();
+       int ret = 0;
+       while (t->state != THREAD_COMPLETED && !ret)
+               /* seq_cst is just a 'don't care' parameter */
+               ret = thread_switch_to_master(new ModelAction(THREAD_JOIN, memory_order_seq_cst, NULL, VALUE_NONE));
 }
 
 int thread_yield(void)
 {
-       struct thread *old, *next;
-
-       DBG();
-       old = thread_current();
-       old->state = THREAD_READY;
-       next = model->system_thread;
-       return thread_swap(old, next);
+       /* seq_cst is just a 'don't care' parameter */
+       return thread_switch_to_master(new ModelAction(THREAD_YIELD, memory_order_seq_cst, NULL, VALUE_NONE));
 }
 
 struct thread *thread_current(void)
@@ -156,7 +166,7 @@ int main()
 
        model = new ModelChecker();
 
-       main_thread = (struct thread *)malloc(sizeof(*main_thread));
+       main_thread = (struct thread *)myMalloc(sizeof(*main_thread));
        create_initial_thread(main_thread);
        model->add_system_thread(main_thread);
 
@@ -167,6 +177,7 @@ int main()
        thread_wait_finish();
 
        delete model;
+       myFree(main_thread);
 
        DEBUG("Exiting\n");
        return 0;