2 .\" Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in
12 .\" the documentation and/or other materials provided with the
14 .\" 3. Neither the name of The DragonFly Project nor the names of its
15 .\" contributors may be used to endorse or promote products derived
16 .\" from this software without specific, prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 .\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 .\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 .\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 .Fn spin_init "struct spinlock *mtx"
49 .Fn spin_uninit "struct spinlock *mtx"
51 .Fn spin_lock "struct spinlock *mtx"
53 .Fn spin_lock_quick "globaldata_t gd" "struct spinlock *mtx"
55 .Fn spin_trylock "struct spinlock *mtx"
57 .Fn spin_unlock "struct spinlock *mtx"
59 .Fn spin_unlock_quick "globaldata_t gd" "struct spinlock *mtx"
63 structure and call API are defined in the
67 header files, respectively.
71 function initializes a new
74 The structure is cleaned up with
76 when it is no longer needed.
80 function obtains an exclusive
83 A thread may hold any number of exclusive spinlocks but should always
84 be mindful of ordering deadlocks.
89 if the spinlock was successfully obtained and
92 If you have the current CPU's
94 pointer in hand you can call
96 but most code will just call the normal version.
97 A spinlock used only for exclusive access has about the same overhead
98 as a mutex based on a locked bus cycle.
100 A previously obtained exclusive spinlock is released by calling either
103 .Fn spin_unlock_quick .
104 .Sh IMPLEMENTATION NOTES
105 A thread may not hold any spinlock across a blocking condition or
107 LWKT tokens should be used for situations where you want an exclusive
108 run-time lock that will survive a blocking condition or thread switch.
109 Tokens will be automatically unlocked when a thread switches away and
110 relocked when the thread is switched back in.
111 If you want a lock that survives a blocking condition or thread switch
112 without being released, use
114 locks or LWKT reader/writer locks.
117 core spinlocks should only be used around small contained sections of
119 For example, to manage a reference count or to implement higher level
121 Both the token code and the
123 code use exclusive spinlocks internally.
124 Core spinlocks should not be used around large chunks of code.
126 Holding one or more spinlocks will disable thread preemption by
127 another thread (e.g. preemption by an interrupt thread), but will not
128 disable FAST interrupts or IPIs.
129 This means that a FAST interrupt can still operate during a spinlock,
130 and any threaded interrupt (which is basically all interrupts except
131 the clock interrupt) will still be scheduled for later execution, but
132 will not be able to preempt the current thread.
133 If you wish to disable FAST interrupts and IPIs you need to enter a
134 critical section prior to obtaining the spinlock.
136 Currently, FAST interrupts, including IPI messages, are not allowed to
137 acquire any spinlocks.
138 It is possible to work around this if
139 .Va mycpu->gd_spinlocks_wr
142 or the other is not zero, the FAST interrupt or IPI cannot acquire
143 any spinlocks without risking a deadlock, even if the spinlocks in
144 question are not related.
146 A thread may hold any number of exclusive
151 A thread will not block, switch away, or lose its critical section
152 while obtaining or releasing a spinlock.
153 Spinlocks do not use IPIs or other mechanisms.
154 They are considered to be a very low level mechanism.
156 If a spinlock can not be obtained after one second a warning will be
157 printed on the console.
158 If a system panic occurs, spinlocks will succeed after one second in
159 order to allow the panic operation to proceed.
161 If you have a complex structure such as a
163 which contains a token or
165 lock, it is legal to directly access the internal spinlock embedded
166 in those structures for other purposes as long as the spinlock is not
167 held when you issue the token or
171 The uncontended path of the spinlock implementation is in
172 .Pa /sys/sys/spinlock2.h .
173 The core of the spinlock implementation is in
174 .Pa /sys/kern/kern_spinlock.c .
182 implementation first appeared in
188 implementation was written by
190 and was later extended by
192 This manual page was written by