xref: /netbsd-src/lib/libpthread/pthread_rwlock.3 (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1.\" $NetBSD: pthread_rwlock.3,v 1.5 2012/11/12 23:34:50 uwe Exp $
2.\"
3.\" Copyright (c) 2002, 2010 The NetBSD Foundation, Inc.
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.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25.\" POSSIBILITY OF SUCH DAMAGE.
26.\"
27.\" Copyright (c) 1998 Alex Nash
28.\" All rights reserved.
29.\"
30.\" Redistribution and use in source and binary forms, with or without
31.\" modification, are permitted provided that the following conditions
32.\" are met:
33.\" 1. Redistributions of source code must retain the above copyright
34.\"    notice, this list of conditions and the following disclaimer.
35.\" 2. Redistributions in binary form must reproduce the above copyright
36.\"    notice, this list of conditions and the following disclaimer in the
37.\"    documentation and/or other materials provided with the distribution.
38.\"
39.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
40.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
43.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49.\" SUCH DAMAGE.
50.\"
51.\" ----------------------------------------------------------------------------
52.Dd July 8, 2010
53.Dt PTHREAD_RWLOCK 3
54.Os
55.Sh NAME
56.Nm pthread_rwlock
57.Nd read/write lock interface
58.Sh LIBRARY
59.Lb libpthread
60.\" ----------------------------------------------------------------------------
61.Sh SYNOPSIS
62.In pthread.h
63.Ft int
64.Fn pthread_rwlock_init "pthread_rwlock_t * restrict lock" \
65"const pthread_rwlockattr_t * restrict attr"
66.Vt pthread_rwlock_t lock No = Dv PTHREAD_RWLOCK_INITIALIZER;
67.Ft int
68.Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
69.Ft int
70.Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
71.Ft int
72.Fn pthread_rwlock_timedrdlock "pthread_rwlock_t * restrict lock" \
73"const struct timespec * restrict abstime"
74.Ft int
75.Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
76.Ft int
77.Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
78.Ft int
79.Fn pthread_rwlock_timedwrlock "pthread_rwlock_t * restrict lock" \
80"const struct timespec * restrict abstime"
81.Ft int
82.Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
83.Ft int
84.Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
85.\" ----------------------------------------------------------------------------
86.Sh DESCRIPTION
87The
88.Fn pthread_rwlock_init
89function is used to initialize a read/write lock, with attributes
90specified by
91.Fa attr .
92If
93.Fa attr
94is
95.Dv NULL ,
96the default read/write lock attributes are used.
97.Pp
98The results of calling
99.Fn pthread_rwlock_init
100with an already initialized lock are undefined.
101.Pp
102The macro
103.Dv PTHREAD_RWLOCK_INITIALIZER
104can be used to initialize a read/write lock when the allocation can be done
105statically, no error checking is required, and the default attributes are
106appropriate.
107The behavior is similar to calling
108.Fn pthread_rwlock_init
109with
110.Fa attr
111specified as
112.Dv NULL .
113.Pp
114.\" -----
115The
116.Fn pthread_rwlock_destroy
117function is used to destroy a read/write lock previously created with
118.Fn pthread_rwlock_init .
119.Pp
120.\" -----
121The
122.Fn pthread_rwlock_rdlock
123function acquires a read lock on
124.Fa lock
125provided that
126.Fa lock
127is not presently held for writing and no writer threads are
128presently blocked on the lock.
129If the read lock cannot be immediately acquired, the calling thread
130blocks until it can acquire the lock.
131.Pp
132The
133.Fn pthread_rwlock_timedrdlock
134performs the same action, but will not wait beyond
135.Fa abstime
136to obtain the lock before returning.
137.Pp
138The
139.Fn pthread_rwlock_tryrdlock
140function performs the same action as
141.Fn pthread_rwlock_rdlock ,
142but does not block if the lock cannot be immediately obtained (i.e.,
143the lock is held for writing or there are waiting writers).
144.Pp
145A thread may hold multiple concurrent read locks.
146If so,
147.Fn pthread_rwlock_unlock
148must be called once for each lock obtained.
149.Pp
150The results of acquiring a read lock while the calling thread holds
151a write lock are undefined.
152.Pp
153.\" -----
154The
155.Fn pthread_rwlock_wrlock
156function blocks until a write lock can be acquired against
157.Fa lock .
158.Pp
159The
160.Fn pthread_rwlock_timedwrlock
161performs the same action, but will not wait beyond
162.Fa abstime
163to obtain the lock before returning.
164.Pp
165The
166.Fn pthread_rwlock_trywrlock
167function performs the same action as
168.Fn pthread_rwlock_wrlock ,
169but does not block if the lock cannot be immediately obtained.
170.Pp
171The results are undefined if the calling thread already holds the
172lock at the time the call is made.
173.Pp
174.\" -----
175The
176.Fn pthread_rwlock_unlock
177function is used to release the read/write lock previously obtained by
178.Fn pthread_rwlock_rdlock ,
179.Fn pthread_rwlock_wrlock ,
180.Fn pthread_rwlock_tryrdlock ,
181or
182.Fn pthread_rwlock_trywrlock .
183.\" ----------------------------------------------------------------------------
184.Sh RETURN VALUES
185If successful, the
186.Fn pthread_rwlock_init
187function will return zero.
188Otherwise an error number will be returned to indicate the error.
189.Pp
190If successful, the
191.Fn pthread_rwlock_destroy ,
192.Fn pthread_rwlock_rdlock ,
193.Fn pthread_rwlock_timedrdlock ,
194.Fn pthread_rwlock_tryrdlock ,
195.Fn pthread_rwlock_wrlock ,
196.Fn pthread_rwlock_timedwrlock ,
197.Fn pthread_rwlock_trywrlock ,
198and
199.Fn pthread_rwlock_unlock
200will return zero.
201Otherwise an error number will be returned to indicate the error.
202.Pp
203The results are undefined if
204.Fa lock
205is not held by the calling thread.
206.\" ----------------------------------------------------------------------------
207.Sh ERRORS
208The
209.Fn pthread_rwlock_init
210function may fail if:
211.Bl -tag -width Er
212.It Bq Er EAGAIN
213The system lacks the resources to initialize another read-write lock.
214.It Bq Er EBUSY
215The system has detected an attempt to re-initialize the object
216referenced by
217.Fa lock ,
218a previously initialized but not yet destroyed read/write lock.
219.It Bq Er EINVAL
220The value specified by
221.Fa attr
222is invalid.
223.It Bq Er ENOMEM
224Insufficient memory exists to initialize the read-write lock.
225.El
226.Pp
227.\" -----
228The
229.Fn pthread_rwlock_destroy
230function may fail if:
231.Bl -tag -width Er
232.It Bq Er EBUSY
233The system has detected an attempt to destroy the object referenced by
234.Fa lock
235while it is locked.
236.It Bq Er EINVAL
237The value specified by
238.Fa lock
239is invalid.
240.El
241.Pp
242.\" -----
243The
244.Fn pthread_rwlock_tryrdlock
245function shall fail if:
246.Bl -tag -width Er
247.It Bq Er EBUSY
248The lock could not be acquired because a writer holds the lock or
249was blocked on it.
250.El
251.Pp
252The
253.Fn pthread_rwlock_timedrdlock
254function shall fail if:
255.Bl -tag -width Er
256.It Bq Er ETIMEDOUT
257The time specified by
258.Fa abstime
259was reached before the lock could be obtained.
260.El
261.Pp
262The
263.Fn pthread_rwlock_rdlock ,
264.Fn pthread_rwlock_timedrdlock ,
265and
266.Fn pthread_rwlock_tryrdlock
267functions may fail if:
268.Bl -tag -width Er
269.It Bq Er EAGAIN
270The lock could not be acquired because the maximum number of read locks
271against
272.Fa lock
273has been exceeded.
274.It Bq Er EDEADLK
275The current thread already owns
276.Fa lock
277for writing.
278.It Bq Er EINVAL
279The value specified by
280.Fa lock
281is invalid.
282.El
283.Pp
284.\" -----
285The
286.Fn pthread_rwlock_trywrlock
287function shall fail if:
288.Bl -tag -width Er
289.It Bq Er EBUSY
290The calling thread is not able to acquire the lock without blocking.
291.El
292.Pp
293The
294.Fn pthread_rwlock_timedrdlock
295function shall fail if:
296.Bl -tag -width Er
297.It Bq Er ETIMEDOUT
298The time specified by
299.Fa abstime
300was reached before the lock could be obtained.
301.El
302.Pp
303The
304.Fn pthread_rwlock_wrlock ,
305.Fn pthread_rwlock_timedwrlock ,
306and
307.Fn pthread_rwlock_trywrlock
308functions may fail if:
309.Bl -tag -width Er
310.It Bq Er EDEADLK
311The calling thread already owns the read/write lock (for reading
312or writing).
313.It Bq Er EINVAL
314The value specified by
315.Fa lock
316is invalid.
317.El
318.Pp
319.\" -----
320The
321.Fn pthread_rwlock_unlock
322function may fail if:
323.Bl -tag -width Er
324.It Bq Er EINVAL
325The value specified by
326.Fa lock
327is invalid.
328.It Bq Er EPERM
329The current thread does not own the read/write lock.
330.El
331.\" ----------------------------------------------------------------------------
332.Sh SEE ALSO
333.Xr pthread 3 ,
334.Xr pthread_barrier 3 ,
335.Xr pthread_cond 3 ,
336.Xr pthread_mutex 3 ,
337.Xr pthread_rwlockattr 3 ,
338.Xr pthread_spin 3
339.Sh STANDARDS
340These functions conform to
341.St -p1003.1-2001 .
342.\" ----------------------------------------------------------------------------
343.Sh BUGS
344The
345.Dv PTHREAD_PROCESS_SHARED
346attribute is not supported.
347