1.\" $NetBSD: pthread_rwlock.3,v 1.3 2010/07/09 10:55:11 wiz 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.Pp 67.Va pthread_rwlock_t lock = Dv PTHREAD_RWLOCK_INITIALIZER; 68.Ft int 69.Fn pthread_rwlock_destroy "pthread_rwlock_t *lock" 70.Ft int 71.Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock" 72.Ft int 73.Fn pthread_rwlock_timedrdlock "pthread_rwlock_t * restrict lock" \ 74"const struct timespec * restrict abstime" 75.Ft int 76.Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock" 77.Ft int 78.Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock" 79.Ft int 80.Fn pthread_rwlock_timedwrlock "pthread_rwlock_t * restrict lock" \ 81"const struct timespec * restrict abstime" 82.Ft int 83.Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock" 84.Ft int 85.Fn pthread_rwlock_unlock "pthread_rwlock_t *lock" 86.\" ---------------------------------------------------------------------------- 87.Sh DESCRIPTION 88The 89.Fn pthread_rwlock_init 90function is used to initialize a read/write lock, with attributes 91specified by 92.Fa attr . 93If 94.Fa attr 95is 96.Dv NULL , 97the default read/write lock attributes are used. 98.Pp 99The results of calling 100.Fn pthread_rwlock_init 101with an already initialized lock are undefined. 102.Pp 103The macro 104.Dv PTHREAD_RWLOCK_INITIALIZER 105can be used to initialize a read/write lock when the allocation can be done 106statically, no error checking is required, and the default attributes are 107appropriate. 108The behavior is similar to calling 109.Fn pthread_rwlock_init 110with 111.Fa attr 112specified as 113.Dv NULL . 114.Pp 115.\" ----- 116The 117.Fn pthread_rwlock_destroy 118function is used to destroy a read/write lock previously created with 119.Fn pthread_rwlock_init . 120.Pp 121.\" ----- 122The 123.Fn pthread_rwlock_rdlock 124function acquires a read lock on 125.Fa lock 126provided that 127.Fa lock 128is not presently held for writing and no writer threads are 129presently blocked on the lock. 130If the read lock cannot be immediately acquired, the calling thread 131blocks until it can acquire the lock. 132.Pp 133The 134.Fn pthread_rwlock_timedrdlock 135performs the same action, but will not wait beyond 136.Fa abstime 137to obtain the lock before returning. 138.Pp 139The 140.Fn pthread_rwlock_tryrdlock 141function performs the same action as 142.Fn pthread_rwlock_rdlock , 143but does not block if the lock cannot be immediately obtained (i.e., 144the lock is held for writing or there are waiting writers). 145.Pp 146A thread may hold multiple concurrent read locks. 147If so, 148.Fn pthread_rwlock_unlock 149must be called once for each lock obtained. 150.Pp 151The results of acquiring a read lock while the calling thread holds 152a write lock are undefined. 153.Pp 154.\" ----- 155The 156.Fn pthread_rwlock_wrlock 157function blocks until a write lock can be acquired against 158.Fa lock . 159.Pp 160The 161.Fn pthread_rwlock_timedwrlock 162performs the same action, but will not wait beyond 163.Fa abstime 164to obtain the lock before returning. 165.Pp 166The 167.Fn pthread_rwlock_trywrlock 168function performs the same action as 169.Fn pthread_rwlock_wrlock , 170but does not block if the lock cannot be immediately obtained. 171.Pp 172The results are undefined if the calling thread already holds the 173lock at the time the call is made. 174.Pp 175.\" ----- 176The 177.Fn pthread_rwlock_unlock 178function is used to release the read/write lock previously obtained by 179.Fn pthread_rwlock_rdlock , 180.Fn pthread_rwlock_wrlock , 181.Fn pthread_rwlock_tryrdlock , 182or 183.Fn pthread_rwlock_trywrlock . 184.\" ---------------------------------------------------------------------------- 185.Sh RETURN VALUES 186If successful, the 187.Fn pthread_rwlock_init 188function will return zero. 189Otherwise an error number will be returned to indicate the error. 190.Pp 191If successful, the 192.Fn pthread_rwlock_destroy , 193.Fn pthread_rwlock_rdlock , 194.Fn pthread_rwlock_timedrdlock , 195.Fn pthread_rwlock_tryrdlock , 196.Fn pthread_rwlock_wrlock , 197.Fn pthread_rwlock_timedwrlock , 198.Fn pthread_rwlock_trywrlock , 199and 200.Fn pthread_rwlock_unlock 201will return zero. 202Otherwise an error number will be returned to indicate the error. 203.Pp 204The results are undefined if 205.Fa lock 206is not held by the calling thread. 207.\" ---------------------------------------------------------------------------- 208.Sh ERRORS 209The 210.Fn pthread_rwlock_init 211function may fail if: 212.Bl -tag -width Er 213.It Bq Er EAGAIN 214The system lacks the resources to initialize another read-write lock. 215.It Bq Er EBUSY 216The system has detected an attempt to re-initialize the object 217referenced by 218.Fa lock , 219a previously initialized but not yet destroyed read/write lock. 220.It Bq Er EINVAL 221The value specified by 222.Fa attr 223is invalid. 224.It Bq Er ENOMEM 225Insufficient memory exists to initialize the read-write lock. 226.El 227.Pp 228.\" ----- 229The 230.Fn pthread_rwlock_destroy 231function may fail if: 232.Bl -tag -width Er 233.It Bq Er EBUSY 234The system has detected an attempt to destroy the object referenced by 235.Fa lock 236while it is locked. 237.It Bq Er EINVAL 238The value specified by 239.Fa lock 240is invalid. 241.El 242.Pp 243.\" ----- 244The 245.Fn pthread_rwlock_tryrdlock 246function shall fail if: 247.Bl -tag -width Er 248.It Bq Er EBUSY 249The lock could not be acquired because a writer holds the lock or 250was blocked on it. 251.El 252.Pp 253The 254.Fn pthread_rwlock_timedrdlock 255function shall fail if: 256.Bl -tag -width Er 257.It Bq Er ETIMEDOUT 258The time specified by 259.Fa abstime 260was reached before the lock could be obtained. 261.El 262.Pp 263The 264.Fn pthread_rwlock_rdlock , 265.Fn pthread_rwlock_timedrdlock , 266and 267.Fn pthread_rwlock_tryrdlock 268functions may fail if: 269.Bl -tag -width Er 270.It Bq Er EAGAIN 271The lock could not be acquired because the maximum number of read locks 272against 273.Fa lock 274has been exceeded. 275.It Bq Er EDEADLK 276The current thread already owns 277.Fa lock 278for writing. 279.It Bq Er EINVAL 280The value specified by 281.Fa lock 282is invalid. 283.El 284.Pp 285.\" ----- 286The 287.Fn pthread_rwlock_trywrlock 288function shall fail if: 289.Bl -tag -width Er 290.It Bq Er EBUSY 291The calling thread is not able to acquire the lock without blocking. 292.El 293.Pp 294The 295.Fn pthread_rwlock_timedrdlock 296function shall fail if: 297.Bl -tag -width Er 298.It Bq Er ETIMEDOUT 299The time specified by 300.Fa abstime 301was reached before the lock could be obtained. 302.El 303.Pp 304The 305.Fn pthread_rwlock_wrlock , 306.Fn pthread_rwlock_timedwrlock , 307and 308.Fn pthread_rwlock_trywrlock 309functions may fail if: 310.Bl -tag -width Er 311.It Bq Er EDEADLK 312The calling thread already owns the read/write lock (for reading 313or writing). 314.It Bq Er EINVAL 315The value specified by 316.Fa lock 317is invalid. 318.El 319.Pp 320.\" ----- 321The 322.Fn pthread_rwlock_unlock 323function may fail if: 324.Bl -tag -width Er 325.It Bq Er EINVAL 326The value specified by 327.Fa lock 328is invalid. 329.It Bq Er EPERM 330The current thread does not own the read/write lock. 331.El 332.\" ---------------------------------------------------------------------------- 333.Sh SEE ALSO 334.Xr pthread 3 , 335.Xr pthread_barrier 3 , 336.Xr pthread_cond 3 , 337.Xr pthread_mutex 3 , 338.Xr pthread_rwlockattr 3 , 339.Xr pthread_spin 3 340.Sh STANDARDS 341These functions conform to 342.St -p1003.1-2001 . 343.\" ---------------------------------------------------------------------------- 344.Sh BUGS 345The PTHREAD_PROCESS_SHARED attribute is not supported. 346