summaryrefslogtreecommitdiff
path: root/share/man/man9/srpl_rc_init.9
blob: 7009d0b3c81bfaa52168cff6fed9e33379ebc6ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
.\"	$OpenBSD: srpl_rc_init.9,v 1.9 2015/12/03 16:27:32 mpi Exp $
.\"
.\" Copyright (c) 2015 David Gwynne <dlg@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: December 3 2015 $
.Dt SRPL_RC_INIT 9
.Os
.Sh NAME
.Nm srpl_rc_init ,
.Nm SRPL_HEAD ,
.Nm SRPL_ENTRY ,
.Nm SRPL_INIT ,
.Nm SRPL_ENTER ,
.Nm SRPL_NEXT ,
.Nm SRPL_FOREACH ,
.Nm SRPL_LEAVE ,
.Nm SRPL_EMPTY_LOCKED ,
.Nm SRPL_FIRST_LOCKED ,
.Nm SRPL_NEXT_LOCKED ,
.Nm SRPL_FOREACH_LOCKED ,
.Nm SRPL_FOREACH_SAFE_LOCKED ,
.Nm SRPL_INSERT_HEAD_LOCKED ,
.Nm SRPL_INSERT_AFTER_LOCKED ,
.Nm SRPL_REMOVE_LOCKED ,
.Nm SRPL_RC_INITIALIZER
.Nd singly-linked shared reference pointer list
.Sh SYNOPSIS
.In sys/srp.h
.Vt struct srpl_iter;
.Vt struct srpl_rc;
.Ft void
.Fo "srpl_rc_init"
.Fa "struct srpl_rc *rc"
.Fa "void (*ref)(void *, void *)"
.Fa "void (*unref)(void *, void *)"
.Fa "void *ctx"
.Fc
.Fn SRPL_HEAD "HEADNAME" "TYPE"
.Fn SRPL_ENTRY "TYPE"
.Fn "SRPL_INIT" "SRPL_HEAD *sl"
.Ft void *
.Fn "SRPL_ENTER" "SRPL_HEAD *sl" "struct srpl_iter *si"
.Ft void *
.Fn "SRPL_NEXT" "struct srpl_iter *si" "struct TYPE *listelm" "FIELDNAME"
.Fo "SRPL_FOREACH"
.Fa "VARNAME"
.Fa "SRPL_HEAD *sl"
.Fa "struct srpl_iter *si"
.Fa "FIELDNAME"
.Fc
.Fn "SRPL_LEAVE" "struct srpl_iter *si" "struct TYPE *listelm"
.Fn "SRPL_EMPTY_LOCKED" "SRPL_HEAD *sl"
.Ft void *
.Fn "SRPL_FIRST_LOCKED" "SRPL_HEAD *sl"
.Ft void *
.Fn "SRPL_NEXT_LOCKED" "struct TYPE *listelm" "FIELDNAME"
.Fn "SRPL_FOREACH_LOCKED" "VARNAME" "SRPL_HEAD *sl" "FIELDNAME"
.Fn "SRPL_FOREACH_SAFE_LOCKED" "VARNAME" "SRPL_HEAD *sl" "FIELDNAME" "TEMP_VARNAME"
.Fo "SRPL_INSERT_HEAD_LOCKED"
.Fa "struct srpl_rc *rc"
.Fa "SRPL_HEAD *sl"
.Fa "struct TYPE *elm"
.Fa "FIELDNAME"
.Fc
.Fo "SRPL_INSERT_AFTER_LOCKED"
.Fa "struct srpl_rc *rc"
.Fa "struct TYPE *listelm"
.Fa "struct TYPE *elm"
.Fa "FIELDNAME"
.Fc
.Fo "SRPL_REMOVE_LOCKED"
.Fa "struct srpl_rc *rc"
.Fa "SRPL_HEAD *sl"
.Fa "struct TYPE *listelm"
.Fa "TYPE"
.Fa "FIELDNAME"
.Fc
.Fo "SRPL_RC_INITIALIZER"
.Fa "void (*ref)(void *, void *)"
.Fa "void (*unref)(void *, void *)"
.Fa "void *ctx"
.Fc
.Sh DESCRIPTION
The
srpl
macros build a linked list on top of shared reference pointers.
This allows concurrent traversal of a linked list and access to the
items on the list.
.Pp
SRP lists are a generic type represented by a
.Vt SRPL_HEAD .
The elements inserted into the list must be structures that contain a
.Vt SRPL_ENTRY
field.
Further, the elements must also support reference counting as
insertion and removal operations can cause items to be temporarily
referenced by multiple SRPs within the list at the same time.
.Pp
.Fn srpl_rc_init
initialises the SRP list refcounts
.Fa rc
structure so it can be used to manage the reference counts on
elements in the list.
The insertion or removal of an element in an SRP list will increment
the reference counts on elements within the list via calls to
.Fa ref .
As these references are released by the SRP infrastructure, the
reference counts will be decremented by calls to
.Fa unref .
.Fa unref
is also responsible for freeing the element when the reference count
reaches 0.
Both
.Fa ref
and
.Fa unref
will be called with
.Fa ctx
as their first argument and a pointer to the element as their second
argument.
.Pp
.Fn SRPL_INIT
initialises the SRP list
.Fa sl
to an empty state.
.Pp
.Fn SRPL_ENTER
begins iterating over elements in the SRP list
.Fa sl .
Local state necessary for iterating over the list is stored in the
SRP list iterator structure
.Fa si .
Every call to
.Fn SRPL_ENTER
must have a corresponding call to
.Fn SRPL_LEAVE
to release references to the list and its elements.
.Pp
.Fn SRPL_NEXT
accesses the element in the SRP list after
.Fa listelm .
.Pp
.Fn SRPL_FOREACH
creates a loop for traversing the list.
Every call to
.Fn SRPL_FOREACH
must have a corresponding call to
.Fn SRPL_LEAVE
to release references to the list and its elements.
.Pp
.Fn SRPL_LEAVE
releases references to the list and its elements held by previous
calls to
.Fn SRPL_ENTER
or
.Fn SRPL_FOREACH .
.Pp
.Fn SRPL_EMPTY_LOCKED
tests whether the SRP list
.Fa sl
is empty.
.Pp
.Fn SRPL_FIRST_LOCKED
accesses the first element in the SRP list
.Fa sl .
.Pp
.Fn SRPL_NEXT_LOCKED
accesses the next element in the SRP list after
.Fa listelm .
.Pp
.Fn SRPL_FOREACH_LOCKED
creates a loop for traversing the elements in the SRP list
.Fa sl .
.Pp
.Fn SRPL_FOREACH_SAFE_LOCKED
creates a loop for traversing the elements in the SRP list
.Fa sl ,
permitting it to remove
.Fa VARNAME
as well as freeing it from within the loop safely without interfering with the
traversal.
.Pp
.Fn SRPL_INSERT_HEAD_LOCKED
inserts
.Fa elm
into the SRP list
.Fa sl .
Reference counts are adjusted on the list items using the functions
specified by
.Fa rc .
.Pp
.Fn SRPL_INSERT_AFTER_LOCKED
inserts
.Fa elm
into an SRP list after the element
.Fa listelm .
Reference counts are adjusted on the list items using the functions
specified by
.Fa rc .
.Pp
.Fn SRPL_REMOVE_LOCKED
iterates over the SRP list
.Fa sl
until it finds
.Fa listelm
and then removes it.
Reference counts are adjusted on the list items using the functions
specified by
.Fa rc .
.Pp
An srpl_rc declaration can be initialised with the
.Fn SRPL_RC_INITIALIZER
macro.
.Sh CONTEXT
.Fn SRPL_INIT ,
.Fn SRPL_ENTER ,
.Fn SRPL_NEXT ,
.Fn SRPL_FOREACH ,
and
.Fn SRPL_LEAVE
may be called during autoconf, from process context, or from interrupt
context.
.Pp
.Fn srpl_rc_init ,
.Fn SRPL_EMPTY_LOCKED ,
.Fn SRPL_FIRST_LOCKED ,
.Fn SRPL_NEXT_LOCKED ,
.Fn SRPL_FOREACH_LOCKED ,
.Fn SRPL_INSERT_HEAD_LOCKED ,
.Fn SRPL_INSERT_AFTER_LOCKED ,
and
.Fn SRPL_REMOVE_LOCKED
may be called during autoconf or from process context.
An appropriate lock must be held that prevents concurrent modifications
to the list.
.Sh RETURN VALUES
.Fn SRPL_ENTER ,
.Fn SRPL_NEXT ,
.Fn SRPL_FIRST_LOCKED ,
and
.Fn SRPL_NEXT_LOCKED
return a pointer to elements in the SRP list, or
.Dv NULL
if there are no more elements.
.Pp
.Fn SRPL_EMPTY_LOCKED
returns non-zero when the list is empty, otherwise 0.
.Sh HISTORY
The srp API was originally written by
.An Jonathan Matthew Aq Mt jmatthew@openbsd.org
and
.An David Gwynne Aq Mt dlg@openbsd.org .
The SRP list API first appeared in
.Ox 5.9 .