2 .\" Copyright (c) 2010 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
38 .Nm mtx_lock_sh_quick ,
40 .Nm mtx_lock_ex_quick ,
53 .Nm mtx_notlocked_ex ,
59 .Nd general blocking/spinnable mutex functions
64 .Fn mtx_init "struct mtx *mtx"
66 .Fn mtx_uninit "struct mtx *mtx"
68 .Fn mtx_lock_sh "struct mtx *mtx" "const char *ident" "int flags" "int to"
70 .Fn mtx_lock_sh_quick "struct mtx *mtx" "const char *ident"
72 .Fn mtx_lock_ex "struct mtx *mtx" "const char *ident" "int flags" "int to"
74 .Fn mtx_lock_ex_quick "struct mtx *mtx" "const char *ident"
76 .Fn mtx_spinlock_ex "struct mtx *mtx"
78 .Fn mtx_spinlock_sh "struct mtx *mtx"
80 .Fn mtx_lock_ex_try "struct mtx *mtx"
82 .Fn mtx_lock_sh_try "struct mtx *mtx"
84 .Fn mtx_downgrade "struct mtx *mtx"
86 .Fn mtx_upgrade_try "struct mtx *mtx"
88 .Fn mtx_unlock "struct mtx *mtx"
90 .Fn mtx_unlock_ex "struct mtx *mtx"
92 .Fn mtx_unlock_sh "struct mtx *mtx"
94 .Fn mtx_islocked "struct mtx *mtx"
96 .Fn mtx_islocked_ex "struct mtx *mtx"
98 .Fn mtx_notlocked "struct mtx *mtx"
100 .Fn mtx_notlocked_ex "struct mtx *mtx"
102 .Fn mtx_owned "struct mtx *mtx"
104 .Fn mtx_notowned "struct mtx *mtx"
106 .Fn mtx_lockrefs "struct mtx *mtx"
108 .Fn mtx_hold "struct mtx *mtx"
110 .Fn mtx_drop "struct mtx *mtx"
112 Mutexes are used to implement mutual exclusion between threads.
113 Mutexes can be locked in shared or exclusive mode; they can also block
114 or spin the current thread when there is contention.
116 Mutexes also have an associated reference count, independent of the lock.
118 System-wide mutex contention statistics can be found in the
119 .Va kern.mtx_contention_count ,
120 .Va kern.mtx_collision_count ,
122 .Va kern.mtx_wakeup_count
124 .Va kern.mtx_contention_count
125 is incremented each time an attempt to acquire a mutex fails due to contention.
126 .Va kern.mtx_wakeup_count
127 is incremented each time an exclusive lock is converted to either a shared or
128 unlocked state an waiters for the shared state are woken.
130 The mutex functions are similar to the
136 function initializes a mutex to the unlocked state.
137 It is an error to use a mutex without initializing it.
141 function deinitializes a mutex.
145 function attempts to lock a mutex in shared mode and blocks the current
146 thread until it is able to do so.
151 it is a string describing the reason for a thread to be blocked.
154 parameter is passed to
156 if the thread must block; the to parameter is a timeout for the sleep.
157 .Fn mtx_lock_sh_quick
158 is a version without flags or a timeout.
162 function attempts to lock a mutex exclusively and blocks the current thread
163 until it is able to do so.
166 parameter and flags parameters are as in
170 parameter is a timeout on the sleep.
171 .Fa mtx_lock_ex_quick
172 is is a version without flags or a timeout.
176 function attempts to lock the mutex in exclusive mode and spins until it is
179 function attempts to lock the mutex in shared mode and spins until it is
186 functions attempt to lock the mutex in exclusive or shared mode, respectively.
187 If they are not able to, they return
192 function converts an exclusively held lock to a shared lock.
193 The lock must be held by the calling thread.
194 If the lock is already shared, this call is a no-op.
198 function attempts to convert a shared lock to an exclusive one.
199 The mutex must be held by the caller in the shared state.
200 If the upgrade is successful, this function returns 0; otherwise, it returns
205 function releases a held mutex;
206 it works on both exclusive and shared mutexes.
211 functions are optimized unlock paths, used when it is known that a lock is held
212 exclusively or in shared state.
216 function returns non-zero if the mutex is locked in either shared of
217 exclusive state by any thread.
219 returns non-zero if the mutex is locked exclusively by any thread.
222 function returns non-zero if the mutex is not locked.
225 function returns non-zero if the mutex is exclusively locked by the calling
229 function returns non-zero if the mutex is not exclusively locked by the
233 function returns the number of shared or exclusive locks on the mutex.
237 function increments the reference count associated with each mutex.
238 The reference count is independent of the lock field.
241 function decrements the reference count associated with each mutex
242 and returns the old value of the count.
245 means that the current count is
248 The uncontended path of the spinlock implementation is in
249 .Pa /sys/sys/mutex2.h .
250 The data structures are in
251 .Pa /sys/sys/mutex.h .
252 The core of the spinlock implementation is in
253 .Pa /sys/kern/kern_mutex.c .
260 Mutexes first appeared in
266 implementation was written by