1 .\" $NetBSD: pthread_mutexattr.3,v 1.11 2010/07/08 22:46:34 jruoho Exp $
3 .\" Copyright (c) 2002, 2010 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" 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 the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
17 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 .\" POSSIBILITY OF SUCH DAMAGE.
25 .\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
26 .\" All rights reserved.
28 .\" Redistribution and use in source and binary forms, with or without
29 .\" modification, are permitted provided that the following conditions
31 .\" 1. Redistributions of source code must retain the above copyright
32 .\" notice(s), this list of conditions and the following disclaimer as
33 .\" the first lines of this file unmodified other than the possible
34 .\" addition of one or more copyright notices.
35 .\" 2. Redistributions in binary form must reproduce the above copyright
36 .\" notice(s), this list of conditions and the following disclaimer in
37 .\" the documentation and/or other materials provided with the
40 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
41 .\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
44 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
47 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
48 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
49 .\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50 .\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 .Dt PTHREAD_MUTEXATTR 3
57 .Nm pthread_mutexattr_init ,
58 .Nm pthread_mutexattr_destroy ,
59 .Nm pthread_mutexattr_setprioceiling ,
60 .Nm pthread_mutexattr_getprioceiling ,
61 .Nm pthread_mutexattr_setprotocol ,
62 .Nm pthread_mutexattr_getprotocol ,
63 .Nm pthread_mutexattr_settype ,
64 .Nm pthread_mutexattr_gettype
65 .Nd mutex attribute operations
69 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
71 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
73 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int prioceiling"
75 .Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *prioceiling"
77 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
79 .Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
81 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
83 .Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
85 .Fn pthread_mutexattr_setpolicy_np "pthread_mutexattr_t *attr" "int policy"
87 .Fn pthread_mutexattr_getpolicy_np "pthread_mutexattr_t *attr" "int *policy"
89 Mutex attributes are used to specify parameters to
90 .Fn pthread_mutex_init .
91 Like with thread attributes,
92 one attribute object can be used in multiple calls to
93 .Xr pthread_mutex_init 3 ,
94 with or without modifications between calls.
97 .Fn pthread_mutexattr_init
100 with all the default mutex attributes.
103 .Fn pthread_mutexattr_destroy
108 .Fn pthread_mutexattr_settype
109 functions set the mutex
111 value of the attribute.
112 Valid mutex types are:
113 .Bl -tag -width "XXX" -offset 2n
114 .It Dv PTHREAD_MUTEX_NORMAL
115 This type of mutex does not check for usage errors.
116 It will deadlock if reentered, and result in undefined behavior if a
117 locked mutex is unlocked by another thread.
118 Attempts to unlock an already unlocked
119 .Dv PTHREAD_MUTEX_NORMAL
120 mutex will result in undefined behavior.
121 .It Dv PTHREAD_MUTEX_ERRORCHECK
122 These mutexes do check for usage errors.
123 If an attempt is made to relock a
124 .Dv PTHREAD_MUTEX_ERRORCHECK
125 mutex without first dropping the lock, an error will be returned.
126 If a thread attempts to unlock a
127 .Dv PTHREAD_MUTEX_ERRORCHECK
128 mutex that is locked by another thread, an error will be returned.
129 If a thread attempts to unlock a
130 .Dv PTHREAD_MUTEX_ERRORCHECK
131 thread that is unlocked, an error will be returned.
132 .It Dv PTHREAD_MUTEX_RECURSIVE
133 These mutexes allow recursive locking.
134 An attempt to relock a
135 .Dv PTHREAD_MUTEX_RECURSIVE
136 mutex that is already locked by the same thread succeeds.
137 An equivalent number of
138 .Xr pthread_mutex_unlock 3
139 calls are needed before the mutex will wake another thread waiting
141 If a thread attempts to unlock a
142 .Dv PTHREAD_MUTEX_RECURSIVE
143 mutex that is locked by another thread, an error will be returned.
144 If a thread attempts to unlock a
145 .Dv PTHREAD_MUTEX_RECURSIVE
146 thread that is unlocked, an error will be returned.
149 .Dv PTHREAD_MUTEX_RECURSIVE
150 mutexes are not used with condition variables.
151 This is because of the implicit unlocking done by
152 .Xr pthread_cond_wait 3
154 .Xr pthread_cond_timedwait 3 .
155 .It Dv PTHREAD_MUTEX_DEFAULT
156 Also this type of mutex will cause undefined behavior if reentered.
158 .Dv PTHREAD_MUTEX_DEFAULT
159 mutex locked by another thread will result in undefined behavior.
160 Attempts to unlock an already unlocked
161 .Dv PTHREAD_MUTEX_DEFAULT
162 mutex will result in undefined behavior.
163 This is the default mutex type for
164 .Fn pthread_mutexaddr_init .
167 .Fn pthread_mutexattr_gettype
168 functions copy the type value of the attribute to the location pointed to by the second parameter.
171 .Fn pthread_mutexattr_setpolicy_np
172 function sets the mutex
174 value of the attribute.
175 Valid mutex policies are:
176 .Bl -tag -width "XXX" -offset 2n
177 .It Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
178 The first-fit mutex policy allows acquisition of the mutex to occur in any
179 order. This policy is similar in operation to os_unfair_lock, new contending
180 acquirers may obtain ownership of the mutex ahead of existing waiters.
181 .It Dv PTHREAD_MUTEX_POLICY_FAIRSHARE_NP
182 The fairshare mutex policy guarantees that ownership of a contended mutex will
183 be granted to waiters on a strictly ordered first-in, first-out basis. That is,
184 a mutex holder that unlocks the mutex and then attempts to relock will wait
185 behind existing threads already waiting on the mutex before being granted
190 .Fn pthread_mutexattr_getpolicy_np
191 function copies the mutex
193 value of the attribute to the location pointed to by the second parameter.
196 .Fn pthread_mutexattr_set*
197 functions set the attribute that corresponds to each function name.
200 .Fn pthread_mutexattr_get*
201 functions copy the value of the attribute that corresponds to each function name
202 to the location pointed to by the second function parameter.
204 If successful, these functions return 0.
205 Otherwise, an error number is returned to indicate the error.
207 The following environment variables change the behavior of the pthread mutex
209 .Bl -tag -width "XXX" -offset 2n
210 .It Ev PTHREAD_MUTEX_DEFAULT_POLICY
211 Controls the process-wide policy used when initializing a pthread_mutex_t that
212 has not had a policy set via
213 .Fn pthread_mutexattr_setpolicy_np .
214 The valid values are mapped as:
216 .Bl -tag -width "XXX"
218 .Dv PTHREAD_MUTEX_POLICY_FAIRSHARE_NP
220 .Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
223 .Sh BACKWARDS COMPATIBILITY
224 Prior to macOS 10.14 (iOS and tvOS 12.0, watchOS 5.0) the only available
225 pthread mutex policy mode was
226 .Dv PTHREAD_MUTEX_POLICY_FAIRSHARE_NP .
227 macOS 10.14 (iOS and tvOS 12.0, watchOS 5.0) introduces
228 .Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
229 and also makes this the default mode for mutexes initialized without a policy
233 .Fn pthread_mutexattr_setpolicy_np
234 to set the policy of a pthread_mutex_t to
235 .Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
236 on earlier releases will fail with
238 and the mutex will continue to operate in fairshare mode.
241 .Fn pthread_mutexattr_init
242 function shall fail if:
249 .Fn pthread_mutexattr_destroy
250 function will fail if:
258 .Fn pthread_mutexattr_setprioceiling
259 function will fail if:
269 .Fn pthread_mutexattr_getprioceiling
270 function will fail if:
278 .Fn pthread_mutexattr_setprotocol
279 function will fail if:
289 .Fn pthread_mutexattr_getprotocol
290 function will fail if:
298 .Fn pthread_mutexattr_settype
299 function shall fail if:
302 The value specified either by
310 .Fn pthread_mutexattr_gettype
311 function will fail if:
319 .Fn pthread_mutexattr_setpolicy_np
320 function will fail if:
328 .Fn pthread_mutexattr_getpolicy_np
329 function will fail if:
332 The value specified either by
339 .Xr pthread_mutex_init 3
342 .Fn pthread_mutexattr_init
344 .Fn pthread_mutexattr_destroy
349 .Fn pthread_mutexattr_setprioceiling ,
350 .Fn pthread_mutexattr_getprioceiling ,
351 .Fn pthread_mutexattr_setprotocol ,
352 .Fn pthread_mutexattr_getprotocol ,
353 .Fn pthread_mutexattr_settype ,
355 .Fn pthread_mutexattr_gettype