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_link ,
40 .Nm mtx_lock_sh_quick ,
42 .\".Nm mtx_lock_ex_link ,
43 .Nm mtx_lock_ex_quick ,
48 .Nm mtx_spinlock_try ,
58 .Nm mtx_notlocked_ex ,
62 .Nd general blocking/spinnable mutex functions
67 .Fn mtx_init "mtx_t *mtx, const char *ident"
69 .Fn mtx_uninit "mtx_t *mtx"
71 .Fn mtx_lock_sh "mtx_t *mtx" "int flags" "int to"
73 .\".Fn mtx_lock_sh_link "mtx_t *mtx" "mtx_link_t *link" "int flags" "int to"
75 .Fn mtx_lock_sh_quick "mtx_t *mtx"
77 .Fn mtx_lock_ex "mtx_t *mtx" "int flags" "int to"
79 .\".Fn mtx_lock_ex_link "mtx_t *mtx" "mtx_link_t *link" "int flags" "int to"
81 .Fn mtx_lock_ex_quick "mtx_t *mtx"
83 .Fn mtx_lock "mtx_t *mtx"
85 .Fn mtx_spinlock "mtx_t *mtx"
87 .Fn mtx_lock_ex_try "mtx_t *mtx"
89 .Fn mtx_lock_sh_try "mtx_t *mtx"
91 .Fn mtx_spinlock_try "mtx_t *mtx"
93 .Fn mtx_downgrade "mtx_t *mtx"
95 .Fn mtx_upgrade_try "mtx_t *mtx"
97 .Fn mtx_unlock "mtx_t *mtx"
99 .Fn mtx_unlock_ex "mtx_t *mtx"
101 .Fn mtx_unlock_sh "mtx_t *mtx"
103 .Fn mtx_spinunlock "mtx_t *mtx"
105 .Fn mtx_islocked "mtx_t *mtx"
107 .Fn mtx_islocked_ex "mtx_t *mtx"
109 .Fn mtx_notlocked "mtx_t *mtx"
111 .Fn mtx_notlocked_ex "mtx_t *mtx"
113 .Fn mtx_owned "mtx_t *mtx"
115 .Fn mtx_notowned "mtx_t *mtx"
117 .Fn mtx_lockrefs "mtx_t *mtx"
119 Mutexes are used to implement mutual exclusion between threads.
120 Mutexes can be locked in shared or exclusive mode; they can also block
121 or spin the current thread when there is contention.
123 Mutexes also have an associated reference count, independent of the lock.
125 System-wide mutex contention statistics can be found in the
126 .Va kern.mtx_contention_count ,
127 .Va kern.mtx_collision_count ,
129 .Va kern.mtx_wakeup_count
131 .Va kern.mtx_contention_count
132 is incremented each time an attempt to acquire a mutex fails due to contention.
133 .Va kern.mtx_wakeup_count
134 is incremented each time an exclusive lock is converted to either a shared or
135 unlocked state an waiters for the shared state are woken.
137 The mutex functions are similar to the
143 function initializes a mutex to the unlocked state.
144 It is an error to use a mutex without initializing it.
149 it is a string describing the reason for a thread to be blocked.
153 function deinitializes a mutex.
157 function attempts to lock a mutex in shared mode and blocks the current
158 thread until it is able to do so.
161 parameter is passed to
163 if the thread must block; the
165 parameter is a timeout for the sleep.
167 .Fn mtx_lock_sh_quick
168 function is a version of
170 without flags or a timeout.
174 .Fn mtx_lock_sh_quick
175 functions return 0 on success, or the
177 return code on failure.
178 An error can only be returned if
180 is specified in the flags.
184 function attempts to lock a mutex exclusively and blocks the current thread
185 until it is able to do so.
192 parameter is a timeout on the sleep.
194 .Fn mtx_lock_ex_quick
195 function is a version of
197 without flags or a timeout.
200 function is a yet shorter form for exclusively locking a mutex, blocking the
201 current thread until acquired.
203 .Fn mtx_lock_ex "mtx" \&"mtxex\&" "0" "0" .
207 .Fn mtx_lock_ex_quick
208 functions return 0 on success, or the
210 return code on failure.
211 An error can only be returned if
212 .Dv PCATCH is specified in the flags.
216 function attempts to lock the mutex in exclusive mode and spins until it is
223 functions attempt to lock the mutex in exclusive or shared mode, respectively.
224 If they are not able to, they return
228 function does the same but for spin mutexes.
232 function converts an exclusively held lock to a shared lock.
233 The lock must be held by the calling thread.
234 If the lock is already shared, this call is a no-op.
238 function attempts to convert a shared lock to an exclusive one.
239 The mutex must be held by the caller in the shared state.
240 If the upgrade is successful, this function returns 0; otherwise, it returns
245 function releases a held mutex;
246 it works on both exclusive and shared mutexes.
251 functions are optimized unlock paths, used when it is known that a lock is held
252 exclusively or in shared state.
256 function releases a held spin mutex.
260 function returns non-zero if the mutex is locked in either shared of
261 exclusive state by any thread.
263 returns non-zero if the mutex is locked exclusively by any thread.
266 function returns non-zero if the mutex is not locked.
269 function returns non-zero if the mutex is exclusively locked by the calling
273 function returns non-zero if the mutex is not exclusively locked by the
277 function returns the number of shared or exclusive locks on the mutex.
279 The uncontended path of the
282 .Pa /sys/sys/mutex2.h .
283 The data structures are in
284 .Pa /sys/sys/mutex.h .
285 The core of the spinlock implementation is in
286 .Pa /sys/kern/kern_mutex.c .
295 Mutexes first appeared in
301 implementation was written by