xref: /netbsd-src/lib/libpthread/pthread_mutex.3 (revision ba5c90c4a4176a50df59b4e9378d56a7a18e3c49)
1.\" $NetBSD: pthread_mutex.3,v 1.11 2019/12/27 09:45:26 msaitoh Exp $
2.\"
3.\" Copyright (c) 2002, 2010 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25.\" POSSIBILITY OF SUCH DAMAGE.
26.\"
27.\" Copyright (c) 1997 Brian Cully <shmit@kublai.com>
28.\" All rights reserved.
29.\"
30.\" Redistribution and use in source and binary forms, with or without
31.\" modification, are permitted provided that the following conditions
32.\" are met:
33.\" 1. Redistributions of source code must retain the above copyright
34.\"    notice, this list of conditions and the following disclaimer.
35.\" 2. Redistributions in binary form must reproduce the above copyright
36.\"    notice, this list of conditions and the following disclaimer in the
37.\"    documentation and/or other materials provided with the distribution.
38.\" 3. Neither the name of the author nor the names of any co-contributors
39.\"    may be used to endorse or promote products derived from this software
40.\"    without specific prior written permission.
41.\"
42.\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
43.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52.\" SUCH DAMAGE.
53.\"
54.\" ----------------------------------------------------------------------------
55.Dd June 12, 2016
56.Dt PTHREAD_MUTEX 3
57.Os
58.Sh NAME
59.Nm pthread_mutex ,
60.Nm pthread_mutex_init ,
61.Nm pthread_mutex_destroy ,
62.Nm pthread_mutex_lock ,
63.Nm pthread_mutex_trylock ,
64.Nm pthread_mutex_unlock ,
65.Nm pthread_mutex_timedlock ,
66.Nm pthread_mutex_getprioceiling ,
67.Nm pthread_mutex_setprioceiling
68.Nd mutual exclusion primitives
69.Sh LIBRARY
70.Lb libpthread
71.\" ----------------------------------------------------------------------------
72.Sh SYNOPSIS
73.In pthread.h
74.Ft int
75.Fn pthread_mutex_init "pthread_mutex_t * restrict mutex" \
76"const pthread_mutexattr_t * restrict attr"
77.Vt pthread_mutex_t mutex No = Dv PTHREAD_MUTEX_INITIALIZER ;
78.Ft int
79.Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
80.Ft int
81.Fn pthread_mutex_lock "pthread_mutex_t *mutex"
82.Ft int
83.Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
84.Ft int
85.Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
86.Ft int
87.Fn pthread_mutex_timedlock "pthread_mutex_t *__restrict mutex" "const struct timespec *__restrict timeout"
88.Ft int
89.Fn pthread_mutex_getprioceiling "const pthread_mutex_t * __restrict mutex" "int * __restrict prioceiling"
90.Ft int
91.Fn pthread_mutex_setprioceiling "pthread_mutex_t * __restrict mutex" \
92"int prioceiling" "int * __restrict old_ceiling"
93.\" ----------------------------------------------------------------------------
94.Sh DESCRIPTION
95The
96.Fn pthread_mutex_init
97function creates a new mutex, with attributes specified with
98.Fa attr .
99If
100.Fa attr
101is
102.Dv NULL ,
103the default attributes are used.
104.Pp
105The macro
106.Dv PTHREAD_MUTEX_INITIALIZER
107can be used to initialize a mutex when the default attributes are
108appropriate and the mutex can be statically allocated.
109The behavior is similar to
110.Fn pthread_mutex_init
111with
112.Fa attr
113specified as
114.Dv NULL ,
115except that no error checking is done.
116.Pp
117.\" -----
118The
119.Fn pthread_mutex_destroy
120function frees the resources allocated for
121.Fa mutex .
122It is possible to reinitialize a destroyed mutex, but undefined
123behavior may follow if the destroyed object is otherwise referenced.
124.Pp
125.\" -----
126The
127.Fn pthread_mutex_lock
128function locks
129.Fa mutex .
130If the mutex is already locked, the calling thread will block until the
131mutex becomes available.
132The error conditions may vary depending on the type of the mutex; see
133.Xr pthread_mutexattr 3
134for additional details.
135.Pp
136The
137.Fn pthread_mutex_trylock
138function locks
139.Fa mutex .
140If the mutex is already locked,
141.Fn pthread_mutex_trylock
142will not block waiting for the mutex, but will return an error condition.
143.Pp
144.\" -----
145The
146.Fn pthread_mutex_unlock
147function unlocks an acquired
148.Fa mutex .
149When operating with the default mutex type,
150undefined behavior follows if a thread tries to unlock a mutex
151that has not been locked by it, or if a thread tries to release
152a mutex that is already unlocked.
153.Pp
154.\" -----
155The
156.Fn pthread_mutex_timedlock
157function shall lock the mutex object referenced by
158.Fa mutex .
159If the mutex is already locked, the calling thread shall block until
160the mutex becomes available in the
161.Fn pthread_mutex_lock
162function.
163If the mutex cannot be locked without waiting for another thread to
164unlock the mutex, this wait shall be terminated when the specified timeout
165expires.
166The timeout shall expire when the absolute time specified by
167.Fa timeout
168passes, as measured by the clock on which timeouts are based.
169.Pp
170.\" -----
171The
172.Fn pthread_mutex_getprioceiling
173function shall return the current priority ceiling of the mutex.
174.Pp
175.\" -----
176The
177.Fn pthread_mutex_setprioceiling
178function shall either lock the mutex if it is unlocked, or block until
179it can successfully lock the mutex, then it shall change the mutex's priority
180ceiling and release the mutex.
181When the change is successful, the previous value of the priority ceiling
182shall be returned
183in
184.Fa old_ceiling .
185The process of locking the mutex need not adhere to the priority
186protect protocol.
187If
188.Fn pthread_mutex_setprioceiling
189function fails, the mutex priority ceiling shall not be changed.
190.\" ----------------------------------------------------------------------------
191.Sh RETURN VALUES
192Upon success all described functions return zero.
193Otherwise, an error number will be returned to indicate the error.
194.Sh ERRORS
195.Fn pthread_mutex_init
196may fail if:
197.Bl -tag -width Er
198.It Bq Er EAGAIN
199The system lacks the resources to initialize another mutex.
200.It Bq Er EINVAL
201The value specified by
202.Fa attr
203is invalid.
204.It Bq Er ENOMEM
205The process cannot allocate enough memory to initialize another mutex.
206.El
207.Pp
208.\" -----
209.Fn pthread_mutex_destroy
210may fail if:
211.Bl -tag -width Er
212.It Bq Er EBUSY
213.Fa Mutex
214is locked by another thread.
215.It Bq Er EINVAL
216The value specified by
217.Fa mutex
218is invalid.
219.El
220.Pp
221.\" -----
222.Fn pthread_mutex_lock
223may fail if:
224.Bl -tag -width Er
225.It Bq Er EDEADLK
226A deadlock would occur if the thread blocked waiting for
227.Fa mutex .
228.It Bq Er EINVAL
229The value specified by
230.Fa mutex
231is invalid.
232.El
233.Pp
234.Fn pthread_mutex_trylock
235may fail if:
236.Bl -tag -width Er
237.It Bq Er EBUSY
238.Fa Mutex
239is already locked.
240.It Bq Er EINVAL
241The value specified by
242.Fa mutex
243is invalid.
244.El
245.Pp
246.\" -----
247.Fn pthread_mutex_unlock
248may fail if:
249.Bl -tag -width Er
250.It Bq Er EINVAL
251The value specified by
252.Fa mutex
253is invalid.
254.It Bq Er EPERM
255The current thread does not hold a lock on
256.Fa mutex .
257.El
258.Pp
259.\" -----
260.Fn pthread_mutex_timedlock
261may fail if:
262.Bl -tag -width Er
263.It Bq Er EINVAL
264The mutex was created with the protocol attribute having the value
265.Dv PTHREAD_PRIO_PROTECT
266and the calling thread's priority is higher than
267the mutex current priority ceiling; or
268the process or thread would have blocked, and the
269.Fa timeout
270parameter specified a nanoseconds field value less than zero or greater
271than or equal to 1000 million.
272.It Bq Er ETIMEDOUT
273The mutex could not be locked before the specified timeout expired.
274.El
275.Pp
276.\" -----
277The
278.Fn pthread_mutex_getprioceiling
279and
280.Fn pthread_mutex_setprioceiling
281functions may fail if:
282.Bl -tag -width Er
283.It Bq Er EINVAL
284The priority requested by
285.Fa prioceiling
286is out of range; or
287the value specified by
288.Fa mutex
289does not refer to a currently existing mutex.
290.It Bq Er EPERM
291The caller does not have the privilege to perform the operation.
292.El
293.\" ----------------------------------------------------------------------------
294.Sh SEE ALSO
295.Xr pthread 3 ,
296.Xr pthread_barrier 3 ,
297.Xr pthread_cond 3 ,
298.Xr pthread_mutexattr 3 ,
299.Xr pthread_rwlock 3 ,
300.Xr pthread_spin 3
301.\" ----------------------------------------------------------------------------
302.Sh STANDARDS
303These functions conform to
304.St -p1003.1-2001 .
305