blob: c0966fcac9f7c920de17e0487b7b160d9c4b09a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#include <u.h>
#include <libc.h>
void
lock(Lock *lk)
{
int i;
/* once fast */
if(!_tas((int*)&lk->key))
return;
/* a thousand times pretty fast */
for(i=0; i<1000; i++){
if(!_tas((int*)&lk->key))
return;
sleep(0);
}
/* now nice and slow */
for(i=0; i<1000; i++){
if(!_tas((int*)&lk->key))
return;
sleep(100);
}
/* take your time */
while(_tas((int*)&lk->key))
sleep(1000);
}
int
canlock(Lock *lk)
{
if(_tas((int*)&lk->key))
return 0;
return 1;
}
void
unlock(Lock *lk)
{
lk->key = 0;
}
|