-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinlock.h
86 lines (71 loc) · 1.86 KB
/
spinlock.h
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
/*
* Spinlock operations. These were hand-coded specially for this
* effort without reference to GPL code.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (c) 2003 IBM Corporation.
*/
#ifndef __SPINLOCK_H
#define __SPINLOCK_H
#include "arch/atomic.h"
#include <stdlib.h>
#include "backoff.h"
typedef atomic_t spinlock_t;
#define SPIN_LOCK_UNLOCKED 0
/*
* Acquire a spinlock.
*/
static inline void
spin_lock(spinlock_t *slp)
{
unsigned long result;
backoff_reset();
for (;; ) {
result = atomic_cmpxchg4(slp, 0, 1);
if (result == 0) {
spin_lock_barrier();
return;
}
while (*slp != 0) {
backoff_delay();
memory_barrier();
}
}
}
/*
* Conditionally acquire a spinlock.
*/
static inline int
spin_trylock(spinlock_t *slp)
{
unsigned long result;
result = atomic_cmpxchg4(slp, 0, 1);
spin_lock_barrier();
return (!result);
}
/*
* Release a spinlock.
*/
static inline void
spin_unlock(spinlock_t *slp)
{
unsigned long result;
spin_unlock_barrier();
if ((result = atomic_xchg4(slp, SPIN_LOCK_UNLOCKED)) != 1) {
abort();
}
}
#endif /* #ifndef __SPINLOCK_H */