plan9port/src/libthread/pthread.c

140 lines
2 KiB
C
Raw Normal View History

2004-12-25 21:57:50 +00:00
#include "threadimpl.h"
static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
static void
lockinit(Lock *lk)
{
pthread_mutexattr_t attr;
pthread_mutex_lock(&initmutex);
if(lk->init == 0){
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
pthread_mutex_init(&lk->mutex, &attr);
pthread_mutexattr_destroy(&attr);
lk->init = 1;
}
pthread_mutex_unlock(&initmutex);
}
int
_threadlock(Lock *lk, int block, ulong pc)
{
int r;
if(!lk->init)
lockinit(lk);
if(block){
if(pthread_mutex_lock(&lk->mutex) != 0)
abort();
return 1;
}else{
r = pthread_mutex_trylock(&lk->mutex);
if(r == 0)
return 1;
if(r == EBUSY)
return 0;
abort();
return 0;
}
}
void
_threadunlock(Lock *lk, ulong pc)
{
if(pthread_mutex_unlock(&lk->mutex) != 0)
abort();
}
void
_procsleep(_Procrendez *r)
{
/* r is protected by r->l, which we hold */
pthread_cond_init(&r->cond, 0);
r->asleep = 1;
pthread_cond_wait(&r->cond, &r->l->mutex);
pthread_cond_destroy(&r->cond);
r->asleep = 0;
}
void
_procwakeup(_Procrendez *r)
{
if(r->asleep){
r->asleep = 0;
pthread_cond_signal(&r->cond);
}
}
2004-12-27 03:49:03 +00:00
static void
startprocfn(void *v)
{
void **a;
void (*fn)(void*);
Proc *p;
a = (void**)v;
fn = a[0];
p = a[1];
free(a);
2004-12-27 16:52:26 +00:00
p->osprocid = pthread_self();
pthread_detach(p->osprocid);
2004-12-27 03:49:03 +00:00
(*fn)(p);
pthread_exit(0);
}
2004-12-25 21:57:50 +00:00
void
2004-12-27 03:49:03 +00:00
_procstart(Proc *p, void (*fn)(Proc*))
2004-12-25 21:57:50 +00:00
{
2004-12-27 03:49:03 +00:00
void **a;
a = malloc(2*sizeof a[0]);
if(a == nil)
sysfatal("_procstart malloc: %r");
a[0] = fn;
a[1] = p;
2004-12-27 16:52:26 +00:00
if(pthread_create(&p->osprocid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
2004-12-25 21:57:50 +00:00
fprint(2, "pthread_create: %r\n");
abort();
}
}
static pthread_key_t prockey;
Proc*
_threadproc(void)
{
Proc *p;
p = pthread_getspecific(prockey);
return p;
}
void
_threadsetproc(Proc *p)
{
pthread_setspecific(prockey, p);
}
void
_pthreadinit(void)
2004-12-25 21:57:50 +00:00
{
pthread_key_create(&prockey, 0);
}
2004-12-28 03:42:11 +00:00
void
threadexitsall(char *msg)
{
exits(msg);
}
void
_threadpexit(void)
{
pthread_exit(0);
}