diff options
author | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-09-26 22:24:31 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-09-26 22:24:31 +0200 |
commit | cdc2c30e99f2fb3d65dfbc8ef73efd433a3f1966 (patch) | |
tree | 5c3d5a6a42311f09444ca34d09bfebc78e08f5a3 /sys/src/libc/port/lock.c | |
parent | b4cdfc6c5517390d6be05b2c01e56bacc9e85ea8 (diff) |
reverting semaphore lock changes from sources (r41ccd6d221da, rb28756e5ba29)
semaphore locks have much higher overhead than initially presented
in the "Semaphores in Plan9" paper. until the reason for it has been
found out i will revert the changes.
Diffstat (limited to 'sys/src/libc/port/lock.c')
-rw-r--r-- | sys/src/libc/port/lock.c | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/sys/src/libc/port/lock.c b/sys/src/libc/port/lock.c index 213fcd778..af07b9cc7 100644 --- a/sys/src/libc/port/lock.c +++ b/sys/src/libc/port/lock.c @@ -2,32 +2,40 @@ #include <libc.h> void -lock(Lock *l) +lock(Lock *lk) { - if(ainc(&l->key) == 1) - return; /* changed from 0 -> 1: we hold lock */ - /* otherwise wait in kernel */ - while(semacquire(&l->sem, 1) < 0){ - /* interrupted; try again */ + int i; + + /* once fast */ + if(!_tas(&lk->val)) + return; + /* a thousand times pretty fast */ + for(i=0; i<1000; i++){ + if(!_tas(&lk->val)) + return; + sleep(0); + } + /* now nice and slow */ + for(i=0; i<1000; i++){ + if(!_tas(&lk->val)) + return; + sleep(100); } + /* take your time */ + while(_tas(&lk->val)) + sleep(1000); } -void -unlock(Lock *l) +int +canlock(Lock *lk) { - if(adec(&l->key) == 0) - return; /* changed from 1 -> 0: no contention */ - semrelease(&l->sem, 1); + if(_tas(&lk->val)) + return 0; + return 1; } -int -canlock(Lock *l) +void +unlock(Lock *lk) { - if(ainc(&l->key) == 1) - return 1; /* changed from 0 -> 1: success */ - /* Undo increment (but don't miss wakeup) */ - if(adec(&l->key) == 0) - return 0; /* changed from 1 -> 0: no contention */ - semrelease(&l->sem, 1); - return 0; + lk->val = 0; } |