xref: /xv6-public/spinlock.c (revision ab08960f)
1 // Mutual exclusion spin locks.
2 
3 #include "types.h"
4 #include "defs.h"
5 #include "param.h"
6 #include "x86.h"
7 #include "mmu.h"
8 #include "proc.h"
9 #include "spinlock.h"
10 
11 extern int use_console_lock;
12 
13 void
14 initlock(struct spinlock *lock, char *name)
15 {
16   lock->name = name;
17   lock->locked = 0;
18   lock->cpu = 0xffffffff;
19 }
20 
21 // Acquire the lock.
22 // Loops (spins) until the lock is acquired.
23 // Holding a lock for a long time may cause
24 // other CPUs to waste time spinning to acquire it.
25 void
26 acquire(struct spinlock *lock)
27 {
28   pushcli();
29   if(holding(lock))
30     panic("acquire");
31 
32   while(cmpxchg(0, 1, &lock->locked) == 1)
33     ;
34 
35   // Serialize instructions: now that lock is acquired, make sure
36   // we wait for all pending writes from other processors.
37   cpuid(0, 0, 0, 0, 0);  // memory barrier (see Ch 7, IA-32 manual vol 3)
38 
39   // Record info about lock acquisition for debugging.
40   // The +10 is only so that we can tell the difference
41   // between forgetting to initialize lock->cpu
42   // and holding a lock on cpu 0.
43   lock->cpu = cpu() + 10;
44   getcallerpcs(&lock, lock->pcs);
45 }
46 
47 // Release the lock.
48 void
49 release(struct spinlock *lock)
50 {
51   if(!holding(lock))
52     panic("release");
53 
54   lock->pcs[0] = 0;
55   lock->cpu = 0xffffffff;
56 
57   // Serialize instructions: before unlocking the lock, make sure
58   // to flush any pending memory writes from this processor.
59   cpuid(0, 0, 0, 0, 0);  // memory barrier (see Ch 7, IA-32 manual vol 3)
60 
61   lock->locked = 0;
62   popcli();
63 }
64 
65 // Record the current call stack in pcs[] by following the %ebp chain.
66 void
67 getcallerpcs(void *v, uint pcs[])
68 {
69   uint *ebp;
70   int i;
71 
72   ebp = (uint*)v - 2;
73   for(i = 0; i < 10; i++){
74     if(ebp == 0 || ebp == (uint*)0xffffffff)
75       break;
76     pcs[i] = ebp[1];     // saved %eip
77     ebp = (uint*)ebp[0]; // saved %ebp
78   }
79   for(; i < 10; i++)
80     pcs[i] = 0;
81 }
82 
83 // Check whether this cpu is holding the lock.
84 int
85 holding(struct spinlock *lock)
86 {
87   return lock->locked && lock->cpu == cpu() + 10;
88 }
89 
90 
91 // Pushcli/popcli are like cli/sti except that they are matched:
92 // it takes two popcli to undo two pushcli.  Also, if interrupts
93 // are off, then pushcli, popcli leaves them off.
94 
95 void
96 pushcli(void)
97 {
98   int eflags;
99 
100   eflags = read_eflags();
101   cli();
102   if(cpus[cpu()].ncli++ == 0)
103     cpus[cpu()].intena = eflags & FL_IF;
104 }
105 
106 void
107 popcli(void)
108 {
109   if(read_eflags()&FL_IF)
110     panic("popcli - interruptible");
111   if(--cpus[cpu()].ncli < 0)
112     panic("popcli");
113   if(cpus[cpu()].ncli == 0 && cpus[cpu()].intena)
114     sti();
115 }
116 
117