kernel - remove spin_lock_rd() and spin_unlock_rd()
[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
MD
59
60#ifdef SMP
61
8f165b8c
MD
62extern int spin_trylock_wr_contested2(globaldata_t gd);
63extern void spin_lock_wr_contested2(struct spinlock *mtx);
9d265729 64
d666840a
MD
65#endif
66
67#ifdef SMP
68
69/*
70 * Attempt to obtain an exclusive spinlock. Returns FALSE on failure,
e124976c 71 * TRUE on success.
d666840a
MD
72 */
73static __inline boolean_t
74spin_trylock_wr(struct spinlock *mtx)
69d78e99 75{
d666840a
MD
76 globaldata_t gd = mycpu;
77 int value;
78
77912481
MD
79 ++gd->gd_curthread->td_critcount;
80 cpu_ccfence();
d666840a
MD
81 ++gd->gd_spinlocks_wr;
82 if ((value = atomic_swap_int(&mtx->lock, SPINLOCK_EXCLUSIVE)) != 0)
8f165b8c 83 return (spin_trylock_wr_contested2(gd));
d666840a 84 return (TRUE);
69d78e99
MD
85}
86
e124976c
MD
87#else
88
89static __inline boolean_t
90spin_trylock_wr(struct spinlock *mtx)
91{
92 globaldata_t gd = mycpu;
93
77912481
MD
94 ++gd->gd_curthread->td_critcount;
95 cpu_ccfence();
e124976c
MD
96 ++gd->gd_spinlocks_wr;
97 return (TRUE);
98}
99
9d265729
MD
100#endif
101
102/*
8f165b8c 103 * Obtain an exclusive spinlock and return.
9d265729 104 */
d666840a
MD
105static __inline void
106spin_lock_wr_quick(globaldata_t gd, struct spinlock *mtx)
35a832df 107{
d666840a
MD
108#ifdef SMP
109 int value;
9d265729 110#endif
d666840a 111
77912481
MD
112 ++gd->gd_curthread->td_critcount;
113 cpu_ccfence();
d666840a
MD
114 ++gd->gd_spinlocks_wr;
115#ifdef SMP
8f165b8c
MD
116 if ((value = atomic_swap_int(&mtx->lock, SPINLOCK_EXCLUSIVE)) != 0)
117 spin_lock_wr_contested2(mtx);
d666840a
MD
118#endif
119}
120
121static __inline void
122spin_lock_wr(struct spinlock *mtx)
123{
124 spin_lock_wr_quick(mycpu, mtx);
35a832df
MD
125}
126
9d265729 127/*
d666840a
MD
128 * Release an exclusive spinlock. We can just do this passively, only
129 * ensuring that our spinlock count is left intact until the mutex is
130 * cleared.
9d265729 131 */
35a832df 132static __inline void
d666840a 133spin_unlock_wr_quick(globaldata_t gd, struct spinlock *mtx)
35a832df 134{
d666840a
MD
135#ifdef SMP
136 mtx->lock = 0;
137#endif
4ccaa0da 138 KKASSERT(gd->gd_spinlocks_wr > 0);
d666840a 139 --gd->gd_spinlocks_wr;
77912481
MD
140 cpu_ccfence();
141 --gd->gd_curthread->td_critcount;
35a832df
MD
142}
143
503a0d52 144static __inline void
d666840a 145spin_unlock_wr(struct spinlock *mtx)
503a0d52 146{
d666840a 147 spin_unlock_wr_quick(mycpu, mtx);
503a0d52
MD
148}
149
35a832df 150static __inline void
d666840a 151spin_init(struct spinlock *mtx)
35a832df 152{
d666840a 153 mtx->lock = 0;
35a832df
MD
154}
155
156static __inline void
d666840a 157spin_uninit(struct spinlock *mtx)
35a832df 158{
d666840a 159 /* unused */
35a832df
MD
160}
161
03d6a592
MD
162#endif /* _KERNEL */
163#endif /* _SYS_SPINLOCK2_H_ */
35a832df 164