ref: 265d0e6862eca0ac326208f67cfc7ef3682500b3
dir: /src/plan9_thread.c/
#include <u.h> #include <libc.h> #include </sys/include/thread.h> #include "thread.h" void dav1d_set_thread_name(const char *const name) { threadsetname(name); } static void thre(void *x) { pthread_t *t; void *p; t = x; p = t->func(t->arg); send(t->waitchan, p); threadexits(nil); } int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*func)(void*), void *arg) { uint stack; stack = attr->stack_size > 65536 ? attr->stack_size : 65536; thread->waitchan = chancreate(sizeof(void*), 0); thread->func = func; thread->arg = arg; thread->pid = proccreate(thre, thread, stack); return 0; } int pthread_join(pthread_t *thread, void **res) { // FIXME this is wrong ofc fprint(2, "joining...\n"); if (thread->waitchan != nil) { recv(thread->waitchan, res); chanfree(thread->waitchan); thread->waitchan = nil; } fprint(2, "joined\n"); return 0; } int pthread_once(pthread_once_t *once, void (*init_routine)(void)) { qlock(once); if (once->done != 12345) { init_routine(); once->done = 12345; } qunlock(once); return 0; } int pthread_mutex_init(pthread_mutex_t *const mutex, const void *const attr) { USED(attr); memset(mutex, 0, sizeof(*mutex)); return 0; } int pthread_mutex_destroy(pthread_mutex_t *const mutex) { USED(mutex); return 0; } int pthread_mutex_lock(pthread_mutex_t *const mutex) { qlock(mutex); return 0; } int pthread_mutex_unlock(pthread_mutex_t *const mutex) { qunlock(mutex); return 0; } int pthread_cond_init(pthread_cond_t *const cond, const void *const attr) { memset(cond, 0, sizeof(*cond)); cond->l = &cond->lock; return 0; } int pthread_cond_destroy(pthread_cond_t *const cond) { return 0; } int pthread_cond_wait(pthread_cond_t *const cond, pthread_mutex_t *const mutex) { qlock(cond->l); qunlock(mutex); rsleep(cond); qlock(mutex); qunlock(cond->l); return 0; } int pthread_cond_signal(pthread_cond_t *const cond) { qlock(cond->l); rwakeup(cond); qunlock(cond->l); return 0; } int pthread_cond_broadcast(pthread_cond_t *const cond) { qlock(cond->l); rwakeupall(cond); qunlock(cond->l); return 0; }