From e5888a1ffdae813d7575f5fb02275c6bb07e5199 Mon Sep 17 00:00:00 2001 From: Taru Karttunen Date: Wed, 30 Mar 2011 15:46:40 +0300 Subject: Import sources from 2011-03-30 iso image --- sys/src/cmd/unix/drawterm/libc/lock.c | 120 ++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100755 sys/src/cmd/unix/drawterm/libc/lock.c (limited to 'sys/src/cmd/unix/drawterm/libc/lock.c') diff --git a/sys/src/cmd/unix/drawterm/libc/lock.c b/sys/src/cmd/unix/drawterm/libc/lock.c new file mode 100755 index 000000000..07d435256 --- /dev/null +++ b/sys/src/cmd/unix/drawterm/libc/lock.c @@ -0,0 +1,120 @@ +#include +#include + +#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) +{ + return !tas(&lk->key); +} + +void +lock(Lock *lk) +{ + int i; + + /* easy case */ + if(canlock(lk)) + return; + + /* for multi processor machines */ + for(i=0; i<100; i++) + if(canlock(lk)) + return; + + for(i=0; i<100; i++) { + osyield(); + if(canlock(lk)) + return; + } + + /* looking bad - make sure it is not a priority problem */ + for(i=0; i<12; i++) { + osmsleep(1<key, lk, getcallerpc(&lk)); + osmsleep(1000); + } +} + +void +unlock(Lock *lk) +{ + assert(lk->key); + lk->key = 0; +} + +#endif + +void +ilock(Lock *lk) +{ + lock(lk); +} + +void +iunlock(Lock *lk) +{ + unlock(lk); +} -- cgit v1.2.3