1 #include "../plan9/lib.h" 2 #include "../plan9/sys9.h" 3 #define _LOCK_EXTENSION 4 #include <lock.h> 5 //#include <lib9.h> 6 7 void lock(Lock * l)8lock(Lock *l) 9 { 10 if(ainc(&l->key) == 1) 11 return; /* changed from 0 -> 1: we hold lock */ 12 /* otherwise wait in kernel */ 13 while(_SEMACQUIRE(&l->sem, 1) < 0){ 14 /* interrupted; try again */ 15 } 16 } 17 18 void unlock(Lock * l)19unlock(Lock *l) 20 { 21 if(adec(&l->key) == 0) 22 return; /* changed from 1 -> 0: no contention */ 23 _SEMRELEASE(&l->sem, 1); 24 } 25 26 int canlock(Lock * l)27canlock(Lock *l) 28 { 29 if(ainc(&l->key) == 1) 30 return 1; /* changed from 0 -> 1: success */ 31 /* Undo increment (but don't miss wakeup) */ 32 if(adec(&l->key) == 0) 33 return 0; /* changed from 1 -> 0: no contention */ 34 _SEMRELEASE(&l->sem, 1); 35 return 0; 36 } 37