shithub: dav1d

ref: c3c49fcd05049a0f89f84ed1a8ce3c7267ef8a8f
dir: /src/plan9_thread.c/

View raw version
#include <u.h>
#include <libc.h>
#include </sys/include/thread.h>
#include "thread.h"

enum {
	Magic = 1325465,
};

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)
{
	int stacksz = attr == nil ? 0 : attr->stack_size;
	if(stacksz < 65536)
		stacksz = 65536;
	thread->waitchan = chancreate(sizeof(void*), 0);
	thread->func = func;
	thread->arg = arg;
	thread->pid = proccreate(thre, thread, stacksz);

	return 0;
}

int
dav1d_pthread_join(pthread_t *thread, void **res)
{
	// FIXME this is wrong ofc
	if (thread->waitchan != nil) {
		recv(thread->waitchan, res);
		chanfree(thread->waitchan);
		thread->waitchan = nil;
	}

	return 0;
}

int
pthread_once(pthread_once_t *once, void (*init_routine)(void))
{
	qlock(once);
	if (once->done != Magic) {
		init_routine();
		once->done = Magic;
	}
	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)
{
	USED(attr);
    memset(cond, 0, sizeof(*cond));
    cond->l = &cond->lock;
    return 0;
}

int
pthread_cond_destroy(pthread_cond_t *const cond)
{
	USED(cond);
    return 0;
}

int
pthread_cond_wait(pthread_cond_t *const cond, pthread_mutex_t *const mutex)
{
	qlock(cond->l);
	qunlock(mutex);
	rsleep(cond);
	qunlock(cond->l);
	qlock(mutex);
    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;
}