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
39 .Nm mtx_lock_sh_quick ,
41 .Nm mtx_lock_ex_quick ,
54 .Nm mtx_notlocked_ex ,
60 .Nd general blocking/spinnable mutex functions
65 .Fn mtx_init "struct mtx *mtx"
67 .Fn mtx_uninit "struct mtx *mtx"
69 .Fn mtx_lock_sh "struct mtx *mtx" "const char *ident" "int flags" "int to"
71 .Fn mtx_lock_sh_quick "struct mtx *mtx" "const char *ident"
73 .Fn mtx_lock_ex "struct mtx *mtx" "const char *ident" "int flags" "int to"
75 .Fn mtx_lock_ex_quick "struct mtx *mtx" "const char *ident"
77 .Fn mtx_spinlock_ex "struct mtx *mtx"
79 .Fn mtx_spinlock_sh "struct mtx *mtx"
81 .Fn mtx_lock_ex_try "struct mtx *mtx"
83 .Fn mtx_lock_sh_try "struct mtx *mtx"
85 .Fn mtx_downgrade "struct mtx *mtx"
87 .Fn mtx_upgrade_try "struct mtx *mtx"
89 .Fn mtx_unlock "struct mtx *mtx"
91 .Fn mtx_unlock_ex "struct mtx *mtx"
93 .Fn mtx_unlock_sh "struct mtx *mtx"
95 .Fn mtx_islocked "struct mtx *mtx"
97 .Fn mtx_islocked_ex "struct mtx *mtx"
99 .Fn mtx_notlocked "struct mtx *mtx"
101 .Fn mtx_notlocked_ex "struct mtx *mtx"
103 .Fn mtx_owned "struct mtx *mtx"
105 .Fn mtx_notowned "struct mtx *mtx"
107 .Fn mtx_lockrefs "struct mtx *mtx"
109 .Fn mtx_hold "struct mtx *mtx"
111 .Fn mtx_drop "struct mtx *mtx"
113 Mutexes are used to implement mutual exclusion between threads.
114 Mutexes can be locked in shared or exclusive mode; they can also block
115 or spin the current thread when there is contention.
117 Mutexes also have an associated reference count, independent of the lock.
119 System-wide mutex contention statistics can be found in the
120 .Va kern.mtx_contention_count ,
121 .Va kern.mtx_collision_count ,
123 .Va kern.mtx_wakeup_count
125 .Va kern.mtx_contention_count
126 is incremented each time an attempt to acquire a mutex fails due to contention.
127 .Va kern.mtx_wakeup_count
128 is incremented each time an exclusive lock is converted to either a shared or
129 unlocked state an waiters for the shared state are woken.
131 The mutex functions are similar to the
137 function initializes a mutex to the unlocked state.
138 It is an error to use a mutex without initializing it.
142 function deinitializes a mutex.
146 function attempts to lock a mutex in shared mode and blocks the current
147 thread until it is able to do so.
152 it is a string describing the reason for a thread to be blocked.
155 parameter is passed to
157 if the thread must block; the to parameter is a timeout for the sleep.
158 .Fn mtx_lock_sh_quick
159 is a version without flags or a timeout.
163 function attempts to lock a mutex exclusively and blocks the current thread
164 until it is able to do so.
167 parameter and flags parameters are as in
171 parameter is a timeout on the sleep.
172 .Fa mtx_lock_ex_quick
173 is is a version without flags or a timeout.
177 function attempts to lock the mutex in exclusive mode and spins until it is
180 function attempts to lock the mutex in shared mode and spins until it is
187 functions attempt to lock the mutex in exclusive or shared mode, respectively.
188 If they are not able to, they return
193 function converts an exclusively held lock to a shared lock.
194 The lock must be held by the calling thread.
195 If the lock is already shared, this call is a no-op.
199 function attempts to convert a shared lock to an exclusive one.
200 The mutex must be held by the caller in the shared state.
201 If the upgrade is successful, this function returns 0; otherwise, it returns
206 function releases a held mutex;
207 it works on both exclusive and shared mutexes.
212 functions are optimized unlock paths, used when it is known that a lock is held
213 exclusively or in shared state.
217 function returns non-zero if the mutex is locked in either shared of
218 exclusive state by any thread.
220 returns non-zero if the mutex is locked exclusively by any thread.
223 function returns non-zero if the mutex is not locked.
226 function returns non-zero if the mutex is exclusively locked by the calling
230 function returns non-zero if the mutex is not exclusively locked by the
234 function returns the number of shared or exclusive locks on the mutex.
238 function increments the reference count associated with each mutex.
239 The reference count is independent of the lock field.
242 function decrements the reference count associated with each mutex
243 and returns the old value of the count.
246 means that the current count is
249 The uncontended path of the
252 .Pa /sys/sys/mutex2.h .
253 The data structures are in
254 .Pa /sys/sys/mutex.h .
255 The core of the spinlock implementation is in
256 .Pa /sys/kern/kern_mutex.c .
263 Mutexes first appeared in
269 implementation was written by