]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libc/port/lock.c
libregexp: improve the transition to next available thread, instruction, and generation
[plan9front.git] / sys / src / libc / port / lock.c
1 #include <u.h>
2 #include <libc.h>
3
4 void
5 lock(Lock *lk)
6 {
7         int i;
8
9         /* once fast */
10         if(!_tas(&lk->val))
11                 return;
12         /* a thousand times pretty fast */
13         for(i=0; i<1000; i++){
14                 if(!_tas(&lk->val))
15                         return;
16                 sleep(0);
17         }
18         /* now nice and slow */
19         for(i=0; i<1000; i++){
20                 if(!_tas(&lk->val))
21                         return;
22                 sleep(100);
23         }
24         /* take your time */
25         while(_tas(&lk->val))
26                 sleep(1000);
27 }
28
29 int
30 canlock(Lock *lk)
31 {
32         if(_tas(&lk->val))
33                 return 0;
34         return 1;
35 }
36
37 void
38 unlock(Lock *lk)
39 {
40         lk->val = 0;
41 }