shithub: dav1d

ref: 095cc7b2a007ca22ece128d6e764c9598e7d1f83
dir: /src/plan9_thread.c/

View raw version
#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 > mainstacksize ? attr->stack_size : mainstacksize;
	thread->waitchan = chancreate(sizeof(void*), 0);
	thread->func = func;
	thread->arg = arg;
	thread->pid = proccreate(thre, thread, stack);

	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 != 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;
}