@amritk/nish-aarch64-linux 0.35.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/threads.ts
CHANGED
|
@@ -2,47 +2,66 @@
|
|
|
2
2
|
* `std/threads` — data parallelism, a scope for heterogeneous tasks, and a
|
|
3
3
|
* lock that owns its data (docs/wp29-thread-surface.md §4.1, §4.2 and §4.3).
|
|
4
4
|
*
|
|
5
|
-
* import {
|
|
5
|
+
* import { parallelFrom, parallelMap, parallelMapInto, parallelReduce, scope, scopeWithStack } from "nish/threads";
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
7
|
+
* const sumOf = (xs: i32[]): i32 => {
|
|
8
|
+
* let total: i32 = 0;
|
|
9
|
+
* for (const x of xs) {
|
|
10
|
+
* total = total + x;
|
|
11
|
+
* }
|
|
12
|
+
* return total;
|
|
13
|
+
* };
|
|
14
|
+
*
|
|
15
|
+
* export const main = (): i32 => {
|
|
16
|
+
* const src: i32[] = [1, 2, 3, 4];
|
|
17
|
+
* const k: i32 = 10;
|
|
18
|
+
* const dst: i32[] = [0, 0, 0, 0];
|
|
19
|
+
* parallelMapInto(src, dst, (x) => x * 3); // dst is [3, 6, 9, 12]
|
|
20
|
+
* const ys = parallelMap(src, (x) => x * k); // [10, 20, 30, 40]: `k` is read by the body
|
|
21
|
+
* const sq = parallelFrom(4, (i) => i * i); // [0, 1, 4, 9]
|
|
22
|
+
* const total = parallelReduce(src, (a, b) => a + b, 0); // 10
|
|
23
|
+
* const out: i32[] = [0, 0];
|
|
24
|
+
* {
|
|
25
|
+
* using s = scope();
|
|
26
|
+
* s.spawn(sumOf, ys, out, 0);
|
|
27
|
+
* s.spawn(sumOf, sq, out, 1);
|
|
28
|
+
* } // both tasks have run, and `out` is [100, 14]
|
|
29
|
+
* {
|
|
30
|
+
* using s = scopeWithStack(64 * 1024 * 1024); // each task's thread has a 64 MiB stack
|
|
31
|
+
* s.spawn(sumOf, src, out, 0);
|
|
32
|
+
* }
|
|
33
|
+
* return total;
|
|
34
|
+
* };
|
|
20
35
|
*
|
|
21
36
|
* **What is written here is the meaning, not the implementation.** Each body
|
|
22
|
-
* below is the sequential program, and it is what runs under Node, what
|
|
23
|
-
*
|
|
24
|
-
* The compiler then recognises the exported templates by module and name
|
|
25
|
-
* and lowers an instance of `parallelMapInto`, `parallelFrom` or
|
|
26
|
-
* `parallelReduce` onto `nish_parallel_range` (runtime/runtime-parallel.c): it
|
|
27
|
-
* replaces the one call that walks the whole range — `mapRange` for a map,
|
|
28
|
-
* `fromRange` for a `parallelFrom`, `reduceBlocks` for a reduce — with a region
|
|
29
|
-
* that hands each thread a contiguous piece of it, and emits the rest of the
|
|
30
|
-
* body as written. `parallelMap` is `parallelMapInto` into an array it
|
|
31
|
-
* allocates first. So the length check, its message and the order a reduce combines in
|
|
32
|
-
* are this file's, whichever way the program is compiled.
|
|
37
|
+
* below is the sequential program, and it is what runs under Node, what `npm
|
|
38
|
+
* run check` type-checks, and what the compiler checks the call against.
|
|
33
39
|
*
|
|
34
|
-
*
|
|
40
|
+
* The compiler then recognises the exported templates by module and name and
|
|
41
|
+
* lowers an instance of `parallelMapInto`, `parallelFrom` or `parallelReduce`
|
|
42
|
+
* onto `nish_parallel_range` (runtime/runtime-parallel.c): it replaces the one
|
|
43
|
+
* call that walks the whole range — `mapRange` for a map, `fromRange` for a
|
|
44
|
+
* `parallelFrom`, `reduceBlocks` for a reduce — with a region that hands each
|
|
45
|
+
* thread a contiguous piece of it, and emits the rest of the body as written.
|
|
46
|
+
* `parallelMap` is `parallelMapInto` into an array it allocates first. So the
|
|
47
|
+
* length check, its message and the order a reduce combines in are this file's,
|
|
48
|
+
* whichever way the program is compiled.
|
|
49
|
+
*
|
|
50
|
+
* **What makes the region safe is checked at the call, not trusted**
|
|
35
51
|
* (docs/LANGUAGE.md, "Data parallelism"): the function passed as `f` may write
|
|
36
52
|
* nothing its caller could observe, may allocate only temporaries it drops
|
|
37
53
|
* before it returns — they are given back after every element — `dst` may not
|
|
38
54
|
* be reachable from an element of `src`, and the result type is a number, a
|
|
39
55
|
* `boolean` or an enum — or, for a map, a `string` or an array of those
|
|
40
|
-
* scalars, which the join copies out of the worker's arena.
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
56
|
+
* scalars, which the join copies out of the worker's arena.
|
|
57
|
+
*
|
|
58
|
+
* An arrow written at the call may read the `const` locals and the parameters
|
|
59
|
+
* of the function it is written in: each one is handed to the body as a
|
|
60
|
+
* parameter of its own, which no body may write through. Importing this module
|
|
61
|
+
* compiles the program with `--threads`, because every worker needs an arena of
|
|
62
|
+
* its own.
|
|
44
63
|
*
|
|
45
|
-
* A reduce is deterministic
|
|
64
|
+
* **A reduce is deterministic.** `src` is split into `min(64, ceil(n / BLOCK))`
|
|
46
65
|
* blocks, each block is folded from `identity`, and the block results are
|
|
47
66
|
* combined left to right — here, on one thread, and by the compiled program on
|
|
48
67
|
* any number of them — so an `f64` sum is the same bits on one core or sixty
|
|
@@ -160,6 +179,14 @@ const dstTooShort = (have: i32, want: i32): void => {
|
|
|
160
179
|
* `dst[i] = f(src[i])` for every index of `src`, on as many threads as the
|
|
161
180
|
* machine has and the length is worth. `dst` must be at least as long as
|
|
162
181
|
* `src`, which is checked once, before any element is written.
|
|
182
|
+
*
|
|
183
|
+
* @param src the elements to map, read and not written.
|
|
184
|
+
* @param dst where `f(src[i])` is written, at the same index. If it is shorter
|
|
185
|
+
* than `src` the program stops before any element is written; any elements
|
|
186
|
+
* past `src.length` are left as they were.
|
|
187
|
+
* @param f computes one element from one element of `src`. It is called once
|
|
188
|
+
* per element, natively on any thread and in no set order, so it may write
|
|
189
|
+
* nothing its caller could observe (see the module header).
|
|
163
190
|
*/
|
|
164
191
|
export const parallelMapInto = <T, U>(src: T[], dst: U[], f: (x: T) => U): void => {
|
|
165
192
|
const n: i32 = toI32(src.length)
|
|
@@ -173,6 +200,15 @@ export const parallelMapInto = <T, U>(src: T[], dst: U[], f: (x: T) => U): void
|
|
|
173
200
|
* `f` folded over `src`, blockwise from `identity` and then left to right over
|
|
174
201
|
* the blocks, so the answer does not depend on how many threads computed it.
|
|
175
202
|
* An empty `src` answers `identity`.
|
|
203
|
+
*
|
|
204
|
+
* @param src the elements to fold, read and not written.
|
|
205
|
+
* @param f combines an accumulator with an element, and the blocks' results
|
|
206
|
+
* with each other, so it must be associative for the answer to mean the
|
|
207
|
+
* fold it reads as.
|
|
208
|
+
* @param identity what each block's fold starts from, which must be `f`'s
|
|
209
|
+
* identity, such as `0` for `+`.
|
|
210
|
+
* @returns the fold of every element of `src`, the same however many threads
|
|
211
|
+
* computed it.
|
|
176
212
|
*/
|
|
177
213
|
export const parallelReduce = <T>(src: T[], f: (acc: T, x: T) => T, identity: T): T => {
|
|
178
214
|
const blocks: i32 = reduceBlockCount(toI32(src.length))
|
|
@@ -192,6 +228,11 @@ export const parallelReduce = <T>(src: T[], f: (acc: T, x: T) => T, identity: T)
|
|
|
192
228
|
* A new array of `f(src[i])` for every index of `src`: `parallelMapInto` into
|
|
193
229
|
* an array this call allocates, before any element is computed, so nothing
|
|
194
230
|
* but the map itself can hold it while the region runs.
|
|
231
|
+
*
|
|
232
|
+
* @param src the elements to map, read and not written.
|
|
233
|
+
* @param f computes one element from one element of `src`, under the rules
|
|
234
|
+
* `parallelMapInto` gives its `f`.
|
|
235
|
+
* @returns a new array of `src.length` elements, which the caller owns.
|
|
195
236
|
*/
|
|
196
237
|
export const parallelMap = <T, U>(src: T[], f: (x: T) => U): U[] => {
|
|
197
238
|
const dst = new Array<U>(toI32(src.length))
|
|
@@ -217,6 +258,11 @@ const fromRange = <U>(dst: U[], f: (i: i32) => U, lo: i32, hi: i32): void => {
|
|
|
217
258
|
* A new array of `n` elements, `f(i)` at index `i`, computed on as many
|
|
218
259
|
* threads as the machine has and the length is worth: `Array.from({ length:
|
|
219
260
|
* n }, (_, i) => f(i))`, with the array allocated before any element is.
|
|
261
|
+
*
|
|
262
|
+
* @param n how many elements the array has.
|
|
263
|
+
* @param f computes the element at an index from the index alone, under the
|
|
264
|
+
* rules `parallelMapInto` gives its `f`.
|
|
265
|
+
* @returns a new array of `n` elements, which the caller owns.
|
|
220
266
|
*/
|
|
221
267
|
export const parallelFrom = <U>(n: i32, f: (i: i32) => U): U[] => {
|
|
222
268
|
const dst = new Array<U>(n)
|
|
@@ -284,6 +330,7 @@ const slotWritten = (at: i32): boolean => at < 0 || at >= 0
|
|
|
284
330
|
*/
|
|
285
331
|
export class ThreadScope {
|
|
286
332
|
/**
|
|
333
|
+
* @internal Used by `scopeWithStack`.
|
|
287
334
|
* The stack, in bytes, of each thread the scope's tasks run on, or 0 for the
|
|
288
335
|
* platform's default. Only `scopeWithStack` writes it, and only
|
|
289
336
|
* `nish_scope_join` (runtime/runtime-parallel.c) reads it, as the scope's
|
|
@@ -296,6 +343,14 @@ export class ThreadScope {
|
|
|
296
343
|
* Run `entry(arg)` on a thread of its own and store its answer in `dst[at]`
|
|
297
344
|
* when the scope closes. `at` is checked here, before the task is taken, and
|
|
298
345
|
* again when its answer is stored.
|
|
346
|
+
*
|
|
347
|
+
* @param entry the task: a function of one argument, run on a thread of its
|
|
348
|
+
* own when the scope closes.
|
|
349
|
+
* @param arg what `entry` is called with when the task runs.
|
|
350
|
+
* @param dst the array the task's answer is stored in, once every task of
|
|
351
|
+
* the scope has finished.
|
|
352
|
+
* @param at the index in `dst` the answer goes to. One outside `dst` stops
|
|
353
|
+
* the program here, before the task is taken.
|
|
299
354
|
*/
|
|
300
355
|
spawn<A, R>(entry: (arg: A) => R, arg: A, dst: R[], at: i32): void {
|
|
301
356
|
if (!slotWritten(at)) {
|
|
@@ -317,6 +372,9 @@ export class ThreadScope {
|
|
|
317
372
|
* a scope is only ever the receiver of a `spawn` statement, so `s.spawnWith(...)`
|
|
318
373
|
* written in a program is refused by that rule (NL2389), as passing the
|
|
319
374
|
* scope would be.
|
|
375
|
+
*
|
|
376
|
+
* @param entry the task, whose answer is dropped.
|
|
377
|
+
* @param arg what `entry` is called with when the task runs.
|
|
320
378
|
*/
|
|
321
379
|
spawnWith<A, R>(entry: (arg: A) => R, arg: A): void {
|
|
322
380
|
runWith(entry, arg)
|
|
@@ -327,6 +385,8 @@ export class ThreadScope {
|
|
|
327
385
|
* captured `const` locals are its arguments, on a thread of its own when the
|
|
328
386
|
* scope closes, and store nothing. Only the compiler names this method, and
|
|
329
387
|
* a program that names it is refused as `spawnWith`'s is (NL2389).
|
|
388
|
+
*
|
|
389
|
+
* @param entry the task: an arrow of no parameters, whose answer is dropped.
|
|
330
390
|
*/
|
|
331
391
|
spawnThunk<R>(entry: () => R): void {
|
|
332
392
|
runThunk(entry)
|
|
@@ -342,7 +402,12 @@ export class ThreadScope {
|
|
|
342
402
|
}
|
|
343
403
|
}
|
|
344
404
|
|
|
345
|
-
/**
|
|
405
|
+
/**
|
|
406
|
+
* A new scope, for a `using` declaration: `using s = scope()`.
|
|
407
|
+
*
|
|
408
|
+
* @returns a new scope of its own, whose tasks run on threads of the
|
|
409
|
+
* platform's default stack size and are joined when the block ends.
|
|
410
|
+
*/
|
|
346
411
|
export const scope = (): ThreadScope => new ThreadScope()
|
|
347
412
|
|
|
348
413
|
/**
|
|
@@ -360,6 +425,11 @@ const MIN_STACK: i32 = 131072
|
|
|
360
425
|
* rounds `bytes` up to the page size and adds a guard page below it, so a task
|
|
361
426
|
* that overflows it faults instead of writing past it. Under Node, and on a
|
|
362
427
|
* target without threads, the size is checked and has no other effect.
|
|
428
|
+
*
|
|
429
|
+
* @param bytes the least stack each task's thread gets, in bytes. Below
|
|
430
|
+
* 131,072 (128 KiB) is a compile error when it is a constant (NL2584) and
|
|
431
|
+
* stops the program when it is computed.
|
|
432
|
+
* @returns a new scope of its own, joined when the block ends.
|
|
363
433
|
*/
|
|
364
434
|
export const scopeWithStack = (bytes: i32): ThreadScope => {
|
|
365
435
|
if (bytes < MIN_STACK) {
|
|
@@ -385,6 +455,7 @@ export class MutexGuard<T> {
|
|
|
385
455
|
value: T
|
|
386
456
|
|
|
387
457
|
/**
|
|
458
|
+
* @internal Read by nothing written here.
|
|
388
459
|
* The lock word: 0 when the lock is free, 1 while a guard's block holds it.
|
|
389
460
|
* Nothing written here reads or writes it. Natively the compiler takes it
|
|
390
461
|
* with an acquire compare-and-swap in `lock()` and gives it back with a
|
|
@@ -393,6 +464,12 @@ export class MutexGuard<T> {
|
|
|
393
464
|
*/
|
|
394
465
|
word: i32 = 0
|
|
395
466
|
|
|
467
|
+
/**
|
|
468
|
+
* A guard over `value`, with the lock free. `Mutex` makes its one guard
|
|
469
|
+
* with this.
|
|
470
|
+
*
|
|
471
|
+
* @param value the data the lock owns, held as it is, not copied.
|
|
472
|
+
*/
|
|
396
473
|
constructor(value: T) {
|
|
397
474
|
this.value = value
|
|
398
475
|
}
|
|
@@ -420,8 +497,15 @@ export class MutexGuard<T> {
|
|
|
420
497
|
*/
|
|
421
498
|
export class Mutex<T> {
|
|
422
499
|
/** The one guard, which holds the data; only `lock()` hands it out. */
|
|
423
|
-
guard: MutexGuard<T>
|
|
500
|
+
private guard: MutexGuard<T>
|
|
424
501
|
|
|
502
|
+
/**
|
|
503
|
+
* A free lock that owns `init`.
|
|
504
|
+
*
|
|
505
|
+
* @param init the data the lock owns. It must be fresh all the way down: a
|
|
506
|
+
* scalar, or a `new` expression or a literal built only from fresh values
|
|
507
|
+
* (NL2463), so that nothing else holds a path to it that skips the lock.
|
|
508
|
+
*/
|
|
425
509
|
constructor(init: T) {
|
|
426
510
|
this.guard = new MutexGuard<T>(init)
|
|
427
511
|
}
|
|
@@ -429,6 +513,9 @@ export class Mutex<T> {
|
|
|
429
513
|
/**
|
|
430
514
|
* The guard over the data, for a `using` declaration: `using g = m.lock()`.
|
|
431
515
|
* Natively the call waits until the lock is free and takes it.
|
|
516
|
+
*
|
|
517
|
+
* @returns the lock's one guard, the same object on every call, through
|
|
518
|
+
* whose `value` the data is read and written.
|
|
432
519
|
*/
|
|
433
520
|
lock(): MutexGuard<T> {
|
|
434
521
|
return this.guard
|
|
@@ -452,37 +539,65 @@ export class Mutex<T> {
|
|
|
452
539
|
*/
|
|
453
540
|
export class AtomicI32 {
|
|
454
541
|
/** The value; named only by this class's methods, which the compiler emits. */
|
|
455
|
-
value: i32
|
|
542
|
+
private value: i32
|
|
456
543
|
|
|
544
|
+
/**
|
|
545
|
+
* An atomic holding `value`.
|
|
546
|
+
*
|
|
547
|
+
* @param value the starting value.
|
|
548
|
+
*/
|
|
457
549
|
constructor(value: i32) {
|
|
458
550
|
this.value = value
|
|
459
551
|
}
|
|
460
552
|
|
|
461
|
-
/**
|
|
553
|
+
/**
|
|
554
|
+
* The value.
|
|
555
|
+
*
|
|
556
|
+
* @returns the value at the moment of the read.
|
|
557
|
+
*/
|
|
462
558
|
load(): i32 {
|
|
463
559
|
return this.value
|
|
464
560
|
}
|
|
465
561
|
|
|
466
|
-
/**
|
|
562
|
+
/**
|
|
563
|
+
* Sets the value to `value`.
|
|
564
|
+
*
|
|
565
|
+
* @param value the new value.
|
|
566
|
+
*/
|
|
467
567
|
store(value: i32): void {
|
|
468
568
|
this.value = value
|
|
469
569
|
}
|
|
470
570
|
|
|
471
|
-
/**
|
|
571
|
+
/**
|
|
572
|
+
* Adds `delta`, wrapping, and answers the value before.
|
|
573
|
+
*
|
|
574
|
+
* @param delta how much to add; a negative one subtracts.
|
|
575
|
+
* @returns the value just before the addition.
|
|
576
|
+
*/
|
|
472
577
|
add(delta: i32): i32 {
|
|
473
578
|
const old = this.value
|
|
474
579
|
this.value = wrappingAdd(old, delta)
|
|
475
580
|
return old
|
|
476
581
|
}
|
|
477
582
|
|
|
478
|
-
/**
|
|
583
|
+
/**
|
|
584
|
+
* Subtracts `delta`, wrapping, and answers the value before.
|
|
585
|
+
*
|
|
586
|
+
* @param delta how much to subtract; a negative one adds.
|
|
587
|
+
* @returns the value just before the subtraction.
|
|
588
|
+
*/
|
|
479
589
|
sub(delta: i32): i32 {
|
|
480
590
|
const old = this.value
|
|
481
591
|
this.value = wrappingSub(old, delta)
|
|
482
592
|
return old
|
|
483
593
|
}
|
|
484
594
|
|
|
485
|
-
/**
|
|
595
|
+
/**
|
|
596
|
+
* Keeps the smaller of the value and `value`, and answers the value before.
|
|
597
|
+
*
|
|
598
|
+
* @param value the candidate, stored only when it is below the value.
|
|
599
|
+
* @returns the value just before the update.
|
|
600
|
+
*/
|
|
486
601
|
min(value: i32): i32 {
|
|
487
602
|
const old = this.value
|
|
488
603
|
if (value < old) {
|
|
@@ -491,7 +606,12 @@ export class AtomicI32 {
|
|
|
491
606
|
return old
|
|
492
607
|
}
|
|
493
608
|
|
|
494
|
-
/**
|
|
609
|
+
/**
|
|
610
|
+
* Keeps the larger of the value and `value`, and answers the value before.
|
|
611
|
+
*
|
|
612
|
+
* @param value the candidate, stored only when it is above the value.
|
|
613
|
+
* @returns the value just before the update.
|
|
614
|
+
*/
|
|
495
615
|
max(value: i32): i32 {
|
|
496
616
|
const old = this.value
|
|
497
617
|
if (value > old) {
|
|
@@ -500,14 +620,27 @@ export class AtomicI32 {
|
|
|
500
620
|
return old
|
|
501
621
|
}
|
|
502
622
|
|
|
503
|
-
/**
|
|
623
|
+
/**
|
|
624
|
+
* Sets the value to `value` and answers the value before.
|
|
625
|
+
*
|
|
626
|
+
* @param value the new value.
|
|
627
|
+
* @returns the value it replaced.
|
|
628
|
+
*/
|
|
504
629
|
exchange(value: i32): i32 {
|
|
505
630
|
const old = this.value
|
|
506
631
|
this.value = value
|
|
507
632
|
return old
|
|
508
633
|
}
|
|
509
634
|
|
|
510
|
-
/**
|
|
635
|
+
/**
|
|
636
|
+
* Sets the value to `desired` if it is `expected`, and answers the value it
|
|
637
|
+
* saw.
|
|
638
|
+
*
|
|
639
|
+
* @param expected the value the update requires.
|
|
640
|
+
* @param desired the value stored when the value is `expected`.
|
|
641
|
+
* @returns the value seen before the update: `expected` exactly when the
|
|
642
|
+
* update happened.
|
|
643
|
+
*/
|
|
511
644
|
compareExchange(expected: i32, desired: i32): i32 {
|
|
512
645
|
const old = this.value
|
|
513
646
|
if (old === expected) {
|
|
@@ -536,37 +669,67 @@ export class AtomicI32 {
|
|
|
536
669
|
*/
|
|
537
670
|
export class AtomicI64 {
|
|
538
671
|
/** The value; named only by this class's methods, which the compiler emits. */
|
|
539
|
-
value: i64
|
|
672
|
+
private value: i64
|
|
540
673
|
|
|
674
|
+
/**
|
|
675
|
+
* An atomic holding `value`.
|
|
676
|
+
*
|
|
677
|
+
* @param value the starting value.
|
|
678
|
+
*/
|
|
541
679
|
constructor(value: i64) {
|
|
542
680
|
this.value = value
|
|
543
681
|
}
|
|
544
682
|
|
|
545
|
-
/**
|
|
683
|
+
/**
|
|
684
|
+
* The value.
|
|
685
|
+
*
|
|
686
|
+
* @returns the value at the moment of the read.
|
|
687
|
+
*/
|
|
546
688
|
load(): i64 {
|
|
547
689
|
return this.value
|
|
548
690
|
}
|
|
549
691
|
|
|
550
|
-
/**
|
|
692
|
+
/**
|
|
693
|
+
* Sets the value to `value`.
|
|
694
|
+
*
|
|
695
|
+
* @param value the new value.
|
|
696
|
+
*/
|
|
551
697
|
store(value: i64): void {
|
|
552
698
|
this.value = value
|
|
553
699
|
}
|
|
554
700
|
|
|
555
|
-
/**
|
|
701
|
+
/**
|
|
702
|
+
* Adds `delta` and answers the value before; natively the sum wraps at 64
|
|
703
|
+
* bits.
|
|
704
|
+
*
|
|
705
|
+
* @param delta how much to add; a negative one subtracts.
|
|
706
|
+
* @returns the value just before the addition.
|
|
707
|
+
*/
|
|
556
708
|
add(delta: i64): i64 {
|
|
557
709
|
const old = this.value
|
|
558
710
|
this.value = old + delta
|
|
559
711
|
return old
|
|
560
712
|
}
|
|
561
713
|
|
|
562
|
-
/**
|
|
714
|
+
/**
|
|
715
|
+
* Subtracts `delta` and answers the value before; natively the difference
|
|
716
|
+
* wraps at 64 bits.
|
|
717
|
+
*
|
|
718
|
+
* @param delta how much to subtract; a negative one adds.
|
|
719
|
+
* @returns the value just before the subtraction.
|
|
720
|
+
*/
|
|
563
721
|
sub(delta: i64): i64 {
|
|
564
722
|
const old = this.value
|
|
565
723
|
this.value = old - delta
|
|
566
724
|
return old
|
|
567
725
|
}
|
|
568
726
|
|
|
569
|
-
/**
|
|
727
|
+
/**
|
|
728
|
+
* Keeps the smaller of the value and `value`, and answers the value before.
|
|
729
|
+
*
|
|
730
|
+
* @param value the candidate, stored only when it is below the value.
|
|
731
|
+
* @returns the value just before the update.
|
|
732
|
+
*/
|
|
570
733
|
min(value: i64): i64 {
|
|
571
734
|
const old = this.value
|
|
572
735
|
if (value < old) {
|
|
@@ -575,7 +738,12 @@ export class AtomicI64 {
|
|
|
575
738
|
return old
|
|
576
739
|
}
|
|
577
740
|
|
|
578
|
-
/**
|
|
741
|
+
/**
|
|
742
|
+
* Keeps the larger of the value and `value`, and answers the value before.
|
|
743
|
+
*
|
|
744
|
+
* @param value the candidate, stored only when it is above the value.
|
|
745
|
+
* @returns the value just before the update.
|
|
746
|
+
*/
|
|
579
747
|
max(value: i64): i64 {
|
|
580
748
|
const old = this.value
|
|
581
749
|
if (value > old) {
|
|
@@ -584,14 +752,27 @@ export class AtomicI64 {
|
|
|
584
752
|
return old
|
|
585
753
|
}
|
|
586
754
|
|
|
587
|
-
/**
|
|
755
|
+
/**
|
|
756
|
+
* Sets the value to `value` and answers the value before.
|
|
757
|
+
*
|
|
758
|
+
* @param value the new value.
|
|
759
|
+
* @returns the value it replaced.
|
|
760
|
+
*/
|
|
588
761
|
exchange(value: i64): i64 {
|
|
589
762
|
const old = this.value
|
|
590
763
|
this.value = value
|
|
591
764
|
return old
|
|
592
765
|
}
|
|
593
766
|
|
|
594
|
-
/**
|
|
767
|
+
/**
|
|
768
|
+
* Sets the value to `desired` if it is `expected`, and answers the value it
|
|
769
|
+
* saw.
|
|
770
|
+
*
|
|
771
|
+
* @param expected the value the update requires.
|
|
772
|
+
* @param desired the value stored when the value is `expected`.
|
|
773
|
+
* @returns the value seen before the update: `expected` exactly when the
|
|
774
|
+
* update happened.
|
|
775
|
+
*/
|
|
595
776
|
compareExchange(expected: i64, desired: i64): i64 {
|
|
596
777
|
const old = this.value
|
|
597
778
|
if (old === expected) {
|
|
@@ -628,27 +809,35 @@ export class AtomicI64 {
|
|
|
628
809
|
*/
|
|
629
810
|
export class Channel<T> {
|
|
630
811
|
/** Under Node: every value sent, in the order it was sent. */
|
|
631
|
-
queue: T[]
|
|
812
|
+
private queue: T[]
|
|
632
813
|
|
|
633
814
|
/** Under Node: how many of `queue`'s values a loop has taken. */
|
|
634
|
-
head: i32 = 0
|
|
815
|
+
private head: i32 = 0
|
|
635
816
|
|
|
636
817
|
/**
|
|
818
|
+
* @internal Read by nothing written here.
|
|
637
819
|
* Natively: the runtime's handle on the channel (runtime/runtime-parallel.c),
|
|
638
820
|
* 0 until the channel is first used. Nothing written here reads or writes it.
|
|
639
821
|
*/
|
|
640
822
|
state: i64 = 0
|
|
641
823
|
|
|
824
|
+
/** An empty channel, open until its scope joins. */
|
|
642
825
|
constructor() {
|
|
643
826
|
this.queue = []
|
|
644
827
|
}
|
|
645
828
|
|
|
646
|
-
/**
|
|
829
|
+
/**
|
|
830
|
+
* Hand `x` to the receiver. It never waits: a channel holds as many values
|
|
831
|
+
* as are sent.
|
|
832
|
+
*
|
|
833
|
+
* @param x the value to send. A string is copied out of the sender's arena.
|
|
834
|
+
*/
|
|
647
835
|
send(x: T): void {
|
|
648
836
|
this.queue.push(x)
|
|
649
837
|
}
|
|
650
838
|
|
|
651
839
|
/**
|
|
840
|
+
* @internal Called by `ChannelWalk`.
|
|
652
841
|
* The next value, for a loop that has found one waiting (`ChannelWalk`).
|
|
653
842
|
* Natively the loop is the compiler's, which takes a value with the runtime
|
|
654
843
|
* instead, and nothing calls this.
|
|
@@ -659,7 +848,7 @@ export class Channel<T> {
|
|
|
659
848
|
return x
|
|
660
849
|
}
|
|
661
850
|
|
|
662
|
-
/** Whether a value is waiting, under Node, where nothing is sent while a loop runs. */
|
|
851
|
+
/** @internal Called by `ChannelWalk`. Whether a value is waiting, under Node, where nothing is sent while a loop runs. */
|
|
663
852
|
ready(): boolean {
|
|
664
853
|
return this.head < toI32(this.queue.length)
|
|
665
854
|
}
|
|
@@ -668,6 +857,9 @@ export class Channel<T> {
|
|
|
668
857
|
* What `for (const x of ch)` asks for under Node: a walk that takes each
|
|
669
858
|
* value as it reaches it, so a second loop over the channel finds it empty,
|
|
670
859
|
* as it does natively.
|
|
860
|
+
*
|
|
861
|
+
* @returns A fresh walk over this channel; each step takes a value, and
|
|
862
|
+
* the walk ends at the first step that finds none waiting.
|
|
671
863
|
*/
|
|
672
864
|
[Symbol.iterator](): ChannelWalk<T> {
|
|
673
865
|
return new ChannelWalk<T>(this)
|
|
@@ -679,7 +871,7 @@ export class Channel<T> {
|
|
|
679
871
|
* never made: the compiler lowers the loop itself.
|
|
680
872
|
*/
|
|
681
873
|
class ChannelWalk<T> {
|
|
682
|
-
channel: Channel<T>
|
|
874
|
+
private channel: Channel<T>
|
|
683
875
|
|
|
684
876
|
constructor(channel: Channel<T>) {
|
|
685
877
|
this.channel = channel
|