static void * wakeupthread(void *arg) { printf("thread %p\n", arg); pthread_mutex_lock(&mtx); predicate = 1; printf("rise and shine %p!\n", arg); pthread_cond_signal(&cv); pthread_mutex_unlock(&mtx); threxit(arg); return NULL; }
static void * waitthread(void *arg) { printf("thread %p\n", arg); pthread_mutex_lock(&mtx); while (!predicate) { printf("no good, need to wait %p\n", arg); pthread_cond_wait(&cv, &mtx); } pthread_mutex_unlock(&mtx); printf("condvar complete %p!\n", arg); threxit(arg); return NULL; }
static void * mythread(void *arg) { printf("thread %p\n", arg); pthread_mutex_lock(&mtx); printf("got lock %p\n", arg); sched_yield(); pthread_mutex_unlock(&mtx); printf("unlocked lock %p\n", arg); sched_yield(); threxit(arg); return NULL; }
void pthread_exit(void *retval) { struct rthread_cleanup_fn *clfn; pid_t tid; struct stack *stack; pthread_t thread = pthread_self(); thread->retval = retval; for (clfn = thread->cleanup_fns; clfn; ) { struct rthread_cleanup_fn *oclfn = clfn; clfn = clfn->next; oclfn->fn(oclfn->arg); free(oclfn); } _rthread_tls_destructors(thread); _spinlock(&_thread_lock); LIST_REMOVE(thread, threads); _spinunlock(&_thread_lock); _sem_post(&thread->donesem); stack = thread->stack; tid = thread->tid; if (thread->flags & THREAD_DETACHED) _rthread_free(thread); else _rthread_setflag(thread, THREAD_DONE); if (tid != _initial_thread.tid) _rthread_add_to_reaper(tid, stack); _rthread_reaper(); threxit(0); for(;;); }