// create pthread, add it to thread table int mypthread_create(mypthread_t *thread, void *(*start_routine) (void *), void *arg) { int i, first = 0; if (!tableup) { ttable_init(); start_timer(); first = 1; } // set up context getcontext(&thread->ctx); thread->ctx.uc_stack.ss_sp = calloc(STACK_SZ, 1); thread->ctx.uc_stack.ss_size = STACK_SZ; thread->ctx.uc_link = NULL; makecontext(&thread->ctx, (start_t)start, 1, thread); thread->arg = arg; thread->fn = start_routine; thread->state = RUNNABLE; thread->join_id = 0; thread->retval = NULL; for (i = 1; i < MAX_THREADS; i++) { if (ttable[i] == NULL || ttable[i]->state == UNUSED) { ttable[i] = thread; thread->id = i; if (first) { // set up main() thread ttable[0]->state = RUNNABLE; ttable[0]->id = 0; ttable[0]->retval = NULL; ttable[0]->join_id = 0; getcontext(&ttable[0]->ctx); } return 0; } } return -1; }
void vpnapi_output_init() { ttable_init(&ttable); }