#include "libthreads.h"
#include "schedule.h"
#include "common.h"
-#include "threads_internal.h"
+#include "threads.h"
/* global "model" object */
#include "model.h"
#define STACK_SIZE (1024 * 1024)
-static void *stack_allocate(size_t size)
+static void * stack_allocate(size_t size)
{
return malloc(size);
}
free(stack);
}
-Thread *thread_current(void)
+Thread * thread_current(void)
{
return model->scheduler->get_current_thread();
}
return swapcontext(&this->context, &t->context);
}
-void Thread::dispose()
+void Thread::complete()
{
- DEBUG("completed thread %d\n", thread_current()->get_id());
- state = THREAD_COMPLETED;
- stack_free(stack);
-}
-
-int Thread::switch_to_master(ModelAction *act)
-{
- Thread *next;
-
- DBG();
- model->set_current_action(act);
- state = THREAD_READY;
- next = model->system_thread;
- return swap(next);
+ if (state != THREAD_COMPLETED) {
+ DEBUG("completed thread %d\n", get_id());
+ state = THREAD_COMPLETED;
+ stack_free(stack);
+ }
}
Thread::Thread(thrd_t *t, void (*func)(), void *a) {
printf("Error in create_context\n");
state = THREAD_CREATED;
- model->assign_id(this);
- model->scheduler->add_thread(this);
+ id = model->get_next_id();
+ *user_thread = id;
}
Thread::Thread(thrd_t *t) {
/* system thread */
user_thread = t;
+ start_routine = NULL;
+ arg = NULL;
+
state = THREAD_CREATED;
- model->assign_id(this);
+ id = model->get_next_id();
+ *user_thread = id;
create_context();
model->add_system_thread(this);
}
+Thread::~Thread()
+{
+ complete();
+ model->remove_thread(this);
+}
+
thread_id_t Thread::get_id()
{
- return thrd_to_id(*user_thread);
+ return id;
}
/*
model->scheduler->add_thread(curr);
else if (curr->get_state() == THREAD_RUNNING)
/* Stopped while running; i.e., completed */
- curr->dispose();
+ curr->complete();
else
DEBUG("ERROR: current thread in unexpected state??\n");
}
th = new Thread(&main_thread);
- /* Start user program */
- thrd_create(&user_thread, &user_main, NULL);
+ do {
+ /* Start user program */
+ thrd_create(&user_thread, &user_main, NULL);
+
+ /* Wait for all threads to complete */
+ thread_wait_finish();
- /* Wait for all threads to complete */
- thread_wait_finish();
+ model->print_trace();
+ } while (model->next_execution());
- model->print_trace();
delete th;
delete model;