X-Git-Url: http://plrg.eecs.uci.edu/git/?p=c11tester.git;a=blobdiff_plain;f=libthreads.cc;h=9e34141bc1de608abdb89393a3fe63570ff25f7d;hp=c0439ffa8bc60b6754bbe744b335775a5dccebc0;hb=e60d8c23d30a0dfe66b8426f7f2ecf576e812028;hpb=edf8e50ce806cf2d16fa78143bc3e6861aae0772 diff --git a/libthreads.cc b/libthreads.cc index c0439ffa..9e34141b 100644 --- a/libthreads.cc +++ b/libthreads.cc @@ -1,174 +1,39 @@ -#include -#include - #include "libthreads.h" -#include "schedule.h" #include "common.h" +#include "threads-model.h" /* global "model" object */ #include "model.h" -#define STACK_SIZE (1024 * 1024) - -static void *stack_allocate(size_t size) -{ - return malloc(size); -} - -static void stack_free(void *stack) -{ - free(stack); -} - -static int create_context(struct thread *t) -{ - int ret; - - memset(&t->context, 0, sizeof(t->context)); - ret = getcontext(&t->context); - if (ret) - return ret; - - /* t->start_routine == NULL means this is our initial context */ - if (!t->start_routine) - return 0; - - /* Initialize new managed context */ - t->stack = stack_allocate(STACK_SIZE); - t->context.uc_stack.ss_sp = t->stack; - t->context.uc_stack.ss_size = STACK_SIZE; - t->context.uc_stack.ss_flags = 0; - t->context.uc_link = &model->system_thread->context; - makecontext(&t->context, t->start_routine, 1, t->arg); - - return 0; -} - -static int create_initial_thread(struct thread *t) -{ - memset(t, 0, sizeof(*t)); - model->assign_id(t); - return create_context(t); -} - -static int thread_swap(struct thread *t1, struct thread *t2) -{ - return swapcontext(&t1->context, &t2->context); -} - -static void thread_dispose(struct thread *t) -{ - DEBUG("completed thread %d\n", thread_current()->id); - t->state = THREAD_COMPLETED; - stack_free(t->stack); -} - -/* - * Return 1 if found next thread, 0 otherwise - */ -static int thread_system_next(void) -{ - struct thread *curr, *next; - - curr = thread_current(); - if (curr) { - if (curr->state == THREAD_READY) - model->scheduler->add_thread(curr); - else if (curr->state == THREAD_RUNNING) - /* Stopped while running; i.e., completed */ - thread_dispose(curr); - else - DEBUG("ERROR: current thread in unexpected state??\n"); - } - next = model->scheduler->next_thread(); - if (next) - next->state = THREAD_RUNNING; - DEBUG("(%d, %d)\n", curr ? curr->id : -1, next ? next->id : -1); - if (!next) - return 1; - return thread_swap(model->system_thread, next); -} - -static void thread_wait_finish(void) -{ - - DBG(); - - while (!thread_system_next()); -} - /* * User program API functions */ -int thread_create(struct thread *t, void (*start_routine)(), void *arg) +int thrd_create(thrd_t *t, thrd_start_t start_routine, void *arg) { - int ret = 0; - + Thread *thread; DBG(); - - memset(t, 0, sizeof(*t)); - model->assign_id(t); - DEBUG("create thread %d\n", t->id); - - t->start_routine = start_routine; - t->arg = arg; - - /* Initialize state */ - ret = create_context(t); - if (ret) - return ret; - - t->state = THREAD_CREATED; - - model->scheduler->add_thread(t); + thread = new Thread(t, start_routine, arg); + model->add_thread(thread); + DEBUG("create thread %d\n", id_to_int(thrd_to_id(*t))); + /* seq_cst is just a 'don't care' parameter */ + model->switch_to_master(new ModelAction(THREAD_CREATE, std::memory_order_seq_cst, thread, VALUE_NONE)); return 0; } -void thread_join(struct thread *t) +int thrd_join(thrd_t t) { - while (t->state != THREAD_COMPLETED) - thread_yield(); -} - -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); + Thread *th = model->get_thread(thrd_to_id(t)); + model->switch_to_master(new ModelAction(THREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(thrd_to_id(t)))); + return 0; } -struct thread *thread_current(void) +int thrd_yield(void) { - return model->scheduler->get_current_thread(); + /* seq_cst is just a 'don't care' parameter */ + return model->switch_to_master(new ModelAction(THREAD_YIELD, std::memory_order_seq_cst, NULL, VALUE_NONE)); } -/* - * Main system function - */ -int main() +thrd_t thrd_current(void) { - struct thread user_thread; - struct thread *main_thread; - - model = new ModelChecker(); - - main_thread = (struct thread *)myMalloc(sizeof(*main_thread)); - create_initial_thread(main_thread); - model->add_system_thread(main_thread); - - /* Start user program */ - thread_create(&user_thread, &user_main, NULL); - - /* Wait for all threads to complete */ - thread_wait_finish(); - - delete model; - myFree(main_thread); - - DEBUG("Exiting\n"); - return 0; + return thread_current()->get_thrd_t(); }