xref: /plan9/sys/src/ape/lib/ap/power/lock.c (revision ec59a3ddbfceee0efe34584c2c9981a5e5ff1ec4)
1 #include "../plan9/lib.h"
2 #include "../plan9/sys9.h"
3 #define _LOCK_EXTENSION
4 #include <lock.h>
5 
6 int	tas(int*);
7 
8 void
9 lock(Lock *lk)
10 {
11 	int i;
12 
13 	/* once fast */
14 	if(!tas(&lk->val))
15 		return;
16 	/* a thousand times pretty fast */
17 	for(i=0; i<1000; i++){
18 		if(!tas(&lk->val))
19 			return;
20 		_SLEEP(0);
21 	}
22 	/* now nice and slow */
23 	for(i=0; i<1000; i++){
24 		if(!tas(&lk->val))
25 			return;
26 		_SLEEP(100);
27 	}
28 	/* take your time */
29 	while(tas(&lk->val))
30 		_SLEEP(1000);
31 }
32 
33 int
34 canlock(Lock *lk)
35 {
36 	if(tas(&lk->val))
37 		return 0;
38 	return 1;
39 }
40 
41 void
42 unlock(Lock *lk)
43 {
44 	lk->val = 0;
45 }
46