-
Notifications
You must be signed in to change notification settings - Fork 0
/
srrcu.tex
359 lines (283 loc) · 9.43 KB
/
srrcu.tex
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
%!TEX root = ts.tex
\rSec1[saferecl.rcu]{Read-copy update (RCU)}
\rSec2[saferecl.rcu.general]{General}
\pnum
RCU is a synchronization mechanism that can be used for linked data
structures that are frequently read, but seldom updated. RCU does
not provide mutual exclusion, but instead allows the user to schedule
specified actions such as deletion at some later time.
\pnum
A class type \tcode{T} is \defn{rcu-protectable} if it has exactly one
base class of type \tcode{rcu_obj_base<T,D>} for some \tcode{D}, and
that base is public and non-virtual,
and it has no base classes of type \tcode{rcu_obj_base<X,Y>} for any other
combination \tcode{X}, \tcode{Y}. An object is rcu-protectable if it is
of rcu-protectable type.
\pnum
An invocation of \tcode{unlock} $U$ on an \tcode{rcu_domain dom} corresponds
to an invocation of \tcode{lock} $L$ on \tcode{dom} if $L$ is
sequenced before $U$ and either
\begin{itemize}
\item no other invocation of \tcode{lock} on \tcode{dom} is sequenced
after $L$ and before $U$, or
\item every invocation of \tcode{unlock} $U2$ on \tcode{dom} such
that $L$ is sequenced before $U2$ and $U2$
is sequenced before $U$ corresponds to an invocation of
\tcode{lock} $L2$ on \tcode{dom} such that $L$ is sequenced
before $L2$ and $L2$ is sequenced before $U2$.
\end{itemize}
\begin{note}
This pairs nested locks and unlocks on a given domain in each thread.
\end{note}
\pnum
A \defn{region of RCU protection} on a domain \tcode{dom} starts
with a \tcode{lock} $L$ on \tcode{dom} and ends with its corresponding
\tcode{unlock} $U$.
\pnum
Given a region of RCU protection $R$ on a domain \tcode{dom}
and given an evaluation $E$ that scheduled another evaluation
$F$ in \tcode{dom}, if $E$ does not strongly happen before
the start of $R$, the end of $R$ strongly happens before
evaluating $F$.
\pnum
The evaluation of a scheduled evaluation is potentially concurrent with
any other scheduled evaluation. Each scheduled evaluation is evaluated at
most once.
\rSec2[saferecl.rcu.syn]{Header \tcode{<rcu>} synopsis}
% \indexheader{rcu}
\begin{codeblock}
namespace std {
// \ref{saferecl.rcu.base}, class template \tcode{rcu_obj_base}
template<class T, class D = default_delete<T>>
class rcu_obj_base;
// \ref{saferecl.rcu.domain}, class \tcode{rcu_domain}
class rcu_domain;
rcu_domain& rcu_default_domain() noexcept;
void rcu_synchronize(rcu_domain& dom = rcu_default_domain()) noexcept;
void rcu_barrier(rcu_domain& dom = rcu_default_domain()) noexcept;
template<class T, class D = default_delete<T>>
void rcu_retire(T* p, D d = D(), rcu_domain& dom = rcu_default_domain());
}
\end{codeblock}
\rSec2[saferecl.rcu.base]{Class \tcode{rcu_obj_base}}
Objects of type \tcode{T} to be protected by RCU inherit from a
specialization of \tcode{rcu_obj_base<T,D>}.
\begin{codeblock}
template<class T, class D = default_delete<T>>
class rcu_obj_base {
public:
void retire(D d = D(), rcu_domain& dom = rcu_default_domain()) noexcept;
protected:
rcu_obj_base() = default;
rcu_obj_base(const rcu_obj_base&) = default;
rcu_obj_base(rcu_obj_base&&) = default;
rcu_obj_base& operator=(const rcu_obj_base&) = default;
rcu_obj_base& operator=(rcu_obj_base&&) = default;
~rcu_obj_base() = default;
private:
D @\exposid{deleter}@; // exposition only
};
\end{codeblock}
\pnum
The behavior of a program that adds specializations for
\tcode{rcu_obj_base} is undefined.
\pnum
\tcode{T} may be an incomplete type.
It shall be complete before any member of the resulting specialization of
\tcode{rcu_obj_base} is referenced.
\pnum
\tcode{D} shall be a
function object type ([function.objects]) for which,
given a value \tcode{d} of type \tcode{D} and a value \tcode{ptr}
of type \tcode{T*}, the expression \tcode{d(ptr)} is valid.
\pnum
\tcode{D} shall meet the requirements for
\oldconcept{DefaultConstructible} and \oldconcept{MoveAssignable}.
\pnum
If \tcode{D} is trivially copyable, all specializations of
\tcode{rcu_obj_base<T,D>} are trivially copyable.
\begin{itemdecl}
void retire(D d = D(), rcu_domain& dom = rcu_default_domain()) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\mandates
\tcode{T} is an rcu-protectable type.
\pnum
\expects
\tcode{*this} is a base class subobject of
an object \tcode{x} of type \tcode{T}. The member function
\tcode{rcu_obj_base<T,D>::retire} was not invoked on \tcode{x}
before. The assignment to \exposid{deleter} does not throw an
exception.
\pnum
\effects
Evaluates \tcode{\exposid{deleter} = std::move(d)} and schedules the
evaluation of the expression \tcode{\exposid{deleter}(ad\-dress\-of(x))} in
the domain \tcode{dom}; the behavior is undefined if that evaluation
exits via an exception.
May invoke scheduled evaluations in \tcode{dom}.
\begin{note}
If such evaluations acquire resources held across any invocation of
\tcode{retire} on \tcode{dom}, deadlock can occur.
\end{note}
\end{itemdescr}
\rSec2[saferecl.rcu.domain]{Class \tcode{rcu_domain}}
\rSec3[saferecl.rcu.domain.general]{General}
This class meets the requirements of \oldconcept{Lockable}
([thread.req.lockable.req]) and provides regions of RCU protection.
\begin{example}
\begin{codeblock}
std::scoped_lock<rcu_domain> rlock(rcu_default_domain());
\end{codeblock}
\end{example}
\begin{codeblock}
class rcu_domain {
public:
rcu_domain(const rcu_domain&) = delete;
rcu_domain& operator=(const rcu_domain&) = delete;
void lock() noexcept;
bool try_lock() noexcept;
void unlock() noexcept;
};
rcu_domain& rcu_default_domain() noexcept;
\end{codeblock}
% Tim Song LWG Mar 1 2023: we don't need the private/unspecified
% ctor now that EWG changed the aggregate rules again. C++20.
The functions \tcode{lock} and \tcode{unlock} establish (possibly nested)
regions of RCU protection.
\rSec3[saferecl.rcu.domain.members]{Member functions}
\begin{itemdecl}
void lock() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Opens a region of RCU protection.
\pnum
\remarks
Calls to \tcode{lock} do not introduce a data race
([intro.races]) involving \tcode{*this}.
\end{itemdescr}
\begin{itemdecl}
bool try_lock() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Equivalent to \tcode{lock()}.
\pnum
\returns
\tcode{true}.
\end{itemdescr}
% @@@ Tomasz:
% Cpp17Lockable.
% bool try_lock() noexcept;
Effects: Equivalent to \tcode{lock()}.
Returns: \tcode{true}.
\begin{itemdecl}
void unlock() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
A call to \tcode{lock} that opened
an unclosed region of RCU protection is sequenced before the
call to \tcode{unlock}.
\pnum
\effects
Closes the unclosed region of RCU protection that was
most recently opened.
May invoke scheduled evaluations in \tcode{*this}.
\pnum
\begin{note}
If such evaluations acquire resources held across any invocation
of \tcode{unlock} on \tcode{*this}, deadlock can occur.
\end{note}
Calls to \tcode{unlock} do not introduce a data race
involving \tcode{*this}.
\begin{note}
Evaluation of scheduled evaluations can still cause a data race.
\end{note}
\end{itemdescr}
\rSec3[saferecl.rcu.domain.func]{Non-member functions}
\begin{itemdecl}
rcu_domain& rcu_default_domain() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
A reference to a static-duration object of type \tcode{rcu_domain}.
A reference to the same object is returned every time this
function is called.
\end{itemdescr}
\begin{itemdecl}
void rcu_synchronize(rcu_domain& dom = rcu_default_domain()) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
If the call to \tcode{rcu_synchronize} does not strongly
happen before the lock opening an RCU protection region \tcode{R}
on \tcode{dom}, blocks until the \tcode{unlock} closing \tcode{R}
happens.
\pnum
\sync
The \tcode{unlock} closing \tcode{R} strongly
happens before the return from \tcode{rcu_synchronize}.
\end{itemdescr}
\begin{itemdecl}
void rcu_barrier(rcu_domain& dom = rcu_default_domain()) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
May evaluate any scheduled evaluations in
\tcode{dom}. For any evaluation that happens before the call
to \tcode{rcu_barrier} and that schedules an evaluation $E$
in \tcode{dom}, blocks until $E$ has been evaluated.
\pnum
\sync
The evaluation of any such $E$ strongly
happens before the return from \tcode{rcu_barrier}.
\begin{note}
A call to \tcode{rcu_barrier} does not imply a call to \tcode{rcu_synchronize}
and vice versa.
\end{note}
\end{itemdescr}
\begin{itemdecl}
template<class T, class D = default_delete<T>>
void rcu_retire(T* p, D d = D(), rcu_domain& dom = rcu_default_domain());
\end{itemdecl}
\begin{itemdescr}
\pnum
\mandates
\tcode{is_move_constructible_v<D>} is \tcode{true} and
the expression \tcode{d(p)} is well-formed.
\pnum
\expects
\tcode{D} meets the \oldconcept{MoveConstructible} and
\oldconcept{Destructible} requirements.
\pnum
\effects
May allocate memory.
It is unspecified whether the memory allocation is performed by
invoking \tcode{operator} \tcode{new}.
Initializes an object \tcode{d1} of type \tcode{D} from
\tcode{std::move(d)}.
Schedules the evaluation of \tcode{d1(p)} in the domain
\tcode{dom}; the behavior is undefined if that evaluation
exits via an exception.
May invoke scheduled evaluations in \tcode{dom}.
\begin{note}
If \tcode{rcu_retire} exits via an exception, no evaluation
is scheduled.
\end{note}
\pnum
\throws
\tcode{bad_alloc} or
any exception thrown by the initialization of \tcode{d1}.
\pnum
\begin{note}
If scheduled evaluations acquire resources held across any invocation
of \tcode{rcu_retire} on \tcode{dom}, deadlock can occur.
\end{note}
\end{itemdescr}