-
Notifications
You must be signed in to change notification settings - Fork 2
/
spinlock.c
156 lines (139 loc) · 4.16 KB
/
spinlock.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* Make sure to build out-of-line versions of inline functions */
#define SPINLOCK_INLINE /* empty */
#define MEMBAR_INLINE /* empty */
#include <types.h>
#include <lib.h>
#include <cpu.h>
#include <spl.h>
#include <spinlock.h>
#include <membar.h>
#include <current.h> /* for curcpu */
/*
* Spinlocks.
*/
/*
* Initialize spinlock.
*/
void
spinlock_init(struct spinlock *splk)
{
spinlock_data_set(&splk->splk_lock, 0);
splk->splk_holder = NULL;
HANGMAN_LOCKABLEINIT(&splk->splk_hangman, "spinlock");
}
/*
* Clean up spinlock.
*/
void
spinlock_cleanup(struct spinlock *splk)
{
KASSERT(splk->splk_holder == NULL);
KASSERT(spinlock_data_get(&splk->splk_lock) == 0);
}
/*
* Get the lock.
*
* First disable interrupts (otherwise, if we get a timer interrupt we
* might come back to this lock and deadlock), then use a machine-level
* atomic operation to wait for the lock to be free.
*/
void
spinlock_acquire(struct spinlock *splk)
{
struct cpu *mycpu;
splraise(IPL_NONE, IPL_HIGH);
/* this must work before curcpu initialization */
if (CURCPU_EXISTS()) {
mycpu = curcpu->c_self;
if (splk->splk_holder == mycpu) {
panic("Deadlock on spinlock %p\n", splk);
}
mycpu->c_spinlocks++;
HANGMAN_WAIT(&curcpu->c_hangman, &splk->splk_hangman);
}
else {
mycpu = NULL;
}
while (1) {
/*
* Do test-test-and-set, that is, read first before
* doing test-and-set, to reduce bus contention.
*
* Test-and-set is a machine-level atomic operation
* that writes 1 into the lock word and returns the
* previous value. If that value was 0, the lock was
* previously unheld and we now own it. If it was 1,
* we don't.
*/
if (spinlock_data_get(&splk->splk_lock) != 0) {
continue;
}
if (spinlock_data_testandset(&splk->splk_lock) != 0) {
continue;
}
break;
}
membar_store_any();
splk->splk_holder = mycpu;
if (CURCPU_EXISTS()) {
HANGMAN_ACQUIRE(&curcpu->c_hangman, &splk->splk_hangman);
}
}
/*
* Release the lock.
*/
void
spinlock_release(struct spinlock *splk)
{
/* this must work before curcpu initialization */
if (CURCPU_EXISTS()) {
KASSERT(splk->splk_holder == curcpu->c_self);
KASSERT(curcpu->c_spinlocks > 0);
curcpu->c_spinlocks--;
HANGMAN_RELEASE(&curcpu->c_hangman, &splk->splk_hangman);
}
splk->splk_holder = NULL;
membar_any_store();
spinlock_data_set(&splk->splk_lock, 0);
spllower(IPL_HIGH, IPL_NONE);
}
/*
* Check if the current cpu holds the lock.
*/
bool
spinlock_do_i_hold(struct spinlock *splk)
{
if (!CURCPU_EXISTS()) {
return true;
}
/* Assume we can read splk_holder atomically enough for this to work */
return (splk->splk_holder == curcpu->c_self);
}