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