Mercurial > hg > Members > anatofuz > CbC_xv6
comparison src/spinlock.c @ 0:83c23a36980d
Init
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 26 May 2017 23:11:05 +0900 |
parents | |
children | ad1d3b268e2d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:83c23a36980d |
---|---|
1 // Mutual exclusion spin locks. | |
2 | |
3 #include "types.h" | |
4 #include "defs.h" | |
5 #include "param.h" | |
6 #include "arm.h" | |
7 #include "memlayout.h" | |
8 #include "mmu.h" | |
9 #include "proc.h" | |
10 #include "spinlock.h" | |
11 | |
12 void initlock(struct spinlock *lk, char *name) | |
13 { | |
14 lk->name = name; | |
15 lk->locked = 0; | |
16 lk->cpu = 0; | |
17 } | |
18 | |
19 // For single CPU systems, there is no need for spinlock. | |
20 // Add the support when multi-processor is supported. | |
21 | |
22 | |
23 // Acquire the lock. | |
24 // Loops (spins) until the lock is acquired. | |
25 // Holding a lock for a long time may cause | |
26 // other CPUs to waste time spinning to acquire it. | |
27 void acquire(struct spinlock *lk) | |
28 { | |
29 pushcli(); // disable interrupts to avoid deadlock. | |
30 lk->locked = 1; // set the lock status to make the kernel happy | |
31 | |
32 #if 0 | |
33 if(holding(lk)) | |
34 panic("acquire"); | |
35 | |
36 // The xchg is atomic. | |
37 // It also serializes, so that reads after acquire are not | |
38 // reordered before it. | |
39 while(xchg(&lk->locked, 1) != 0) | |
40 ; | |
41 | |
42 // Record info about lock acquisition for debugging. | |
43 lk->cpu = cpu; | |
44 getcallerpcs(get_fp(), lk->pcs); | |
45 | |
46 #endif | |
47 } | |
48 | |
49 // Release the lock. | |
50 void release(struct spinlock *lk) | |
51 { | |
52 #if 0 | |
53 if(!holding(lk)) | |
54 panic("release"); | |
55 | |
56 lk->pcs[0] = 0; | |
57 lk->cpu = 0; | |
58 | |
59 // The xchg serializes, so that reads before release are | |
60 // not reordered after it. The 1996 PentiumPro manual (Volume 3, | |
61 // 7.2) says reads can be carried out speculatively and in | |
62 // any order, which implies we need to serialize here. | |
63 // But the 2007 Intel 64 Architecture Memory Ordering White | |
64 // Paper says that Intel 64 and IA-32 will not move a load | |
65 // after a store. So lock->locked = 0 would work here. | |
66 // The xchg being asm volatile ensures gcc emits it after | |
67 // the above assignments (and after the critical section). | |
68 xchg(&lk->locked, 0); | |
69 #endif | |
70 | |
71 lk->locked = 0; // set the lock state to keep the kernel happy | |
72 popcli(); | |
73 } | |
74 | |
75 | |
76 // Check whether this cpu is holding the lock. | |
77 int holding(struct spinlock *lock) | |
78 { | |
79 return lock->locked; // && lock->cpu == cpus; | |
80 } | |
81 |