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