remove csh(1)
[unleashed.git] / share / man / man3c / rwlock.3c
blobee9eecf226910fe13008f9779e333b17ceb24644
1 '\" te
2 .\"  Copyright (c) 1998 Sun Microsystems, Inc.  All Rights Reserved
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH RWLOCK 3C "Dec 19, 2013"
7 .SH NAME
8 rwlock, rwlock_init, rwlock_destroy, rw_rdlock, rw_wrlock, rw_tryrdlock,
9 rw_trywrlock, rw_unlock \- multiple readers, single writer locks
10 .SH SYNOPSIS
11 .LP
12 .nf
13 cc -mt [ \fIflag\fR... ] \fIfile\fR...[ \fIlibrary\fR... ]
15 #include <synch.h>
17 \fBint\fR \fBrwlock_init\fR(\fBrwlock_t *\fR\fIrwlp\fR, \fBint\fR \fItype\fR, \fBvoid *\fR\fIarg\fR);
18 .fi
20 .LP
21 .nf
22 \fBint\fR \fBrwlock_destroy\fR(\fBrwlock_t *\fR\fIrwlp\fR);
23 .fi
25 .LP
26 .nf
27 \fBint\fR \fBrw_rdlock\fR(\fBrwlock_t *\fR\fIrwlp\fR);
28 .fi
30 .LP
31 .nf
32 \fBint\fR \fBrw_wrlock\fR(\fBrwlock_t *\fR\fIrwlp\fR);
33 .fi
35 .LP
36 .nf
37 \fBint\fR \fBrw_unlock\fR(\fBrwlock_t *\fR\fIrwlp\fR);
38 .fi
40 .LP
41 .nf
42 \fBint\fR \fBrw_tryrdlock\fR(\fBrwlock_t *\fR\fIrwlp\fR);
43 .fi
45 .LP
46 .nf
47 \fBint\fR \fBrw_trywrlock\fR(\fBrwlock_t *\fR\fIrwlp\fR);
48 .fi
50 .SH DESCRIPTION
51 .sp
52 .LP
53 Many threads can have simultaneous read-only access to data, while only one
54 thread can have write access at any given time. Multiple read access with
55 single write access is controlled by locks, which are generally used to protect
56 data that is frequently searched.
57 .sp
58 .LP
59 Readers/writer locks can synchronize threads in this process and other
60 processes if they are allocated in writable memory  and shared among
61 cooperating processes (see \fBmmap\fR(2)), and are initialized for this
62 purpose.
63 .sp
64 .LP
65 Additionally, readers/writer locks must be initialized prior to use.
66 The readers/writer lock pointed to by \fIrwlp\fR is
67 initialized by  \fBrwlock_init()\fR. A readers/writer lock is capable of having
68 several types of behavior, which is specified by \fItype\fR. \fIarg\fR is
69 currently not used, although a future type may define  new behavior parameters
70 by way of  \fIarg\fR.
71 .sp
72 .LP
73 The \fItype\fR argument can be one of the following:
74 .sp
75 .ne 2
76 .na
77 \fB\fBUSYNC_PROCESS\fR \fR
78 .ad
79 .RS 18n
80 The readers/writer lock can synchronize threads in this process and other
81 processes. The readers/writer lock should be initialized by only one process.
82 \fIarg\fR is ignored. A readers/writer lock initialized with this type, must be
83 allocated in memory shared between processses, i.e. either in Sys V shared
84 memory (see \fBshmop\fR(2)) or in memory mapped to a file (see \fBmmap\fR(2)).
85 It is illegal to initialize the object this way and to not allocate it in such
86 shared memory.
87 .RE
89 .sp
90 .ne 2
91 .na
92 \fB\fBUSYNC_THREAD\fR \fR
93 .ad
94 .RS 18n
95 The readers/writer lock can synchronize  threads in this process, only.
96 \fIarg\fR is ignored.
97 .RE
99 .sp
101 Additionally, readers/writer locks can be initialized by allocation  in zeroed
102 memory. A \fItype\fR of \fBUSYNC_THREAD\fR is assumed in this case. Multiple
103 threads must not simultaneously initialize the same  readers/writer lock. And a
104 readers/writer lock must not be re-initialized while in use by other threads.
107 The following are default readers/writer lock initialization (intra-process):
109 .in +2
111 rwlock_t rwlp;
112 rwlock_init(&rwlp, NULL, NULL);
115 .in -2
121 .in +2
123 rwlock_init(&rwlp, USYNC_THREAD, NULL);
125 .in -2
131 .in +2
133 rwlock_t  rwlp  =  DEFAULTRWLOCK;
135 .in -2
139 The following is a customized readers/writer lock  initialization
140 (inter-process):
142 .in +2
144 rwlock_init(&rwlp, USYNC_PROCESS, NULL);
146 .in -2
150 Any state associated with the readers/writer lock pointed to by \fIrwlp\fR are
151 destroyed by  \fBrwlock_destroy()\fR and the readers/writer lock storage space
152 is not released.
155 \fBrw_rdlock()\fR gets a read lock on the readers/writer lock pointed to by
156 \fIrwlp\fR. If the readers/writer lock is currently locked for writing, the
157 calling thread blocks until the write lock is freed. Multiple threads may
158 simultaneously hold a read lock on a  readers/writer lock.
161 \fBrw_tryrdlock()\fR tries to get a read lock on the readers/writer lock pointed
162 to by \fIrwlp\fR. If the readers/writer lock is locked for writing, it returns
163 an error; otherwise, the read lock is acquired.
166 \fBrw_wrlock()\fR gets a write lock on the readers/writer lock pointed to by
167 \fIrwlp\fR. If the readers/writer lock is currently locked for reading or
168 writing, the calling thread blocks until all the read and write locks are
169 freed. At any given time, only one thread may have a write lock on a
170 readers/writer lock.
173 \fBrw_trywrlock()\fR tries to get a write lock on the readers/writer lock
174 pointed to by \fIrwlp\fR. If the readers/writer lock is currently locked for
175 reading or writing, it returns an error.
178 \fBrw_unlock()\fR unlocks a readers/writer lock pointed to by  \fIrwlp\fR, if
179 the readers/writer lock is locked and the calling thread holds the lock for
180 either reading or writing. One of the other threads that is waiting for the
181 readers/writer  lock to be freed will be unblocked, provided there are other
182 waiting threads. If the calling thread does not hold the lock for either
183 reading or writing, no error status is returned, and the program's behavior is
184 unknown.
185 .SH RETURN VALUES
188 If successful, these functions return \fB0\fR. Otherwise, a non-zero value is
189 returned to indicate the error.
190 .SH ERRORS
193 The \fBrwlock_init()\fR function will fail if:
195 .ne 2
197 \fB\fBEINVAL\fR \fR
199 .RS 11n
200 \fItype\fR is invalid.
205 The  \fBrw_tryrdlock()\fR or \fBrw_trywrlock()\fR functions will fail if:
207 .ne 2
209 \fB\fBEBUSY\fR \fR
211 .RS 10n
212 The readers/writer lock pointed to by \fIrwlp\fR was already locked.
217 These functions may fail if:
219 .ne 2
221 \fB\fBEFAULT\fR \fR
223 .RS 11n
224 \fIrwlp\fR or \fIarg\fR points to an illegal address.
227 .SH ATTRIBUTES
230 See \fBattributes\fR(5) for descriptions of the following attributes:
235 box;
236 c | c
237 l | l .
238 ATTRIBUTE TYPE  ATTRIBUTE VALUE
240 MT-Level        MT-Safe
243 .SH SEE ALSO
246 \fBmmap\fR(2), \fBattributes\fR(5)
247 .SH NOTES
250 These interfaces also available by way of:
253 \fB#include\fR \fB<thread.h>\fR
256 If multiple threads are waiting for a readers/writer lock, the acquisition
257 order is random by default. However, some implementations may bias acquisition
258 order to avoid depriving writers. The current implementation favors writers
259 over readers.