-
Notifications
You must be signed in to change notification settings - Fork 3
/
srfi-230.html
572 lines (511 loc) · 26.7 KB
/
srfi-230.html
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SRFI 230: Atomic Operations</title>
<link href="/favicon.png" rel="icon" sizes="192x192" type="image/png">
<link rel="stylesheet" href="https://srfi.schemers.org/srfi.css" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body
{
counter-reset: section;
font-family: serif;
}
h2
{
counter-reset: subsection;
}
h2::before
{
counter-increment: section;
content: counter(section) " ";
}
h3
{
counter-reset: subsubsection;
}
h3::before
{
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
h4::before
{
counter-increment: subsubsection;
content: counter(section) "." counter(subsection) "." counter(subsubsection) " ";
}
span.token
{
font-family: serif;
}
span.token::before
{
content: "⟨";
}
span.token::after
{
content: "⟩";
}
dl.entries dt
{
background-color: lightgrey;
}
dl.entries dd
{
margin-left: 0;
}
dl.entries > dt.condition-type::after
{
float: right;
content: "condition type"
}
dl.entries > dt.procedure::after
{
float: right;
content: "procedure"
}
dl.entries > dt.syntax::after
{
float: right;
content: "syntax"
}
dt.entry > span.type
{
float: right;
}
pre, code
{
font-size: smaller;
}
pre.example
{
margin-left: 1em;
margin-right: 1em;
}
pre.example > span.result
{
float: right;
}
pre.example > span.result::before
{
padding-left: 1em;
padding-right: 1em;
content: "⟹"
}
sup
{
display: inline-block;
}
</style>
</head>
<body>
<h1><a href="https://srfi.schemers.org/"><img class="srfi-logo" src="https://srfi.schemers.org/srfi-logo.svg" alt="SRFI surfboard logo" /></a>230: Atomic Operations</h1>
<p>by Marc Nieper-Wißkirchen</p>
<h2 id="status">Status</h2>
<p>This SRFI is currently in <em>final</em> status. Here is <a href="https://srfi.schemers.org/srfi-process.html">an explanation</a> of each status that a SRFI can hold. To provide input on this SRFI, please send email to <code><a href="mailto:srfi+minus+230+at+srfi+dotschemers+dot+org">srfi-230@<span class="antispam">nospam</span>srfi.schemers.org</a></code>. To subscribe to the list, follow <a href="https://srfi.schemers.org/srfi-list-subscribe.html">these instructions</a>. You can access previous messages via the mailing list <a href="https://srfi-email.schemers.org/srfi-230">archive</a>.</p>
<ul>
<li>Received: 2021-08-29</li>
<li>Draft #1 published: 2021-09-01</li>
<li>Draft #2 published: 2021-11-07</li>
<li>Finalized: 2021-11-15</li>
</ul>
<h2 id="abstract">Abstract</h2>
<p>This SRFI defines atomic operations for the Scheme programming language.
An <dfn>atomic operation</dfn> is an operation that, even in the presence
of multiple threads, is either executed completely or not at all. Atomic
operations can be used to implement mutexes and other synchronization
primitives, and they can be used to make concurrent algorithms lock-free.
For this, this SRFI defines two data types, <dfn>atomic flags</dfn>
and <dfn>atomic (fixnum) boxes</dfn>, whose contents can be queried and
mutated atomically. Moreover, each atomic operation comes with
a <dfn>memory order</dfn> that defines the level of synchronization with
other threads.</p>
<h2 id="rationale">Rationale</h2>
<p>Virtually all modern multi-threaded CPUs support atomic operations.
They can often be used to avoid costly locks in concurrent programming.
Many general-purpose programming languages like C, C++, Go, or Rust
expose a standard set of atomic operations to the programmer. This SRFI
does the same for the Scheme programming language.</p>
<p>This SRFI is particularly interesting for multi-threaded applications,
e.g. those based on <a href="https://srfi.schemers.org/srfi-18/">SRFI 18</a> (or any other threading library), but can
also be used in general library code to make it thread-safe.</p>
<h3>Example</h3>
<p>The following example shows a typical use of atomic fixnum boxes. If
the counter were a regular variable, the read-modify-write operation of
increasing the counter might be interrupted by another one in a different
thread, resulting in a total count too small.</p>
<pre class="example">(import (scheme base)
(scheme write)
(srfi 18)
(srfi 230))
(define *atomic-counter* (make-atomic-fxbox 0))
(define (task)
(do ((i 0 (+ i 1)))
((= i 1000))
(atomic-fxbox+/fetch! *atomic-counter* 1)))
(define threads (make-vector 10))
(do ((i 0 (+ i 1)))
((= i 10))
(let ((thread (make-thread task)))
(vector-set! threads i thread)
(thread-start! thread)))
(do ((i 0 (+ i 1)))
((= i 10))
(thread-join! (vector-ref threads i)))
(display (atomic-fxbox-ref *atomic-counter*))
(newline)</pre>
<h2 id="specification">Specification</h2>
<p>The procedures and syntax described in this section are exported by
the <code>(srfi 230)</code> library in an R<sup>7</sup>RS system and by both the <code>(srfi :230
atomic)</code> and <code>(srfi :230)</code> libraries in an R<sup>6</sup>RS system.</p>
<h3>Entry format</h3>
<p>The following naming conventions imply type restrictions:</p>
<dl>
<dt><code><var>obj</var></code></dt>
<dt><code><var>expected</var></code></dt>
<dt><code><var>desired</var></code></dt>
<dd>any object</dd>
<dt><code><var>fx</var></code></dt>
<dd>fixnum</dd>
<dt><code><var>memory-order</var></code></dt>
<dd>any memory order</dd>
<dt><code><var>atomic-flag</var></code></dt>
<dd>atomic flag</dd>
<dt><code><var>atomic-box</var></code></dt>
<dd>atomic box</dd>
<dt><code><var>atomic-fxbox</var></code></dt>
<dd>atomic fixnum box</dd>
<dt><code><var>atomic-pair</var></code></dt>
<dd>atomic pair</dd>
</dl>
<h3>Atomicity</h3>
<p>Unless otherwise specified, all procedures prefixed with <code>atomic-</code>
in the library are executed atomically.</p>
<p><i>Note:</i> In a <i>safe</i> top-level program (in the sense of
R<sup>6</sup>RS), all reads and writes to the store are necessarily
atomic. In particular, a library implementing SRFI 18 is not necessarily
safe.</p>
<h3>Memory orders</h3>
<p>The memory order specifies how accesses, including regular, non-atomic
memory accesses, are to be ordered around an atomic operation. When
multiple threads simultaneously read and write to several variables, one
thread can observe the values change in an order different from the order
in which another thread wrote them. Indeed, the apparent order of changes can even
differ among multiple reader threads.
<p>The default behavior of all atomic operations defined in this library
provides for <i>sequentially consistent ordering</i> (see below). That
default can hurt performance, but the library's atomic operations can be
given an additional <code><var>memory-order</var></code> argument to
specify the exact constraints, beyond atomicity, that the implementation
must enforce for that operation.</p>
<p>Atomic operations can be given the following memory orders to modify the
default behavior:</p>
<dl>
<dt><code>relaxed</code></dt>
<dd><i>Relaxed operation</i>: there are no synchronization or ordering
constraints imposed on other reads or writes. Only this operation's
atomicity is guaranteed.</dd>
<dt><code>acquire</code></dt>
<dd>A read operation on the store with this memory order performs the
<i>acquire operation</i> on the affected memory location: no reads or
writes in the current thread can be reordered before this read. All
writes in other threads that <i>release</i> the same atomic variable
are visible in the current thread.</dd>
<dt><code>release</code></dt>
<dd>A write operation on the store with this memory order performs
the <i>release operation</i>: no reads or writes in the current thread
can be reordered after this write. All writes in the current thread
are visible in other threads that <i>acquire</i> the same atomic
variable.</dd>
<dt><code>acquire-release</code></dt>
<dd>A read-modify-write operation on the store with this memory order is
both an <i>acquire operation</i> and a <i>release operation</i>. No
reads or writes on the store in the current thread can be reordered before or
after this write. All writes in other threads that release the same
atomic variable are visible before the modification and the modification
is visible in other threads that acquire the same atomic variable.</dd>
<dt><code>sequentially-consistent</code></dt>
<dd>A read operation on the store with this memory order performs an
<i>acquire operation</i>, a write performs a <i>release operation</i>,
and read-modify-write performs both an <i>acquire operation</i> and
a <i>release operation</i>, plus a single total order exists in which all
threads observe all modifications in the same order.</dd>
</dl>
<dl class="entries">
<dt class="entry"><code>(memory-order <span class="token">memory-order symbol</span>)</code><span class="type">syntax</span></dt>
<dd>
<p>It is a syntax violation if <code><span class="token">memory-order
symbol</span></code> is not a symbol whose name is one of
<code>relaxed</code>, <code>acquire</code>, <code>release</code>, <code>acquire-release</code>,
and <code>sequentially-consistent</code>. The result is the
corresponding symbol, and specifies the associated memory order.</p>
<p><i>Note:</i>
Only the name of <code><span class="token">memory-order
symbol</span></code> is significant.</p>
</dd>
<dt class="entry"><code>(memory-order? <var>obj</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns <code>#t</code> if the argument is a valid memory-order
symbol, and returns <code>#f</code> otherwise.</p>
</dd>
</dl>
<h3>Atomic flags</h3>
<p>An <dfn>atomic flag</dfn> is tagged with a location that can be either
in the <dfn>clear</dfn> state or in the <dfn>set</dfn> state and which can be
changed and tested atomically.</p>
<dl class="entries">
<dt class="entry"><code>(make-atomic-flag)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns a newly allocated atomic flag, which is initially in the
clear state.</p>
</dd>
<dt class="entry"><code>(atomic-flag? <var>obj</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns <code>#t</code> if <code><var>obj</var></code> is an atomic
flag, and returns <code>#f</code> otherwise.</p>
</dd>
<dt class="entry"><code>(atomic-flag-test-and-set! <var>atomic-flag</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-flag-test-and-set! <var>atomic-flag</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns <code>#t</code> if the state
of <code><var>atomic-flag</var></code> is set, and
returns <code>#f</code> otherwise. Changes the state
of <code><var>atomic-flag</var></code> to set.</p>
</dd>
<dt class="entry"><code>(atomic-flag-clear! <var>atomic-flag</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-flag-clear! <var>atomic-flag</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Changes the state of <code><var>atomic-flag</var></code> to clear.</p>
</dd>
</dl>
<h3>Atomic boxes</h3>
<p>An <dfn>atomic box</dfn> is tagged with a location that can hold any
object, the <dfn>content</dfn> of the atomic box, and which can be
changed and tested atomically.</p>
<dl class="entries">
<dt class="entry"><code>(make-atomic-box <var>obj</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns a newly allocated atomic box whose initial content
is <code><var>obj</var></code>.</p>
</dd>
<dt class="entry"><code>(atomic-box? <var>obj</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns <code>#t</code> if <code><var>obj</var></code> is an atomic
box, and returns <code>#f</code> otherwise.</p>
</dd>
<dt class="entry"><code>(atomic-box-ref <var>atomic-box</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-box-ref <var>atomic-box</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns the content of <code><var>atomic-box</var></code>.</p>
</dd>
<dt class="entry"><code>(atomic-box-set! <var>atomic-box</var> <var>obj</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-box-set! <var>atomic-box</var> <var>obj</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-box</var></code>
to <code><var>obj</var></code>, and returns unspecified values.</p>
</dd>
<dt class="entry"><code>(atomic-box-swap! <var>atomic-box</var> <var>obj</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-box-swap! <var>atomic-box</var> <var>obj</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets of the content of <code><var>atomic-box</var></code>
to <code><var>obj</var></code>, and returns the previous content.</p>
</dd>
<dt class="entry"><code>(atomic-box-compare-and-swap! <var>atomic-box</var> <var>expected</var> <var>desired</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-box-compare-and-swap! <var>atomic-box</var> <var>expected</var> <var>desired</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Operationally equivalent to the following, executed atomically:</p>
<pre class="code">(let ((actual (atomic-box-ref <var>atomic-box</var> <var>memory-order</var>)))
(when (eq? actual expected)
(atomic-box-set! <var>atomic-box</var> <var>desired</var> <var>memory-order</var>))
actual)</pre></dd>
</dl>
<h3>Atomic fixnum boxes</h3>
<p>An <dfn>atomic fixnum box</dfn> is tagged with a location that can hold
a fixnum, the <dfn>content</dfn> of the atomic fixnum box, which can be
changed and tested atomically.</p>
<dl class="entries">
<dt class="entry"><code>(make-atomic-fxbox <var>obj</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns a newly allocated atomic fixnum box whose initial content
is <code><var>obj</var></code>.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox? <var>obj</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns <code>#t</code> if <code><var>obj</var></code> is an atomic
fixnum box, and returns <code>#f</code> otherwise.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-ref <var>atomic-fxbox</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-ref <var>atomic-fxbox</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns the content of <code><var>atomic-fxbox</var></code>.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-set! <var>atomic-fxbox</var> <var>fx</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-set! <var>atomic-fxbox</var> <var>fx</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-fxbox</var></code>
to <code><var>fx</var></code>, and returns unspecified values.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-swap! <var>atomic-fxbox</var> <var>fx</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-swap! <var>atomic-fxbox</var> <var>fx</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets of the content of <code><var>atomic-fxbox</var></code>
to <code><var>fx</var></code>, and returns the previous content.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-compare-and-swap! <var>atomic-fxbox</var> <var>expected</var> <var>desired</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-compare-and-swap! <var>atomic-fxbox</var> <var>expected</var> <var>desired</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Operationally equivalent to the following, executed atomically:</p>
<pre class="code">(let ((actual (atomic-fxbox-ref <var>atomic-fxbox</var> <var>memory-order</var>)))
(when (fx=? actual expected)
(atomic-fxbox-set! <var>atomic-fxbox</var> <var>desired</var> <var>memory-order</var>))
actual)</pre></dd>
<dt class="entry"><code>(atomic-fxbox+/fetch! <var>atomic-fxbox</var> <var>fx</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox+/fetch! <var>atomic-fxbox</var> <var>fx</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-fxbox</var></code> to the sum of
the previous content and <code><var>fx</var></code>, and returns the
previous content.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-/fetch! <var>atomic-fxbox</var> <var>fx</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-/fetch! <var>atomic-fxbox</var> <var>fx</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-fxbox</var></code> to the
difference of the previous content and <code><var>fx</var></code>,
and returns the previous content.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-and/fetch! <var>atomic-fxbox</var> <var>fx</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-and/fetch! <var>atomic-fxbox</var> <var>fx</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-fxbox</var></code> to the
bitwise <em>and</em> of the previous content and <code><var>fx</var></code>,
and returns the previous content.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-ior/fetch! <var>atomic-fxbox</var> <var>fx</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-ior/fetch! <var>atomic-fxbox</var> <var>fx</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-fxbox</var></code> to the
bitwise <em>inclusive or</em> of the previous content
and <code><var>fx</var></code>, and returns the previous content.</p>
</dd>
<dt class="entry"><code>(atomic-fxbox-xor/fetch! <var>atomic-fxbox</var> <var>fx</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-fxbox-xor/fetch! <var>atomic-fxbox</var> <var>fx</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-fxbox</var></code> to the
bitwise <em>exclusive or</em> of the previous content
and <code><var>fx</var></code>, and returns the previous content.</p>
</dd>
</dl>
<h3>Atomic pairs</h3>
<p>An <dfn>atomic pair</dfn> is tagged with two locations that can hold
a pair of Scheme objects, the <dfn>contents</dfn> of the atomic pair, and which can be
changed and tested atomically.</p>
<p>Atomic pairs can be used to solve the
so-called <a href="https://en.wikipedia.org/wiki/ABA_problem">ABA
problem</a>, for example.</p>
<dl class="entries">
<dt class="entry"><code>(make-atomic-pair <var>obj<sub>1</sub></var> <var>obj<sub>2</sub></var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns a newly allocated atomic pair whose initial
contents are <code><var>obj<sub>1</sub></var></code>
and <code><var>obj<sub>2</sub></var></code>.</p>
</dd>
<dt class="entry"><code>(atomic-pair? <var>obj</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns <code>#t</code> if <code><var>obj</var></code> is
an atomic pair, and returns <code>#f</code> otherwise.</p>
</dd>
<dt class="entry"><code>(atomic-pair-ref <var>atomic-pair</var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-pair-ref <var>atomic-pair</var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Returns the contents of <code><var>atomic-pair</var></code> as two values.</p>
</dd>
<dt class="entry"><code>(atomic-pair-set! <var>atomic-pair</var> <var>obj<sub>1</sub></var> <var>obj<sub>2</sub></var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-pair-set! <var>atomic-pair</var> <var>obj<sub>1</sub></var> <var>obj<sub>2</sub></var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-pair</var></code>
to <code><var>obj<sub>1</sub></var></code>
and <code><var>obj<sub>2</sub></var></code>, and returns
unspecified values.</p>
</dd>
<dt class="entry"><code>(atomic-pair-swap! <var>atomic-pair</var> <var>obj<sub>1</sub></var> <var>obj<sub>2</sub></var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-pair-swap! <var>atomic-pair</var> <var>obj<sub>1</sub></var> <var>obj<sub>2</sub></var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Sets the content of <code><var>atomic-pair</var></code>
to <code><var>obj<sub>1</sub></var></code>
and <code><var>obj<sub>2</sub></var></code>, and returns the
previous contents as two values.
</dd>
<dt class="entry"><code>(atomic-pair-compare-and-swap! <var>atomic-pair</var> <var>expected<sub>1</sub></var> <var>expected<sub>2</sub></var> <var>desired<sub>1</sub></var> <var>desired<sub>2</sub></var>)</code><span class="type">procedure</span></dt>
<dt class="entry"><code>(atomic-pair-compare-and-swap! <var>atomic-pair</var> <var>expected<sub>1</sub></var> <var>expected<sub>2</sub></var> <var>desired<sub>1</sub></var> <var>desired<sub>2</sub></var> <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Operationally equivalent to the following, executed atomically:</p>
<pre class="code">(let-values (((actual1 actual2) (atomic-pair-ref <var>atomic-pair</var> <var>memory-order</var>)))
(when (and (eq? actual1 <var>expected<sub>1</sub></var>) (eq? actual2 <var>expected<sub>2</sub></var>))
(atomic-pair-set! <var>atomic-pair</var> <var>desired<sub>1</sub></var> <var>desired<sub>2</sub></var> <var>memory-order</var>))
(values actual1 actual2))</pre></dd>
</dl>
<h3>Memory synchronization</h3>
<dl class="entries">
<dt class="entry"><code>(atomic-fence <var>memory-order</var>)</code><span class="type">procedure</span></dt>
<dd>
<p>Establishes memory synchronization ordering of non-atomic and
relaxed atomic accesses, as instructed
by <code><var>memory-order</var></code>, without an associated atomic
operation. Returns unspecified values.</p>
<p><i>Note:</i> As this procedure does not store a value into a
previous allocated location, its name does not end in
“<code>!</code>”.</p>
</dd>
</dl>
<h2 id="implementation">Implementation</h2>
<p>For Schemes that do not support multiple threads, an implementation of
this SRFI is trivial as all operations are automatically atomic. In
particular, atomic flags and atomic boxes could be represented by SRFI
111 boxes.</p>
<p>For Schemes that support multiple threads, an implementation based on
SRFI 18 or <a href="https://srfi.schemers.org/srfi-226/">SRFI 226</a> mutexes is possible.</p>
<p>The primitives defined in this SRFI are all supported by modern
multi-threaded CPUs and are also implemented in the C and the C++
programming languages. Thus, Scheme systems with a native compiler or
Scheme systems with a virtual machine based on C or C++ can easily
implement this SRFI efficiently.</p>
<p>The <a href="./lib/srfi/230.sld">sample implementation</a> is an R<sup>7</sup>RS
implementation based on SRFI 18.</p>
<h2 id="acknowledgements">Acknowledgements</h2>
<p>This SRFI was inspired by the C11 atomic operations library.</p>
<p>It uses language from
the <a href="https://en.cppreference.com/w/c"><cite>C
reference</cite></a> wiki.</p>
<p>Many thanks go to Justin Ethier,
who <a href="https://github.com/cyclone-scheme/srfi-230">implemented</a>
SRFI 230 natively for his Cyclone Scheme compiler.</p>
<p>Likewise, thanks go to Göran Weinholt for private feedback. He
pointed out to me the omission of double-width CAS operations in
the first draft.</p>
<p>Shiro Kawai wrote portable tests.</p>
<h2 id="copyright">Copyright</h2>
<p>© 2021 Marc Nieper-Wißkirchen.</p>
<p>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:</p>
<p>
The above copyright notice and this permission notice (including the
next paragraph) shall be included in all copies or substantial
portions of the Software.</p>
<p>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
<hr>
<address>Editor: <a href="mailto:srfi-editors+at+srfi+dot+schemers+dot+org">Arthur A. Gleckler</a></address></body></html>