shithub: npe

ref: 4c1fa073c7d7fff3c4e21717fcb0ad81a9ab3cf7
dir: /libnpe_pthread/pthread_create.c/

View raw version
#include "_pthread.h"

static void
thread(void *x)
{
	pthread_t t;
	void *p;

	t = x;
	p = t->func(t->arg);
	send(t->waitchan, p);
	chanfree(t->waitchan);
	free(t);

	threadexits(nil);
}

int
pthread_create(pthread_t *pt, const pthread_attr_t *attr, void *(*func)(void*), void *arg)
{
	npe_pthread_t *t;
	int stacksz;

	stacksz = attr == nil ? 0 : attr->stack_size;
	if(stacksz == 0)
		stacksz = mainstacksize;

	t = calloc(1, sizeof(npe_pthread_t));
	t->waitchan = chancreate(sizeof(void*), 0);
	t->func = func;
	t->arg = arg;
	t->pid = proccreate(thread, t, stacksz);
	*pt = t;

	return 0;
}