xref: /netbsd-src/lib/libpthread/pthread_mutex.3 (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1.\" $NetBSD: pthread_mutex.3,v 1.6 2012/11/12 23:11:05 uwe 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 July 8, 2010
56.Dt PTHREAD_MUTEX 3
57.Os
58.Sh NAME
59.Nm pthread_mutex
60.Nd mutual exclusion primitives
61.Sh LIBRARY
62.Lb libpthread
63.\" ----------------------------------------------------------------------------
64.Sh SYNOPSIS
65.In pthread.h
66.Ft int
67.Fn pthread_mutex_init "pthread_mutex_t * restrict mutex" \
68"const pthread_mutexattr_t * restrict attr"
69.Vt pthread_mutex_t mutex No = Dv PTHREAD_MUTEX_INITIALIZER;
70.Ft int
71.Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
72.Ft int
73.Fn pthread_mutex_lock "pthread_mutex_t *mutex"
74.Ft int
75.Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
76.Ft int
77.Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
78.\" ----------------------------------------------------------------------------
79.Sh DESCRIPTION
80The
81.Fn pthread_mutex_init
82function creates a new mutex, with attributes specified with
83.Fa attr .
84If
85.Fa attr
86is
87.Dv NULL ,
88the default attributes are used.
89.Pp
90The macro
91.Dv PTHREAD_MUTEX_INITIALIZER
92can be used to initialize a mutex when the default attributes are
93appropriate and the mutex can be statically allocated.
94The behavior is similar to
95.Fn pthread_mutex_init
96with
97.Fa attr
98specified as
99.Dv NULL ,
100except that no error checking is done.
101.Pp
102.\" -----
103The
104.Fn pthread_mutex_destroy
105function frees the resources allocated for
106.Fa mutex .
107It is possible to reinitialize a destroyed mutex, but undefined
108behavior may follow if the destroyed object is otherwise referenced.
109.Pp
110.\" -----
111The
112.Fn pthread_mutex_lock
113function locks
114.Fa mutex .
115If the mutex is already locked, the calling thread will block until the
116mutex becomes available.
117The error conditions may vary depending on the type of the mutex; see
118.Xr pthread_mutexattr 3
119for additional details.
120.Pp
121The
122.Fn pthread_mutex_trylock
123function locks
124.Fa mutex .
125If the mutex is already locked,
126.Fn pthread_mutex_trylock
127will not block waiting for the mutex, but will return an error condition.
128.Pp
129.\" -----
130The
131.Fn pthread_mutex_unlock
132function unlocks an acquired
133.Fa mutex .
134When operating with the default mutex type,
135undefined behavior follows if a thread tries to unlock a mutex
136that has not been locked by it, or if a thread tries to release
137a mutex that is already unlocked.
138.\" ----------------------------------------------------------------------------
139.Sh RETURN VALUES
140Upon success all described functions return zero.
141Otherwise, an error number will be returned to indicate the error.
142.Sh ERRORS
143.Fn pthread_mutex_init
144may fail if:
145.Bl -tag -width Er
146.It Bq Er EAGAIN
147The system lacks the resources to initialize another mutex.
148.It Bq Er EINVAL
149The value specified by
150.Fa attr
151is invalid.
152.It Bq Er ENOMEM
153The process cannot allocate enough memory to initialize another mutex.
154.El
155.Pp
156.\" -----
157.Fn pthread_mutex_destroy
158may fail if:
159.Bl -tag -width Er
160.It Bq Er EBUSY
161.Fa Mutex
162is locked by another thread.
163.It Bq Er EINVAL
164The value specified by
165.Fa mutex
166is invalid.
167.El
168.Pp
169.\" -----
170.Fn pthread_mutex_lock
171may fail if:
172.Bl -tag -width Er
173.It Bq Er EDEADLK
174A deadlock would occur if the thread blocked waiting for
175.Fa mutex .
176.It Bq Er EINVAL
177The value specified by
178.Fa mutex
179is invalid.
180.El
181.Pp
182.Fn pthread_mutex_trylock
183may fail if:
184.Bl -tag -width Er
185.It Bq Er EBUSY
186.Fa Mutex
187is already locked.
188.It Bq Er EINVAL
189The value specified by
190.Fa mutex
191is invalid.
192.El
193.Pp
194.\" -----
195.Fn pthread_mutex_unlock
196may fail if:
197.Bl -tag -width Er
198.It Bq Er EINVAL
199The value specified by
200.Fa mutex
201is invalid.
202.It Bq Er EPERM
203The current thread does not hold a lock on
204.Fa mutex .
205.El
206.\" ----------------------------------------------------------------------------
207.Sh SEE ALSO
208.Xr pthread 3 ,
209.Xr pthread_barrier 3 ,
210.Xr pthread_cond 3 ,
211.Xr pthread_mutexattr 3 ,
212.Xr pthread_rwlock 3 ,
213.Xr pthread_spin 3
214.\" ----------------------------------------------------------------------------
215.Sh STANDARDS
216These functions conform to
217.St -p1003.1-2001 .
218