1.\" $OpenBSD: pthread_rwlock_rdlock.3,v 1.8 2007/05/31 19:19:37 jmc Exp $
2.\" Copyright (c) 1998 Alex Nash
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.\" $FreeBSD: pthread_rwlock_rdlock.3,v 1.2 1999/08/28 00:03:09 peter Exp $
27.\"
28.Dd $Mdocdate: May 31 2007 $
29.Dt PTHREAD_RWLOCK_RDLOCK 3
30.Os
31.Sh NAME
32.Nm pthread_rwlock_rdlock ,
33.Nm pthread_rwlock_tryrdlock
34.Nd acquire a read/write lock for reading
35.Sh SYNOPSIS
36.Fd #include <pthread.h>
37.Ft int
38.Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
39.Ft int
40.Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
41.Sh DESCRIPTION
42The
43.Fn pthread_rwlock_rdlock
44function acquires a read lock on
45.Fa lock
46provided that
47.Fa lock
48is not presently held for writing and no writer threads are
49presently blocked on the lock.
50If the read lock cannot be immediately acquired,
51the calling thread blocks until it can acquire the lock.
52.Pp
53The
54.Fn pthread_rwlock_tryrdlock
55function performs the same action, but does not block if the lock
56cannot be immediately obtained (i.e., the lock is held for writing
57or there are waiting writers).
58.Pp
59A thread may hold multiple concurrent read locks.
60If so,
61.Fn pthread_rwlock_unlock
62must be called once for each lock obtained.
63.Pp
64The results of acquiring a read lock while the calling thread holds
65a write lock are undefined.
66.Sh IMPLEMENTATION NOTES
67To prevent writer starvation, writers are favored over readers.
68.Sh RETURN VALUES
69If successful, the
70.Fn pthread_rwlock_rdlock
71and
72.Fn pthread_rwlock_tryrdlock
73functions will return zero.
74Otherwise an error number will be returned to indicate the error.
75.Sh ERRORS
76The
77.Fn pthread_rwlock_tryrdlock
78function will fail if:
79.Bl -tag -width Er
80.It Bq Er EBUSY
81The lock could not be acquired because a writer holds the lock or
82was blocked on it.
83.El
84.Pp
85The
86.Fn pthread_rwlock_rdlock
87and
88.Fn pthread_rwlock_tryrdlock
89functions may fail if:
90.Bl -tag -width Er
91.It Bq Er EAGAIN
92The lock could not be acquired because the maximum number of read locks
93against
94.Fa lock
95has been exceeded.
96.It Bq Er EDEADLK
97The current thread already owns
98.Fa lock
99for writing.
100.It Bq Er EINVAL
101The value specified by
102.Fa lock
103is invalid.
104.It Bq Er ENOMEM
105Insufficient memory exists to initialize the lock (applies to
106statically initialized locks only).
107.El
108.Sh SEE ALSO
109.Xr pthread_rwlock_init 3 ,
110.Xr pthread_rwlock_trywrlock 3 ,
111.Xr pthread_rwlock_unlock 3 ,
112.Xr pthread_rwlock_wrlock 3
113.Sh STANDARDS
114The
115.Fn pthread_rwlock_rdlock
116and
117.Fn pthread_rwlock_tryrdlock
118functions are expected to conform to
119.St -susv2 .
120.Sh HISTORY
121The
122.Fn pthread_rwlock_rdlock
123function first appeared in
124.Fx 3.0
125and
126.Ox 2.5 .
127