xref: /openbsd-src/lib/libpthread/man/pthread_mutex_lock.3 (revision 86f9d4cdda0e0267dccd8755ac0035525579c4a7)
1*86f9d4cdStedu.\" $OpenBSD: pthread_mutex_lock.3,v 1.11 2013/06/05 03:44:50 tedu Exp $
282d2d131Sfgsch.\"
3a278733dSd.\" Copyright (c) 1997 Brian Cully <shmit@kublai.com>
4a278733dSd.\" All rights reserved.
5a278733dSd.\"
6a278733dSd.\" Redistribution and use in source and binary forms, with or without
7a278733dSd.\" modification, are permitted provided that the following conditions
8a278733dSd.\" are met:
9a278733dSd.\" 1. Redistributions of source code must retain the above copyright
10a278733dSd.\"    notice, this list of conditions and the following disclaimer.
11a278733dSd.\" 2. Redistributions in binary form must reproduce the above copyright
12a278733dSd.\"    notice, this list of conditions and the following disclaimer in the
13a278733dSd.\"    documentation and/or other materials provided with the distribution.
14a278733dSd.\" 3. Neither the name of the author nor the names of any co-contributors
15a278733dSd.\"    may be used to endorse or promote products derived from this software
16a278733dSd.\"    without specific prior written permission.
17a278733dSd.\"
18a278733dSd.\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
19a278733dSd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a278733dSd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a278733dSd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22a278733dSd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a278733dSd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a278733dSd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a278733dSd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a278733dSd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a278733dSd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a278733dSd.\" SUCH DAMAGE.
29a278733dSd.\"
3074493c91Sd.\" $FreeBSD: pthread_mutex_lock.3,v 1.5 1999/08/28 00:03:07 peter Exp $
3174493c91Sd.\"
32*86f9d4cdStedu.Dd $Mdocdate: June 5 2013 $
33a278733dSd.Dt PTHREAD_MUTEX_LOCK 3
34c043c0f7Sd.Os
35a278733dSd.Sh NAME
3619706a26Sguenther.Nm pthread_mutex_lock ,
3719706a26Sguenther.Nm pthread_mutex_timedlock ,
3819706a26Sguenther.Nm pthread_mutex_trylock
39a278733dSd.Nd lock a mutex
40a278733dSd.Sh SYNOPSIS
41*86f9d4cdStedu.In pthread.h
42a278733dSd.Ft int
43a278733dSd.Fn pthread_mutex_lock "pthread_mutex_t *mutex"
4419706a26Sguenther.Ft int
4519706a26Sguenther.Fn pthread_mutex_timedlock "pthread_mutex_t *mutex" "const struct timespec *abstime"
4619706a26Sguenther.Ft int
4719706a26Sguenther.Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
48a278733dSd.Sh DESCRIPTION
49a278733dSdThe
50a278733dSd.Fn pthread_mutex_lock
51a278733dSdfunction locks
52a278733dSd.Fa mutex .
5319706a26SguentherIf the mutex is currently locked by another thread,
5419706a26Sguentherthe calling thread will block until the
55a278733dSdmutex becomes available.
5619706a26Sguenther.Pp
5719706a26SguentherIf the mutex is currently locked by the calling thread,
5819706a26Sguentherthen the behavior depends on the type of the mutex.
5919706a26SguentherIf
6019706a26Sguenther.Fa mutex
6119706a26Sguentheris of type
6219706a26Sguenther.Dv PTHREAD_MUTEX_NORMAL ,
6319706a26Sguentherthen the calling thread will deadlock and never return from
6419706a26Sguenther.Fn pthread_mutex_lock .
6519706a26SguentherIf
6619706a26Sguenther.Fa mutex
6719706a26Sguentheris of type
6819706a26Sguenther.Dv PTHREAD_MUTEX_ERRORCHECK ,
6919706a26Sguentherthen
7019706a26Sguenther.Er EDEADLK
7119706a26Sguentheris immediately returned.
7219706a26SguentherIf
7319706a26Sguenther.Fa mutex
7419706a26Sguentheris of type
7519706a26Sguenther.Dv PTHREAD_MUTEX_RECURSIVE ,
7619706a26Sguentherthen the recursion count on the mutex is incremented.
7719706a26Sguenther.Pp
7819706a26SguentherThe
7919706a26Sguenther.Fn pthread_mutex_timedlock
8019706a26Sguentherfunction locks
8119706a26Sguenther.Fa mutex
8219706a26Sguentherlike
8319706a26Sguenther.Fn pthread_mutex_lock
8419706a26Sguentherexcept that it will not block or deadlock past the system time
8519706a26Sguentherspecified in
8619706a26Sguenther.Fa abstime .
8719706a26SguentherIf that time is reached without being able to lock
8819706a26Sguenther.Fa mutex ,
8919706a26Sguentherthen it returns
9019706a26Sguenther.Er ETIMEDOUT .
9119706a26Sguenther.Pp
9219706a26SguentherThe
9319706a26Sguenther.Fn pthread_mutex_trylock
9419706a26Sguentherfunction locks
9519706a26Sguenther.Fa mutex
9619706a26Sguentherlike
9719706a26Sguenther.Fn pthread_mutex_lock
9819706a26Sguentherexcept that if
9919706a26Sguenther.Fa mutex
10019706a26Sguentheris locked by another thread,
10119706a26Sguentheror is locked by the calling thread and is not of type
10219706a26Sguenther.Dv PTHREAD_MUTEX_RECURSIVE ,
10319706a26Sguentherthen it will immediately return
10419706a26Sguenther.Er EBUSY .
105a278733dSd.Sh RETURN VALUES
106a278733dSdIf successful,
10719706a26Sguenther.Fn pthread_mutex_lock ,
10819706a26Sguenther.Fn pthread_mutex_timedlock ,
10919706a26Sguentherand
11019706a26Sguenther.Fn pthread_mutex_trylock
111a278733dSdwill return zero, otherwise an error number will be returned to
112a278733dSdindicate the error.
113a278733dSd.Sh ERRORS
11419706a26Sguenther.Fn pthread_mutex_lock ,
11519706a26Sguenther.Fn pthread_mutex_timedlock ,
11619706a26Sguentherand
11719706a26Sguenther.Fn pthread_mutex_trylock
118a278733dSdwill fail if:
119a278733dSd.Bl -tag -width Er
120a278733dSd.It Bq Er EINVAL
121a278733dSdThe value specified by
122a278733dSd.Fa mutex
123a278733dSdis invalid.
12419706a26Sguenther.It Bq Er EAGAIN
12519706a26SguentherThe mutex is of type
12619706a26Sguenther.Dv PTHREAD_MUTEX_RECURSIVE
12719706a26Sguentherand the maximum recursion count has been reached.
12819706a26Sguenther.El
12919706a26Sguenther.Pp
13019706a26SguentherIn addition,
13119706a26Sguenther.Fn pthread_mutex_lock
13219706a26Sguentherand
13319706a26Sguenther.Fn pthread_mutex_timedlock
13419706a26Sguenthermay return the following error:
13519706a26Sguenther.Bl -tag -width Er
136a278733dSd.It Bq Er EDEADLK
13719706a26SguentherThe mutex is of type
13819706a26Sguenther.Dv PTHREAD_MUTEX_ERRORCHECK
13919706a26Sguentherand is already locked by the calling thread.
14019706a26Sguenther.El
14119706a26Sguenther.Pp
14219706a26Sguenther.Fn pthread_mutex_timedlock
14319706a26Sguenthermay return the following error:
14419706a26Sguenther.Bl -tag -width Er
14519706a26Sguenther.It Bq Er ETIMEDOUT
14619706a26SguentherThe mutex could not be locked and the specified time was reached.
14719706a26Sguenther.El
14819706a26Sguenther.Pp
14919706a26Sguenther.Fn pthread_mutex_trylock
15019706a26Sguenthermay return the following error:
15119706a26Sguenther.Bl -tag -width Er
15219706a26Sguenther.It Bq Er EBUSY
15319706a26SguentherThe mutex could not be locked because it was already locked.
154a278733dSd.El
155a278733dSd.Sh SEE ALSO
156f8344ebdSd.Xr pthread_mutex_destroy 3 ,
157a278733dSd.Xr pthread_mutex_init 3 ,
15819706a26Sguenther.Xr pthread_mutex_unlock 3 ,
15919706a26Sguenther.Xr pthread_mutexattr_settype 3
160a278733dSd.Sh STANDARDS
16119706a26Sguenther.Fn pthread_mutex_lock ,
16219706a26Sguenther.Fn pthread_mutex_timedlock ,
16319706a26Sguentherand
16419706a26Sguenther.Fn pthread_mutex_trylock
16519706a26Sguentherconform to
16619706a26Sguenther.St -p1003.1-2008 .
167