nightly: remove unused BINARCHIVE
[unleashed.git] / share / man / man3c / pthread_mutex_lock.3c
blob94a00574b50e251bdf55265a49fe5aa7ca47f9d7
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1991, 1992, 1994, The X/Open Company Ltd.
44 .\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
45 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
46 .\"
47 .TH PTHREAD_MUTEX_LOCK 3C "Nov 11, 2008"
48 .SH NAME
49 pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock \- lock or
50 unlock a mutex
51 .SH SYNOPSIS
52 .LP
53 .nf
54 cc -mt [ \fIflag\fR... ] \fIfile\fR... -lpthread [ \fIlibrary\fR... ]
55 #include <pthread.h>
57 \fBint\fR \fBpthread_mutex_lock\fR(\fBpthread_mutex_t *\fR\fImutex\fR);
58 .fi
60 .LP
61 .nf
62 \fBint\fR \fBpthread_mutex_trylock\fR(\fBpthread_mutex_t *\fR\fImutex\fR);
63 .fi
65 .LP
66 .nf
67 \fBint\fR \fBpthread_mutex_unlock\fR(\fBpthread_mutex_t *\fR\fImutex\fR);
68 .fi
70 .SH DESCRIPTION
71 .sp
72 .LP
73 The mutex object referenced by mutex is locked by calling
74 \fBpthread_mutex_lock()\fR. If the mutex is already locked, the calling thread
75 blocks until the mutex becomes available. This operation returns with the mutex
76 object referenced by mutex in the locked state with the calling thread as its
77 owner.
78 .sp
79 .LP
80 If the mutex type is  \fBPTHREAD_MUTEX_NORMAL\fR, deadlock detection is not
81 provided. Attempting to relock the mutex causes deadlock. If a thread attempts
82 to unlock a mutex that it has not locked or a mutex that is unlocked, undefined
83 behavior results.
84 .sp
85 .LP
86 If the mutex type is  \fBPTHREAD_MUTEX_ERRORCHECK\fR, then error checking is
87 provided. If a thread attempts to relock a mutex that it has already locked, an
88 error will be returned. If a thread attempts to unlock a mutex that it has not
89 locked or a mutex which is unlocked, an error will be returned.
90 .sp
91 .LP
92 If the mutex type is  \fBPTHREAD_MUTEX_RECURSIVE\fR, then the mutex  maintains
93 the concept of a lock count. When a thread successfully acquires a mutex for
94 the first time, the lock count is set to 1. Every time a thread relocks this
95 mutex, the lock count is  incremented by one. Each time the thread unlocks the
96 mutex, the lock count is decremented by one. When the lock count reaches
97 \fB0\fR, the mutex becomes available for other threads to acquire. If a thread
98 attempts to unlock a mutex that it has not locked or a mutex that is unlocked,
99 an error will be returned.
102 If the mutex type is  \fBPTHREAD_MUTEX_DEFAULT\fR, attempting to recursively
103 lock the mutex results in undefined behavior. Attempting to  unlock the mutex
104 if it was not locked by the calling thread results in undefined behavior.
105 Attempting to unlock the mutex if it is not locked results in undefined
106 behavior.
109 The \fBpthread_mutex_trylock()\fR function is identical to
110 \fBpthread_mutex_lock()\fR except that if the mutex object referenced by
111 \fImutex\fR is currently locked (by any thread, including the current thread),
112 the call fails immediately with \fBEBUSY\fR.
115 The \fBpthread_mutex_unlock()\fR function releases the mutex object referenced
116 by \fImutex\fR. The manner in which a mutex is released is dependent upon the
117 mutex's type attribute. If there are threads blocked on the mutex object
118 referenced by \fImutex\fR when \fBpthread_mutex_unlock()\fR is called,
119 resulting in the mutex  becoming available, the scheduling policy is used to
120 determine  which thread will acquire the mutex. (In the case of
121 \fBPTHREAD_MUTEX_RECURSIVE\fR mutexes, the mutex becomes available when the
122 count reaches  \fB0\fR and the calling thread no longer has any locks on this
123 mutex.)
126 If a signal is delivered to a thread waiting for a mutex, upon return from the
127 signal handler the thread resumes waiting for the mutex as if it was not
128 interrupted.
129 .SH RETURN VALUES
132 If successful, the  \fBpthread_mutex_lock()\fR and
133 \fBpthread_mutex_unlock()\fR functions return  \fB0\fR. Otherwise, an error
134 number is returned to indicate the error.
137 The  \fBpthread_mutex_trylock()\fR function returns  \fB0\fR if a lock on the
138 mutex object referenced by \fImutex\fR is acquired. Otherwise, an error number
139 is returned to indicate the error.
140 .SH ERRORS
143 The  \fBpthread_mutex_lock()\fR and \fBpthread_mutex_trylock()\fR functions
144 will fail if:
146 .ne 2
148 \fB\fBEAGAIN\fR\fR
150 .RS 10n
151 The mutex could not be acquired because the maximum number of recursive locks
152 for mutex has been exceeded.
156 .ne 2
158 \fB\fBEINVAL\fR\fR
160 .RS 10n
161 The \fImutex\fR was created with the protocol attribute having the value
162 \fBPTHREAD_PRIO_PROTECT\fR and the calling thread's priority is higher than the
163 mutex's current priority ceiling.
167 .ne 2
169 \fB\fBEPERM\fR\fR
171 .RS 10n
172 The mutex was created with the protocol attribute having the value
173 \fBPTHREAD_PRIO_PROTECT\fR and the calling thread is not in the real-time class
174 (\fBSCHED_RR\fR or \fBSCHED_FIFO\fR scheduling class).
179 The  \fBpthread_mutex_trylock()\fR function will fail if:
181 .ne 2
183 \fB\fBEBUSY\fR\fR
185 .RS 9n
186 The \fImutex\fR could not be acquired because it was already locked.
191 The  \fBpthread_mutex_lock()\fR, \fBpthread_mutex_trylock()\fR and
192 \fBpthread_mutex_unlock()\fR functions may fail if:
194 .ne 2
196 \fB\fBEINVAL\fR\fR
198 .RS 10n
199 The value specified by \fImutex\fR does not refer to an initialized mutex
200 object.
205 The  \fBpthread_mutex_lock()\fR function may fail if:
207 .ne 2
209 \fB\fBEDEADLK\fR\fR
211 .RS 11n
212 The current thread already owns the mutex.
216 .ne 2
218 \fB\fBENOMEM\fR\fR
220 .RS 11n
221 The limit on the number of simultaneously held mutexes has been exceeded.
226 The  \fBpthread_mutex_unlock()\fR function will fail if:
228 .ne 2
230 \fB\fBEPERM\fR\fR
232 .RS 9n
233 The mutex type is PTHREAD_MUTEX_ERRORCHECK or the mutex is a robust mutex, and
234 the current thread does not own the mutex.
239 When a thread makes a call to \fBpthread_mutex_lock()\fR or
240 \fBpthread_mutex_trylock()\fR, if the mutex is initialized with the robustness
241 attribute having the value \fBPTHREAD_MUTEX_ROBUST\fR (see
242 \fBpthread_mutexattr_getrobust\fR(3C)), the call will return these error values
245 .ne 2
247 \fB\fBEOWNERDEAD\fR\fR
249 .RS 19n
250 The last owner of this mutex died while holding the mutex, or the process
251 containing the owner of the mutex unmapped the memory containing the mutex or
252 performed one of the \fBexec\fR(2) functions. This mutex is now owned by the
253 caller. The caller must now attempt to make the state protected by the mutex
254 consistent. If it is able to clean up the state, then it should call
255 \fBpthread_mutex_consistent()\fR for the mutex and unlock the mutex. Subsequent
256 calls to \fBpthread_mutex_lock()\fR and \fBpthread_mutex_trylock()\fR will
257 behave normally, as before. If the caller is not able to clean up the state,
258 \fBpthread_mutex_consistent()\fR should not be called for the mutex, but the
259 mutex should be unlocked. Subsequent calls to \fBpthread_mutex_lock()\fR and
260 \fBpthread_mutex_trylock()\fR will fail to acquire the mutex with the error
261 value \fBENOTRECOVERABLE\fR. If the owner who acquired the lock with
262 \fBEOWNERDEAD\fR dies, the next owner will acquire the lock with
263 \fBEOWNERDEAD\fR.
267 .ne 2
269 \fB\fBENOTRECOVERABLE\fR\fR
271 .RS 19n
272 The mutex trying to be acquired was protecting the state that has been left
273 irrecoverable by the mutex's last owner. The mutex has not been acquired. This
274 condition can occur when the lock was previously acquired with
275 \fBEOWNERDEAD\fR, and the owner was not able to clean up the state and unlocked
276 the mutex without calling \fBpthread_mutex_consistent()\fR.
279 .SH ATTRIBUTES
282 See \fBattributes\fR(5) for descriptions of the following attributes:
287 box;
288 c | c
289 l | l .
290 ATTRIBUTE TYPE  ATTRIBUTE VALUE
292 Interface Stability     Committed
294 MT-Level        MT-Safe
296 Standard        See \fBstandards\fR(5).
299 .SH SEE ALSO
302 \fBpthread_mutex_consistent\fR(3C), \fBpthread_mutex_init\fR(3C),
303 \fBpthread_mutexattr_setprotocol\fR(3C), \fBpthread_mutexattr_setrobust\fR(3C),
304 \fBpthread_mutexattr_settype\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)
305 .SH NOTES
308 In the current implementation of threads, \fBpthread_mutex_lock()\fR,
309 \fBpthread_mutex_unlock()\fR, \fBmutex_lock()\fR, \fBmutex_unlock()\fR,
310 \fBpthread_mutex_trylock()\fR, and \fBmutex_trylock()\fR do not validate the
311 mutex type.  Therefore, an uninitialized mutex or a mutex with an invalid type
312 does not return  \fBEINVAL\fR. Interfaces for mutexes with an invalid type have
313 unspecified behavior.
316 Uninitialized mutexes that are allocated locally may contain junk data. Such
317 mutexes need to be initialized using  \fBpthread_mutex_init()\fR or
318 \fBmutex_init()\fR.