]> git.saurik.com Git - apple/libpthread.git/blame - man/pthread_mutexattr.3
libpthread-330.230.1.tar.gz
[apple/libpthread.git] / man / pthread_mutexattr.3
CommitLineData
2546420a 1.\" $NetBSD: pthread_mutexattr.3,v 1.11 2010/07/08 22:46:34 jruoho Exp $
f1a1da6c 2.\"
2546420a 3.\" Copyright (c) 2002, 2010 The NetBSD Foundation, Inc.
f1a1da6c
A
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
7.\" are met:
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.
f1a1da6c
A
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.
24.\"
25.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
26.\" All rights reserved.
27.\"
28.\" Redistribution and use in source and binary forms, with or without
29.\" modification, are permitted provided that the following conditions
30.\" are met:
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
38.\" distribution.
39.\"
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.
51.\"
2546420a
A
52.\" $FreeBSD$
53.Dd July 9, 2010
f1a1da6c
A
54.Dt PTHREAD_MUTEXATTR 3
55.Os
56.Sh NAME
f1a1da6c 57.Nm pthread_mutexattr_init ,
2546420a 58.Nm pthread_mutexattr_destroy ,
f1a1da6c 59.Nm pthread_mutexattr_setprioceiling ,
2546420a 60.Nm pthread_mutexattr_getprioceiling ,
f1a1da6c 61.Nm pthread_mutexattr_setprotocol ,
2546420a
A
62.Nm pthread_mutexattr_getprotocol ,
63.Nm pthread_mutexattr_settype ,
64.Nm pthread_mutexattr_gettype
f1a1da6c
A
65.Nd mutex attribute operations
66.Sh SYNOPSIS
2546420a 67.In pthread.h
f1a1da6c 68.Ft int
2546420a 69.Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
f1a1da6c 70.Ft int
2546420a 71.Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
f1a1da6c 72.Ft int
2546420a 73.Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int prioceiling"
f1a1da6c 74.Ft int
2546420a 75.Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *prioceiling"
f1a1da6c 76.Ft int
2546420a 77.Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
f1a1da6c 78.Ft int
2546420a 79.Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
f1a1da6c 80.Ft int
2546420a 81.Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
f1a1da6c 82.Ft int
2546420a 83.Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
214d78a2
A
84.Ft int
85.Fn pthread_mutexattr_setpolicy_np "pthread_mutexattr_t *attr" "int policy"
86.Ft int
87.Fn pthread_mutexattr_getpolicy_np "pthread_mutexattr_t *attr" "int *policy"
f1a1da6c
A
88.Sh DESCRIPTION
89Mutex attributes are used to specify parameters to
90.Fn pthread_mutex_init .
2546420a
A
91Like with thread attributes,
92one attribute object can be used in multiple calls to
93.Xr pthread_mutex_init 3 ,
f1a1da6c
A
94with or without modifications between calls.
95.Pp
96The
97.Fn pthread_mutexattr_init
98function initializes
99.Fa attr
2546420a 100with all the default mutex attributes.
f1a1da6c
A
101.Pp
102The
103.Fn pthread_mutexattr_destroy
104function destroys
105.Fa attr .
106.Pp
107The
108.Fn pthread_mutexattr_settype
2546420a
A
109functions set the mutex
110.Fa type
111value of the attribute.
112Valid mutex types are:
113.Bl -tag -width "XXX" -offset 2n
114.It Dv PTHREAD_MUTEX_NORMAL
115This type of mutex does not check for usage errors.
116It will deadlock if reentered, and result in undefined behavior if a
117locked mutex is unlocked by another thread.
118Attempts to unlock an already unlocked
f1a1da6c
A
119.Dv PTHREAD_MUTEX_NORMAL
120mutex will result in undefined behavior.
2546420a
A
121.It Dv PTHREAD_MUTEX_ERRORCHECK
122These mutexes do check for usage errors.
f1a1da6c
A
123If an attempt is made to relock a
124.Dv PTHREAD_MUTEX_ERRORCHECK
125mutex without first dropping the lock, an error will be returned.
126If a thread attempts to unlock a
127.Dv PTHREAD_MUTEX_ERRORCHECK
2546420a
A
128mutex that is locked by another thread, an error will be returned.
129If a thread attempts to unlock a
f1a1da6c
A
130.Dv PTHREAD_MUTEX_ERRORCHECK
131thread that is unlocked, an error will be returned.
2546420a
A
132.It Dv PTHREAD_MUTEX_RECURSIVE
133These mutexes allow recursive locking.
f1a1da6c
A
134An attempt to relock a
135.Dv PTHREAD_MUTEX_RECURSIVE
2546420a
A
136mutex that is already locked by the same thread succeeds.
137An equivalent number of
f1a1da6c 138.Xr pthread_mutex_unlock 3
2546420a
A
139calls are needed before the mutex will wake another thread waiting
140on this lock.
141If a thread attempts to unlock a
f1a1da6c 142.Dv PTHREAD_MUTEX_RECURSIVE
2546420a
A
143mutex that is locked by another thread, an error will be returned.
144If a thread attempts to unlock a
f1a1da6c
A
145.Dv PTHREAD_MUTEX_RECURSIVE
146thread that is unlocked, an error will be returned.
147.Pp
2546420a
A
148It is advised that
149.Dv PTHREAD_MUTEX_RECURSIVE
150mutexes are not used with condition variables.
151This is because of the implicit unlocking done by
152.Xr pthread_cond_wait 3
153and
154.Xr pthread_cond_timedwait 3 .
155.It Dv PTHREAD_MUTEX_DEFAULT
156Also this type of mutex will cause undefined behavior if reentered.
f1a1da6c
A
157Unlocking a
158.Dv PTHREAD_MUTEX_DEFAULT
2546420a
A
159mutex locked by another thread will result in undefined behavior.
160Attempts to unlock an already unlocked
f1a1da6c
A
161.Dv PTHREAD_MUTEX_DEFAULT
162mutex will result in undefined behavior.
2546420a
A
163This is the default mutex type for
164.Fn pthread_mutexaddr_init .
165.El
f1a1da6c
A
166.Pp
167.Fn pthread_mutexattr_gettype
168functions copy the type value of the attribute to the location pointed to by the second parameter.
169.Pp
170The
214d78a2
A
171.Fn pthread_mutexattr_setpolicy_np
172function sets the mutex
173.Fa policy
174value of the attribute.
175Valid mutex policies are:
176.Bl -tag -width "XXX" -offset 2n
177.It Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
178The first-fit mutex policy allows acquisition of the mutex to occur in any
179order. This policy is similar in operation to os_unfair_lock, new contending
180acquirers may obtain ownership of the mutex ahead of existing waiters.
181.It Dv PTHREAD_MUTEX_POLICY_FAIRSHARE_NP
182The fairshare mutex policy guarantees that ownership of a contended mutex will
183be granted to waiters on a strictly ordered first-in, first-out basis. That is,
184a mutex holder that unlocks the mutex and then attempts to relock will wait
185behind existing threads already waiting on the mutex before being granted
186ownership again.
187.El
188.Pp
189The
190.Fn pthread_mutexattr_getpolicy_np
191function copies the mutex
192.Fa policy
193value of the attribute to the location pointed to by the second parameter.
194.Pp
195The
f1a1da6c
A
196.Fn pthread_mutexattr_set*
197functions set the attribute that corresponds to each function name.
198.Pp
199The
200.Fn pthread_mutexattr_get*
201functions copy the value of the attribute that corresponds to each function name
202to the location pointed to by the second function parameter.
203.Sh RETURN VALUES
204If successful, these functions return 0.
205Otherwise, an error number is returned to indicate the error.
214d78a2
A
206.Sh ENVIRONMENT
207The following environment variables change the behavior of the pthread mutex
208implementation.
209.Bl -tag -width "XXX" -offset 2n
210.It Ev PTHREAD_MUTEX_DEFAULT_POLICY
211Controls the process-wide policy used when initializing a pthread_mutex_t that
212has not had a policy set via
213.Fn pthread_mutexattr_setpolicy_np .
214The valid values are mapped as:
215.Pp
216.Bl -tag -width "XXX"
217.It Fa 1
218.Dv PTHREAD_MUTEX_POLICY_FAIRSHARE_NP
219.It Fa 3
220.Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
221.El
222.El
223.Sh BACKWARDS COMPATIBILITY
224Prior to macOS 10.14 (iOS and tvOS 12.0, watchOS 5.0) the only available
225pthread mutex policy mode was
226.Dv PTHREAD_MUTEX_POLICY_FAIRSHARE_NP .
227macOS 10.14 (iOS and tvOS 12.0, watchOS 5.0) introduces
228.Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
229and also makes this the default mode for mutexes initialized without a policy
230attribute set.
231.Pp
232Attempting to use
233.Fn pthread_mutexattr_setpolicy_np
234to set the policy of a pthread_mutex_t to
235.Dv PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
236on earlier releases will fail with
237.Er EINVAL
238and the mutex will continue to operate in fairshare mode.
f1a1da6c 239.Sh ERRORS
2546420a 240The
f1a1da6c 241.Fn pthread_mutexattr_init
2546420a 242function shall fail if:
f1a1da6c
A
243.Bl -tag -width Er
244.It Bq Er ENOMEM
245Out of memory.
246.El
247.Pp
2546420a 248The
f1a1da6c 249.Fn pthread_mutexattr_destroy
2546420a 250function will fail if:
f1a1da6c
A
251.Bl -tag -width Er
252.It Bq Er EINVAL
253Invalid value for
254.Fa attr .
255.El
256.Pp
2546420a 257The
f1a1da6c 258.Fn pthread_mutexattr_setprioceiling
2546420a 259function will fail if:
f1a1da6c
A
260.Bl -tag -width Er
261.It Bq Er EINVAL
262Invalid value for
263.Fa attr ,
264or invalid value for
265.Fa prioceiling .
266.El
267.Pp
2546420a 268The
f1a1da6c 269.Fn pthread_mutexattr_getprioceiling
2546420a 270function will fail if:
f1a1da6c
A
271.Bl -tag -width Er
272.It Bq Er EINVAL
273Invalid value for
274.Fa attr .
275.El
276.Pp
2546420a 277The
f1a1da6c 278.Fn pthread_mutexattr_setprotocol
2546420a 279function will fail if:
f1a1da6c
A
280.Bl -tag -width Er
281.It Bq Er EINVAL
282Invalid value for
283.Fa attr ,
284or invalid value for
285.Fa protocol .
286.El
287.Pp
2546420a 288The
f1a1da6c 289.Fn pthread_mutexattr_getprotocol
2546420a 290function will fail if:
f1a1da6c
A
291.Bl -tag -width Er
292.It Bq Er EINVAL
293Invalid value for
294.Fa attr .
295.El
296.Pp
2546420a 297The
f1a1da6c 298.Fn pthread_mutexattr_settype
2546420a 299function shall fail if:
f1a1da6c
A
300.Bl -tag -width Er
301.It Bq Er EINVAL
2546420a
A
302The value specified either by
303.Fa type
304or
305.Fa attr
306is invalid.
f1a1da6c
A
307.El
308.Pp
2546420a 309The
f1a1da6c 310.Fn pthread_mutexattr_gettype
2546420a 311function will fail if:
f1a1da6c
A
312.Bl -tag -width Er
313.It Bq Er EINVAL
314Invalid value for
315.Fa attr .
316.El
214d78a2
A
317.Pp
318The
319.Fn pthread_mutexattr_setpolicy_np
320function will fail if:
321.Bl -tag -width Er
322.It Bq Er EINVAL
323Invalid value for
324.Fa attr .
325.El
326.Pp
327The
328.Fn pthread_mutexattr_getpolicy_np
329function will fail if:
330.Bl -tag -width Er
331.It Bq Er EINVAL
332The value specified either by
333.Fa type
334or
335.Fa attr
336is invalid.
337.El
f1a1da6c
A
338.Sh SEE ALSO
339.Xr pthread_mutex_init 3
340.Sh STANDARDS
2546420a 341The
f1a1da6c
A
342.Fn pthread_mutexattr_init
343and
344.Fn pthread_mutexattr_destroy
2546420a 345functions conform to
f1a1da6c
A
346.St -p1003.1-96
347.Pp
2546420a 348The
f1a1da6c
A
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 ,
354and
355.Fn pthread_mutexattr_gettype
2546420a 356functions conform to
f1a1da6c 357.St -susv2