This source file includes following definitions.
- initlock
- acquire
- release
- getcallerpcs
- holding
- pushcli
- popcli
1
2
3 #include "types.h"
4 #include "defs.h"
5 #include "param.h"
6 #include "x86.h"
7 #include "memlayout.h"
8 #include "mmu.h"
9 #include "proc.h"
10 #include "spinlock.h"
11
12 void
13 initlock(struct spinlock *lk, char *name)
14 {
15 lk->name = name;
16 lk->locked = 0;
17 lk->cpu = 0;
18 }
19
20
21
22
23
24 void
25 acquire(struct spinlock *lk)
26 {
27 pushcli();
28 if(holding(lk))
29 panic("acquire");
30
31
32 while(xchg(&lk->locked, 1) != 0)
33 ;
34
35
36
37
38 __sync_synchronize();
39
40
41 lk->cpu = mycpu();
42 getcallerpcs(&lk, lk->pcs);
43 }
44
45
46 void
47 release(struct spinlock *lk)
48 {
49 if(!holding(lk))
50 panic("release");
51
52 lk->pcs[0] = 0;
53 lk->cpu = 0;
54
55
56
57
58
59
60 __sync_synchronize();
61
62
63
64
65 asm volatile("movl $0, %0" : "+m" (lk->locked) : );
66
67 popcli();
68 }
69
70
71 void
72 getcallerpcs(void *v, uint pcs[])
73 {
74 uint *ebp;
75 int i;
76
77 ebp = (uint*)v - 2;
78 for(i = 0; i < 10; i++){
79 if(ebp == 0 || ebp < (uint*)KERNBASE || ebp == (uint*)0xffffffff)
80 break;
81 pcs[i] = ebp[1];
82 ebp = (uint*)ebp[0];
83 }
84 for(; i < 10; i++)
85 pcs[i] = 0;
86 }
87
88
89 int
90 holding(struct spinlock *lock)
91 {
92 int r;
93 pushcli();
94 r = lock->locked && lock->cpu == mycpu();
95 popcli();
96 return r;
97 }
98
99
100
101
102
103
104 void
105 pushcli(void)
106 {
107 int eflags;
108
109 eflags = readeflags();
110 cli();
111 if(mycpu()->ncli == 0)
112 mycpu()->intena = eflags & FL_IF;
113 mycpu()->ncli += 1;
114 }
115
116 void
117 popcli(void)
118 {
119 if(readeflags()&FL_IF)
120 panic("popcli - interruptible");
121 if(--mycpu()->ncli < 0)
122 panic("popcli");
123 if(mycpu()->ncli == 0 && mycpu()->intena)
124 sti();
125 }
126