diff options
author | rsc <devnull@localhost> | 2004-09-21 01:11:28 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2004-09-21 01:11:28 +0000 |
commit | c6687d4591964cb13df87f55ec4770e778a4a55c (patch) | |
tree | ee669a419904e929ae8ae54fd04c2bb41b5a57b1 /src/lib9/lock-pthread.c | |
parent | 3d5e34e146b5ba5c973230abb624ce9126024569 (diff) | |
download | plan9port-c6687d4591964cb13df87f55ec4770e778a4a55c.tar.gz plan9port-c6687d4591964cb13df87f55ec4770e778a4a55c.tar.bz2 plan9port-c6687d4591964cb13df87f55ec4770e778a4a55c.zip |
Continue the pthreads torture.
Diffstat (limited to 'src/lib9/lock-pthread.c')
-rw-r--r-- | src/lib9/lock-pthread.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/lib9/lock-pthread.c b/src/lib9/lock-pthread.c new file mode 100644 index 00000000..689261f6 --- /dev/null +++ b/src/lib9/lock-pthread.c @@ -0,0 +1,54 @@ +#include <u.h> +#include <unistd.h> +#include <sys/time.h> +#include <sched.h> +#include <libc.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); +} + +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(); +} |