blob: 307c4b7d6fc24b96f73a41e26d7b8b4c690770a3 (
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>
extern uintptr _barrier(uintptr);
void
lock(Lock *lk)
{
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);
}
int
canlock(Lock *lk)
{
return _tas(&lk->val) == 0;
}
void
unlock(Lock *lk)
{
lk->val = _barrier(0);
}
|