kernel - Major MPSAFE Infrastructure
[dragonfly.git] / sys / sys / spinlock2.h
CommitLineData
35a832df
MD
1/*
2 * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey M. Hsu.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
74af985e 32 * $DragonFly: src/sys/sys/spinlock2.h,v 1.12 2008/06/04 04:34:54 nth Exp $
35a832df
MD
33 */
34
35#ifndef _SYS_SPINLOCK2_H_
36#define _SYS_SPINLOCK2_H_
37
03d6a592
MD
38#ifndef _KERNEL
39
40#error "This file should not be included by userland programs."
41
42#else
43
bbb31c5d
MD
44#ifndef _SYS_SYSTM_H_
45#include <sys/systm.h>
46#endif
03d6a592 47#ifndef _SYS_THREAD2_H_
35a832df 48#include <sys/thread2.h>
03d6a592 49#endif
d666840a
MD
50#ifndef _SYS_GLOBALDATA_H_
51#include <sys/globaldata.h>
52#endif
03d6a592 53#ifndef _MACHINE_ATOMIC_H_
35a832df 54#include <machine/atomic.h>
03d6a592
MD
55#endif
56#ifndef _MACHINE_CPUFUNC_H_
35a832df 57#include <machine/cpufunc.h>
03d6a592 58#endif
35a832df 59
d666840a
MD
60/*
61 * SPECIAL NOTE! Obtaining a spinlock does not enter a critical section
62 * or protect against FAST interrupts but it will prevent thread preemption.
63 * Because the spinlock code path is ultra critical, we do not check for
64 * LWKT reschedule requests (due to an interrupt thread not being able to
65 * preempt).
66 */
67
35a832df
MD
68#ifdef SMP
69
74af985e
NT
70extern int spin_trylock_wr_contested(globaldata_t gd, struct spinlock *mtx,
71 int value);
d666840a
MD
72extern void spin_lock_wr_contested(struct spinlock *mtx, int value);
73extern void spin_lock_rd_contested(struct spinlock *mtx);
9d265729 74
d666840a
MD
75#endif
76
77#ifdef SMP
78
79/*
80 * Attempt to obtain an exclusive spinlock. Returns FALSE on failure,
e124976c 81 * TRUE on success.
d666840a
MD
82 */
83static __inline boolean_t
84spin_trylock_wr(struct spinlock *mtx)
69d78e99 85{
d666840a
MD
86 globaldata_t gd = mycpu;
87 int value;
88
77912481
MD
89 ++gd->gd_curthread->td_critcount;
90 cpu_ccfence();
d666840a
MD
91 ++gd->gd_spinlocks_wr;
92 if ((value = atomic_swap_int(&mtx->lock, SPINLOCK_EXCLUSIVE)) != 0)
74af985e 93 return (spin_trylock_wr_contested(gd, mtx, value));
d666840a 94 return (TRUE);
69d78e99
MD
95}
96
e124976c
MD
97#else
98
99static __inline boolean_t
100spin_trylock_wr(struct spinlock *mtx)
101{
102 globaldata_t gd = mycpu;
103
77912481
MD
104 ++gd->gd_curthread->td_critcount;
105 cpu_ccfence();
e124976c
MD
106 ++gd->gd_spinlocks_wr;
107 return (TRUE);
108}
109
9d265729
MD
110#endif
111
112/*
e5c830a4
MD
113 * Obtain an exclusive spinlock and return. Shortcut the case where the only
114 * cached read lock was from our own cpu (it can just be cleared).
9d265729 115 */
d666840a
MD
116static __inline void
117spin_lock_wr_quick(globaldata_t gd, struct spinlock *mtx)
35a832df 118{
d666840a
MD
119#ifdef SMP
120 int value;
9d265729 121#endif
d666840a 122
77912481
MD
123 ++gd->gd_curthread->td_critcount;
124 cpu_ccfence();
d666840a
MD
125 ++gd->gd_spinlocks_wr;
126#ifdef SMP
e5c830a4
MD
127 if ((value = atomic_swap_int(&mtx->lock, SPINLOCK_EXCLUSIVE)) != 0) {
128 value &= ~gd->gd_cpumask;
129 if (value)
130 spin_lock_wr_contested(mtx, value);
131 }
d666840a
MD
132#endif
133}
134
135static __inline void
136spin_lock_wr(struct spinlock *mtx)
137{
138 spin_lock_wr_quick(mycpu, mtx);
35a832df
MD
139}
140
69d78e99 141/*
d666840a
MD
142 * Obtain a shared spinlock and return. This is a critical code path.
143 *
144 * The vast majority of the overhead is in the cpu_mfence() (5ns vs 1ns for
145 * the entire rest of the procedure). Unfortunately we have to ensure that
bbb31c5d 146 * spinlock pointer is written out before we check the cpumask to interlock
d666840a 147 * against an exclusive spinlock that clears the cpumask and then checks
bbb31c5d 148 * the spinlock pointer.
d666840a
MD
149 *
150 * But what is EXTREMELY important here is that we do not have to perform
151 * a locked bus cycle on the spinlock itself if the shared bit for our cpu
152 * is already found to be set. We only need the mfence, and the mfence is
153 * local to the cpu and never conflicts with other cpu's.
9d265729 154 *
d666840a
MD
155 * This means that multiple parallel shared acessors (e.g. filedescriptor
156 * table lookups, namecache lookups) run at full speed and incur NO cache
bbb31c5d 157 * contention at all. It is the difference between 10ns and 40-100ns.
69d78e99 158 */
35a832df 159static __inline void
d666840a 160spin_lock_rd_quick(globaldata_t gd, struct spinlock *mtx)
35a832df 161{
77912481
MD
162 ++gd->gd_curthread->td_critcount;
163 cpu_ccfence();
bbb31c5d 164 gd->gd_spinlock_rd = mtx;
d666840a
MD
165#ifdef SMP
166 cpu_mfence();
167 if ((mtx->lock & gd->gd_cpumask) == 0)
168 spin_lock_rd_contested(mtx);
9d265729 169#endif
35a832df
MD
170}
171
172static __inline void
d666840a 173spin_lock_rd(struct spinlock *mtx)
35a832df 174{
d666840a 175 spin_lock_rd_quick(mycpu,mtx);
35a832df
MD
176}
177
9d265729 178/*
d666840a
MD
179 * Release an exclusive spinlock. We can just do this passively, only
180 * ensuring that our spinlock count is left intact until the mutex is
181 * cleared.
9d265729 182 */
35a832df 183static __inline void
d666840a 184spin_unlock_wr_quick(globaldata_t gd, struct spinlock *mtx)
35a832df 185{
d666840a
MD
186#ifdef SMP
187 mtx->lock = 0;
188#endif
4ccaa0da 189 KKASSERT(gd->gd_spinlocks_wr > 0);
d666840a 190 --gd->gd_spinlocks_wr;
77912481
MD
191 cpu_ccfence();
192 --gd->gd_curthread->td_critcount;
35a832df
MD
193}
194
503a0d52 195static __inline void
d666840a 196spin_unlock_wr(struct spinlock *mtx)
503a0d52 197{
d666840a 198 spin_unlock_wr_quick(mycpu, mtx);
503a0d52
MD
199}
200
dd55d707 201/*
d666840a 202 * Release a shared spinlock. We leave the shared bit set in the spinlock
bbb31c5d 203 * as a cache and simply clear the spinlock pointer for the cpu. This
d666840a 204 * fast-paths another shared lock later at the cost of an exclusive lock
bbb31c5d 205 * having to check per-cpu spinlock pointers to determine when there are no
d666840a 206 * shared holders remaining.
dd55d707 207 */
d666840a
MD
208static __inline void
209spin_unlock_rd_quick(globaldata_t gd, struct spinlock *mtx)
210{
bbb31c5d
MD
211 KKASSERT(gd->gd_spinlock_rd == mtx);
212 gd->gd_spinlock_rd = NULL;
77912481
MD
213 cpu_ccfence();
214 --gd->gd_curthread->td_critcount;
d666840a 215}
35a832df 216
d666840a
MD
217static __inline void
218spin_unlock_rd(struct spinlock *mtx)
219{
220 spin_unlock_rd_quick(mycpu, mtx);
221}
35a832df
MD
222
223static __inline void
d666840a 224spin_init(struct spinlock *mtx)
35a832df 225{
d666840a 226 mtx->lock = 0;
35a832df
MD
227}
228
229static __inline void
d666840a 230spin_uninit(struct spinlock *mtx)
35a832df 231{
d666840a 232 /* unused */
35a832df
MD
233}
234
03d6a592
MD
235#endif /* _KERNEL */
236#endif /* _SYS_SPINLOCK2_H_ */
35a832df 237