add make CONF=pthread for modern systems

This commit is contained in:
Russ Cox
2008-12-09 07:05:09 +00:00
parent 189f67ae9b
commit 4f14409f56
11 changed files with 590 additions and 3 deletions

View File

@ -1,6 +1,61 @@
#include <u.h>
#include <libc.h>
#ifdef PTHREAD
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);
}
void
lock(Lock *lk)
{
if(!lk->init)
lockinit(lk);
if(pthread_mutex_lock(&lk->mutex) != 0)
abort();
}
int
canlock(Lock *lk)
{
int r;
if(!lk->init)
lockinit(lk);
r = pthread_mutex_trylock(&lk->mutex);
if(r == 0)
return 1;
if(r == EBUSY)
return 0;
abort();
}
void
unlock(Lock *lk)
{
if(pthread_mutex_unlock(&lk->mutex) != 0)
abort();
}
#else
/* old, non-pthread systems */
int
canlock(Lock *lk)
{
@ -50,6 +105,8 @@ unlock(Lock *lk)
lk->key = 0;
}
#endif
void
ilock(Lock *lk)
{
@ -61,4 +118,3 @@ iunlock(Lock *lk)
{
unlock(lk);
}