#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);
+ return userMalloc(size);
}
static void stack_free(void *stack)
{
- free(stack);
+ userFree(stack);
}
-Thread *thread_current(void)
+Thread * thread_current(void)
{
return model->scheduler->get_current_thread();
}
context.uc_stack.ss_sp = stack;
context.uc_stack.ss_size = STACK_SIZE;
context.uc_stack.ss_flags = 0;
- context.uc_link = &model->system_thread->context;
+ context.uc_link = model->get_system_context();
makecontext(&context, start_routine, 1, arg);
return 0;
}
-int Thread::swap(Thread *t)
+int Thread::swap(Thread *t, ucontext_t *ctxt)
{
- return swapcontext(&this->context, &t->context);
+ return swapcontext(&t->context, ctxt);
}
-void Thread::dispose()
+int Thread::swap(ucontext_t *ctxt, Thread *t)
{
- DEBUG("completed thread %d\n", thread_current()->get_id());
- state = THREAD_COMPLETED;
- stack_free(stack);
+ return swapcontext(ctxt, &t->context);
}
-int Thread::switch_to_master(ModelAction *act)
+void Thread::complete()
{
- Thread *next;
+ if (state != THREAD_COMPLETED) {
+ DEBUG("completed thread %d\n", get_id());
+ state = THREAD_COMPLETED;
+ if (stack)
+ stack_free(stack);
+ }
+}
- DBG();
- model->set_current_action(act);
- state = THREAD_READY;
- next = model->system_thread;
- return swap(next);
+void * Thread::operator new(size_t size) {
+ return userMalloc(size);
+}
+
+void Thread::operator delete(void *ptr) {
+ userFree(ptr);
}
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;
- state = THREAD_CREATED;
- model->assign_id(this);
+ start_routine = NULL;
+ arg = NULL;
+
create_context();
- model->add_system_thread(this);
+ stack = NULL;
+ state = THREAD_CREATED;
+ id = model->get_next_id();
+ *user_thread = id;
+ model->set_system_context(&context);
+}
+
+Thread::~Thread()
+{
+ complete();
+ model->remove_thread(this);
}
thread_id_t Thread::get_id()
{
- return thrd_to_id(*user_thread);
+ return id;
}
/*
Thread *curr, *next;
curr = thread_current();
- model->check_current_action();
if (curr) {
- if (curr->get_state() == THREAD_READY)
+ if (curr->get_state() == THREAD_READY) {
+ model->check_current_action();
model->scheduler->add_thread(curr);
- else if (curr->get_state() == THREAD_RUNNING)
+ } 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");
}
DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
if (!next)
return 1;
- return model->system_thread->swap(next);
+ return Thread::swap(model->get_system_context(), next);
}
static void thread_wait_finish(void)
*/
int main()
{
- thrd_t user_thread, main_thread;
- Thread *th;
+ thrd_t user_thread;
+ ucontext_t main_context;
model = new ModelChecker();
- th = new Thread(&main_thread);
+ if (getcontext(&main_context))
+ return 1;
+
+ model->set_system_context(&main_context);
- /* 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();
+ } while (model->next_execution());
- model->print_trace();
- delete th;
delete model;
DEBUG("Exiting\n");