1.\" $OpenBSD: pthread_mutex_lock.3,v 1.11 2013/06/05 03:44:50 tedu Exp $ 2.\" 3.\" Copyright (c) 1997 Brian Cully <shmit@kublai.com> 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.\" 3. Neither the name of the author nor the names of any co-contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" $FreeBSD: pthread_mutex_lock.3,v 1.5 1999/08/28 00:03:07 peter Exp $ 31.\" 32.Dd $Mdocdate: June 5 2013 $ 33.Dt PTHREAD_MUTEX_LOCK 3 34.Os 35.Sh NAME 36.Nm pthread_mutex_lock , 37.Nm pthread_mutex_timedlock , 38.Nm pthread_mutex_trylock 39.Nd lock a mutex 40.Sh SYNOPSIS 41.In pthread.h 42.Ft int 43.Fn pthread_mutex_lock "pthread_mutex_t *mutex" 44.Ft int 45.Fn pthread_mutex_timedlock "pthread_mutex_t *mutex" "const struct timespec *abstime" 46.Ft int 47.Fn pthread_mutex_trylock "pthread_mutex_t *mutex" 48.Sh DESCRIPTION 49The 50.Fn pthread_mutex_lock 51function locks 52.Fa mutex . 53If the mutex is currently locked by another thread, 54the calling thread will block until the 55mutex becomes available. 56.Pp 57If the mutex is currently locked by the calling thread, 58then the behavior depends on the type of the mutex. 59If 60.Fa mutex 61is of type 62.Dv PTHREAD_MUTEX_NORMAL , 63then the calling thread will deadlock and never return from 64.Fn pthread_mutex_lock . 65If 66.Fa mutex 67is of type 68.Dv PTHREAD_MUTEX_ERRORCHECK , 69then 70.Er EDEADLK 71is immediately returned. 72If 73.Fa mutex 74is of type 75.Dv PTHREAD_MUTEX_RECURSIVE , 76then the recursion count on the mutex is incremented. 77.Pp 78The 79.Fn pthread_mutex_timedlock 80function locks 81.Fa mutex 82like 83.Fn pthread_mutex_lock 84except that it will not block or deadlock past the system time 85specified in 86.Fa abstime . 87If that time is reached without being able to lock 88.Fa mutex , 89then it returns 90.Er ETIMEDOUT . 91.Pp 92The 93.Fn pthread_mutex_trylock 94function locks 95.Fa mutex 96like 97.Fn pthread_mutex_lock 98except that if 99.Fa mutex 100is locked by another thread, 101or is locked by the calling thread and is not of type 102.Dv PTHREAD_MUTEX_RECURSIVE , 103then it will immediately return 104.Er EBUSY . 105.Sh RETURN VALUES 106If successful, 107.Fn pthread_mutex_lock , 108.Fn pthread_mutex_timedlock , 109and 110.Fn pthread_mutex_trylock 111will return zero, otherwise an error number will be returned to 112indicate the error. 113.Sh ERRORS 114.Fn pthread_mutex_lock , 115.Fn pthread_mutex_timedlock , 116and 117.Fn pthread_mutex_trylock 118will fail if: 119.Bl -tag -width Er 120.It Bq Er EINVAL 121The value specified by 122.Fa mutex 123is invalid. 124.It Bq Er EAGAIN 125The mutex is of type 126.Dv PTHREAD_MUTEX_RECURSIVE 127and the maximum recursion count has been reached. 128.El 129.Pp 130In addition, 131.Fn pthread_mutex_lock 132and 133.Fn pthread_mutex_timedlock 134may return the following error: 135.Bl -tag -width Er 136.It Bq Er EDEADLK 137The mutex is of type 138.Dv PTHREAD_MUTEX_ERRORCHECK 139and is already locked by the calling thread. 140.El 141.Pp 142.Fn pthread_mutex_timedlock 143may return the following error: 144.Bl -tag -width Er 145.It Bq Er ETIMEDOUT 146The mutex could not be locked and the specified time was reached. 147.El 148.Pp 149.Fn pthread_mutex_trylock 150may return the following error: 151.Bl -tag -width Er 152.It Bq Er EBUSY 153The mutex could not be locked because it was already locked. 154.El 155.Sh SEE ALSO 156.Xr pthread_mutex_destroy 3 , 157.Xr pthread_mutex_init 3 , 158.Xr pthread_mutex_unlock 3 , 159.Xr pthread_mutexattr_settype 3 160.Sh STANDARDS 161.Fn pthread_mutex_lock , 162.Fn pthread_mutex_timedlock , 163and 164.Fn pthread_mutex_trylock 165conform to 166.St -p1003.1-2008 . 167