@amritk/nish-aarch64-linux 0.35.0 → 0.36.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 +6 -1
- package/bin/nish +0 -0
- package/docs/AI.md +9 -2
- package/package.json +1 -1
- package/runtime/nish.d.ts +4612 -430
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/base64.ts +34 -5
- package/std/bytes.ts +256 -40
- 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 +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- 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 +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- 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 +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +235 -27
- package/std/net/http1-server.ts +343 -85
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +246 -96
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +723 -103
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +228 -44
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +489 -171
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- 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 +205 -35
- package/std/sync/shm.ts +194 -16
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- package/std/yaml.ts +764 -384
package/std/sync/ring.ts
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
* `nish/sync/ring` — bounded lock-free queues of byte records in a shared
|
|
3
3
|
* region, for handing work between processes on different cores.
|
|
4
4
|
*
|
|
5
|
-
* import { createRegion,
|
|
5
|
+
* import { createRegion, openRegion } from "nish/sync/shm"
|
|
6
6
|
* import { initSpscRing, attachSpscRing, spscRingBytes } from "nish/sync/ring"
|
|
7
7
|
*
|
|
8
8
|
* const region = createRegion("jobs", spscRingBytes(65536)).expect("region")
|
|
9
9
|
* const ring = initSpscRing(region, 0, 65536, true)
|
|
10
|
-
* ring.tryPush(job, 0, job.length) // false when full
|
|
10
|
+
* ring.tryPush(job, 0, toI32(job.length)) // false when full
|
|
11
11
|
*
|
|
12
|
-
* // The other process
|
|
13
|
-
* const
|
|
14
|
-
* const
|
|
12
|
+
* // The other process opens the region by name and attaches:
|
|
13
|
+
* const view = openRegion("jobs").expect("open")
|
|
14
|
+
* const rx = attachSpscRing(view, 0).expect("ring")
|
|
15
|
+
* const n = rx.popWait(buf, 0, 1000) // the record's length, or RING_EMPTY
|
|
15
16
|
*
|
|
16
17
|
* **Two rings.** `SpscRing` has one producer and one consumer, and holds
|
|
17
18
|
* records of any length up to its capacity back to back: a record costs its
|
|
@@ -141,10 +142,25 @@ const ringCheckPlace = (region: SharedRegion, base: i32, bytes: i64, what: strin
|
|
|
141
142
|
}
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
/**
|
|
145
|
+
/**
|
|
146
|
+
* The bytes an `SpscRing` of `capacity` data bytes takes from its base: the
|
|
147
|
+
* 256-byte header and the data. Nothing is checked here; `initSpscRing`
|
|
148
|
+
* refuses a capacity that is not a power of two from 8.
|
|
149
|
+
* @param capacity the ring's data bytes, as `initSpscRing` will be given them
|
|
150
|
+
* @returns how many bytes from the ring's base the region must hold
|
|
151
|
+
*/
|
|
145
152
|
export const spscRingBytes = (capacity: i32): i32 => RING_DATA_AT + capacity
|
|
146
153
|
|
|
147
|
-
/**
|
|
154
|
+
/**
|
|
155
|
+
* The bytes an `MpscRing` of `slots` slots of `slotBytes` takes from its base:
|
|
156
|
+
* the 256-byte header and each slot's 16-byte head and payload. Nothing is
|
|
157
|
+
* checked here; `initMpscRing` refuses a shape it cannot make.
|
|
158
|
+
* @param slots how many records the ring holds at once, as `initMpscRing`
|
|
159
|
+
* will be given it
|
|
160
|
+
* @param slotBytes the longest record, in bytes, as `initMpscRing` will be
|
|
161
|
+
* given it
|
|
162
|
+
* @returns how many bytes from the ring's base the region must hold
|
|
163
|
+
*/
|
|
148
164
|
export const mpscRingBytes = (slots: i32, slotBytes: i32): i32 =>
|
|
149
165
|
RING_DATA_AT + slots * (RING_SLOT_HEAD + slotBytes)
|
|
150
166
|
|
|
@@ -202,33 +218,46 @@ const ringSleep = (region: SharedRegion, base: i32, ready: boolean, waitMs: i32)
|
|
|
202
218
|
*/
|
|
203
219
|
export class SpscRing {
|
|
204
220
|
/** The region the ring lives in. */
|
|
205
|
-
region: SharedRegion
|
|
221
|
+
private region: SharedRegion
|
|
206
222
|
/** Its header's offset in the region. */
|
|
207
|
-
base: i32
|
|
223
|
+
private base: i32
|
|
208
224
|
/** Data bytes, a power of two. */
|
|
209
225
|
capacity: i32
|
|
210
226
|
/** Whether a push rings the consumer's doorbell. */
|
|
211
|
-
doorbell: boolean
|
|
227
|
+
private doorbell: boolean
|
|
212
228
|
/** The producer's position: bytes ever pushed (this object's, as a producer). */
|
|
213
|
-
tail: i64
|
|
229
|
+
private tail: i64
|
|
214
230
|
/** The consumer's position: bytes ever popped (this object's, as a consumer). */
|
|
215
231
|
head: i64
|
|
216
232
|
/** The other side's position, as last loaded. */
|
|
217
|
-
seenHead: i64
|
|
233
|
+
private seenHead: i64
|
|
234
|
+
/**
|
|
235
|
+
* The producer's position as this object, consuming, last loaded it: the
|
|
236
|
+
* cache a pop compares `head` with before it loads the producer's counter
|
|
237
|
+
* again. A snapshot, possibly behind the shared counter.
|
|
238
|
+
*/
|
|
218
239
|
seenTail: i64
|
|
219
240
|
/** `reserve`'s window, re-pointed by each call; null until the first. */
|
|
220
|
-
wnd: u8[] | null = null
|
|
241
|
+
private wnd: u8[] | null = null
|
|
221
242
|
/** `peek`'s window, likewise. */
|
|
222
|
-
view: u8[] | null = null
|
|
243
|
+
private view: u8[] | null = null
|
|
223
244
|
/** The open reservation's bytes, or -1 for none. */
|
|
224
|
-
reserved: i32 = -1
|
|
245
|
+
private reserved: i32 = -1
|
|
225
246
|
/** The padding the open reservation skips at the tail. */
|
|
226
|
-
reservedPad: i32 = 0
|
|
247
|
+
private reservedPad: i32 = 0
|
|
227
248
|
/** The position past the record `peek` lent, or -1 for none. */
|
|
228
|
-
peekedEnd: i64 = -1
|
|
249
|
+
private peekedEnd: i64 = -1
|
|
229
250
|
/** The position of the record `lengthAt` last read, past any skip marker. */
|
|
230
|
-
found: i64 = 0
|
|
251
|
+
private found: i64 = 0
|
|
231
252
|
|
|
253
|
+
/**
|
|
254
|
+
* Reads the header of the ring at `base`: its capacity and doorbell, and
|
|
255
|
+
* both positions (each an `ACQUIRE` load). It checks nothing; a program
|
|
256
|
+
* gets its ring from `initSpscRing` or `attachSpscRing`, which check the
|
|
257
|
+
* place and the magic first.
|
|
258
|
+
* @param region the region the ring lives in
|
|
259
|
+
* @param base the byte offset of the ring's header in `region`
|
|
260
|
+
*/
|
|
232
261
|
constructor(region: SharedRegion, base: i32) {
|
|
233
262
|
this.region = region
|
|
234
263
|
this.base = base
|
|
@@ -243,7 +272,15 @@ export class SpscRing {
|
|
|
243
272
|
/**
|
|
244
273
|
* Appends `src[off, off + len)` as one record: false when the ring has no
|
|
245
274
|
* room for it now. A record longer than the ring could ever hold panics,
|
|
246
|
-
* and so does a call while a reservation is open.
|
|
275
|
+
* and so does a call while a reservation is open. The bytes are plain
|
|
276
|
+
* copies, published to the consumer by one store of the producer's position:
|
|
277
|
+
* `RELEASE`, or `SEQ_CST` followed by the doorbell check on a doorbell ring.
|
|
278
|
+
* @param src the bytes to queue
|
|
279
|
+
* @param off where in `src` the record starts
|
|
280
|
+
* @param len the record's length in bytes, from 0; its length rounded up to
|
|
281
|
+
* 4, plus 4, may not exceed `capacity`. A range outside `src` panics
|
|
282
|
+
* @returns true when the record is queued, false when the ring is too full
|
|
283
|
+
* for it now (nothing is written)
|
|
247
284
|
*/
|
|
248
285
|
tryPush(src: readonly u8[], off: i32, len: i32): boolean {
|
|
249
286
|
if (this.reserved >= 0) {
|
|
@@ -305,6 +342,11 @@ export class SpscRing {
|
|
|
305
342
|
* for one; outside that, or while a reservation is open, it panics. A ring
|
|
306
343
|
* of 8 has no such `n` and always panics; one of 16 allows 4. The bytes
|
|
307
344
|
* are not zeroed: they hold whatever the ring last held there.
|
|
345
|
+
* @param n the most bytes the record may have; `commit` may publish fewer
|
|
346
|
+
* @returns the producer's window, the same array on every call after the
|
|
347
|
+
* first and re-pointed by each: its bytes are the ring's own memory, so
|
|
348
|
+
* what is written into it lands in the region, which the consumer may
|
|
349
|
+
* read once `commit` publishes it. Length `n`, or 0 when there is no room
|
|
308
350
|
*/
|
|
309
351
|
reserve(n: i32): u8[] {
|
|
310
352
|
if (this.reserved >= 0) {
|
|
@@ -355,6 +397,8 @@ export class SpscRing {
|
|
|
355
397
|
* store `tryPush` makes. The window is given back, so it is empty after.
|
|
356
398
|
* Panics without a reservation, for `len` outside `[0, n]`, or when a
|
|
357
399
|
* `push` moved the window out of the ring.
|
|
400
|
+
* @param len how many of the window's bytes, from its start, make the
|
|
401
|
+
* record: from 0 to the `n` that `reserve` was given
|
|
358
402
|
*/
|
|
359
403
|
commit(len: i32): void {
|
|
360
404
|
const n: i32 = this.reserved
|
|
@@ -432,7 +476,13 @@ export class SpscRing {
|
|
|
432
476
|
* Moves the next record into `dst` from `off` and answers its length:
|
|
433
477
|
* `RING_EMPTY` when there is none, `RING_TOO_SMALL` (keeping it queued)
|
|
434
478
|
* when it is longer than `dst.length - off`. Panics while `peek` has a
|
|
435
|
-
* record lent.
|
|
479
|
+
* record lent. The producer's position is loaded `ACQUIRE` (only when the
|
|
480
|
+
* cache says the ring is empty), and the freed space is handed back with a
|
|
481
|
+
* `RELEASE` store of the consumer's position.
|
|
482
|
+
* @param dst the buffer the record is copied into
|
|
483
|
+
* @param off where in `dst` the record's first byte goes
|
|
484
|
+
* @returns the record's length in bytes, or `RING_EMPTY`, or
|
|
485
|
+
* `RING_TOO_SMALL`
|
|
436
486
|
*/
|
|
437
487
|
tryPop(dst: u8[], off: i32): i32 {
|
|
438
488
|
this.notPeeked("tryPop")
|
|
@@ -453,7 +503,15 @@ export class SpscRing {
|
|
|
453
503
|
* Moves up to `lens.length` records into `dst`, back to back from 0, while
|
|
454
504
|
* they fit, and sets `lens[i]` to each one's length: how many it moved. One
|
|
455
505
|
* load of the producer's position and one store of the consumer's, however
|
|
456
|
-
* many records it moves. Panics while `peek` has a record lent.
|
|
506
|
+
* many records it moves. Panics while `peek` has a record lent. The load is
|
|
507
|
+
* `ACQUIRE` and the store, made only when a record moved, is `RELEASE`.
|
|
508
|
+
* @param dst the buffer the records are copied into, with no gap between
|
|
509
|
+
* them; the first record that does not fit the room left ends the batch
|
|
510
|
+
* and stays queued
|
|
511
|
+
* @param lens where each moved record's length is written, from index 0;
|
|
512
|
+
* its length is the most records one call moves
|
|
513
|
+
* @returns how many records it moved, 0 when the ring is empty or the next
|
|
514
|
+
* record does not fit `dst`; `lens[0, count)` holds their lengths
|
|
457
515
|
*/
|
|
458
516
|
popBatch(dst: u8[], lens: i32[]): i32 {
|
|
459
517
|
this.notPeeked("popBatch")
|
|
@@ -489,7 +547,16 @@ export class SpscRing {
|
|
|
489
547
|
* limit): `RING_EMPTY` only once that has passed. `SHM_UNSUPPORTED` for a
|
|
490
548
|
* ring without a doorbell, or off Linux, where the caller polls `tryPop`.
|
|
491
549
|
* Panics while `peek` has a record lent, from `tryPop`, so the message
|
|
492
|
-
* names `tryPop`.
|
|
550
|
+
* names `tryPop`. The deadline is checked between rounds of polling, so a
|
|
551
|
+
* wait can run past it by one round; a ring without a doorbell still polls
|
|
552
|
+
* one round before it answers `SHM_UNSUPPORTED`.
|
|
553
|
+
* @param dst the buffer the record is copied into
|
|
554
|
+
* @param off where in `dst` the record's first byte goes
|
|
555
|
+
* @param timeoutMs the longest wait in milliseconds, from the call; 0 polls
|
|
556
|
+
* one round, negative waits with no limit
|
|
557
|
+
* @returns what `tryPop` answers for the record (its length, or
|
|
558
|
+
* `RING_TOO_SMALL`), `RING_EMPTY` when the time ran out, or
|
|
559
|
+
* `SHM_UNSUPPORTED`
|
|
493
560
|
*/
|
|
494
561
|
popWait(dst: u8[], off: i32, timeoutMs: i32): i32 {
|
|
495
562
|
const deadline: i64 = ringDeadline(timeoutMs)
|
|
@@ -520,7 +587,12 @@ export class SpscRing {
|
|
|
520
587
|
* there is none, and also for a record peek cannot lend: one `tryPush`
|
|
521
588
|
* wrapped across the end of the ring, or one of length 0. On an empty
|
|
522
589
|
* answer nothing is lent, and `tryPop` takes the record if there is one.
|
|
523
|
-
* Panics while a record is lent.
|
|
590
|
+
* Panics while a record is lent. The producer's position is loaded
|
|
591
|
+
* `ACQUIRE` when the cache says the ring is empty.
|
|
592
|
+
* @returns the consumer's window, the same array on every call after the
|
|
593
|
+
* first and re-pointed by each: its bytes are the ring's own memory, not a
|
|
594
|
+
* copy, and stay the record's until `release`, which empties the window.
|
|
595
|
+
* Length 0 when nothing is lent
|
|
524
596
|
*/
|
|
525
597
|
peek(): readonly u8[] {
|
|
526
598
|
this.notPeeked("peek")
|
|
@@ -561,7 +633,21 @@ export class SpscRing {
|
|
|
561
633
|
/**
|
|
562
634
|
* Writes an `SpscRing`'s header at `base` and answers the ring: `capacity`
|
|
563
635
|
* data bytes, a power of two from 8, and `doorbell` for `popWait`. The
|
|
564
|
-
* memory is the creator's to give; a ring already there is reset.
|
|
636
|
+
* memory is the creator's to give; a ring already there is reset. The header
|
|
637
|
+
* is stored `RELAXED` and its magic last with `RELEASE`, so an attach whose
|
|
638
|
+
* `ACQUIRE` load reads that store sees the header stored before it. A reset
|
|
639
|
+
* ring had the magic already, so an attach racing a reset has no such
|
|
640
|
+
* promise.
|
|
641
|
+
* @param region the region the ring lives in
|
|
642
|
+
* @param base the byte offset of the ring's header, a multiple of 64 with
|
|
643
|
+
* `spscRingBytes(capacity)` bytes of the region from it; anything else
|
|
644
|
+
* panics
|
|
645
|
+
* @param capacity the ring's data bytes, a power of two from 8 (anything else
|
|
646
|
+
* panics); a record costs its length rounded up to 4, plus 4
|
|
647
|
+
* @param doorbell whether a consumer's `popWait` may sleep on Linux's futex,
|
|
648
|
+
* at the cost of a `SEQ_CST` store and load on every push
|
|
649
|
+
* @returns a new `SpscRing` on the empty ring, for this process's producer
|
|
650
|
+
* or consumer
|
|
565
651
|
*/
|
|
566
652
|
export const initSpscRing = (region: SharedRegion, base: i32, capacity: i32, doorbell: boolean): SpscRing => {
|
|
567
653
|
if (capacity < 8 || !ringIsPowerOfTwo(capacity)) {
|
|
@@ -577,7 +663,18 @@ export const initSpscRing = (region: SharedRegion, base: i32, capacity: i32, doo
|
|
|
577
663
|
return new SpscRing(region, base)
|
|
578
664
|
}
|
|
579
665
|
|
|
580
|
-
/**
|
|
666
|
+
/**
|
|
667
|
+
* The `SpscRing` an `initSpscRing` wrote at `base`; an error when there is
|
|
668
|
+
* none. The magic is loaded `ACQUIRE`, which pairs with the `RELEASE` store
|
|
669
|
+
* of it that the init function makes last.
|
|
670
|
+
* @param region the region the ring lives in, as this process mapped it
|
|
671
|
+
* @param base the byte offset of the ring's header, a multiple of 64 inside
|
|
672
|
+
* the region; anything else panics, and so does a ring whose data would run
|
|
673
|
+
* past the region's end
|
|
674
|
+
* @returns a new `SpscRing` that starts from both positions as the header
|
|
675
|
+
* holds them now, or the reason there is no ring at `base`: no SPSC magic,
|
|
676
|
+
* or a capacity no `initSpscRing` writes
|
|
677
|
+
*/
|
|
581
678
|
export const attachSpscRing = (region: SharedRegion, base: i32): Result<SpscRing, string> => {
|
|
582
679
|
ringCheckPlace(region, base, toI64(RING_DATA_AT), "an SPSC ring")
|
|
583
680
|
if (region.loadI32(base + RING_MAGIC_AT, ACQUIRE) !== RING_SPSC_MAGIC) {
|
|
@@ -597,18 +694,26 @@ export const attachSpscRing = (region: SharedRegion, base: i32): Result<SpscRing
|
|
|
597
694
|
*/
|
|
598
695
|
export class MpscRing {
|
|
599
696
|
/** The region the ring lives in. */
|
|
600
|
-
region: SharedRegion
|
|
697
|
+
private region: SharedRegion
|
|
601
698
|
/** Its header's offset in the region. */
|
|
602
|
-
base: i32
|
|
699
|
+
private base: i32
|
|
603
700
|
/** Slots, a power of two. */
|
|
604
|
-
slots: i32
|
|
701
|
+
private slots: i32
|
|
605
702
|
/** Each slot's payload bytes, a multiple of 8. */
|
|
606
|
-
slotBytes: i32
|
|
703
|
+
private slotBytes: i32
|
|
607
704
|
/** Whether a push rings the consumer's doorbell. */
|
|
608
|
-
doorbell: boolean
|
|
705
|
+
private doorbell: boolean
|
|
609
706
|
/** The consumer's position: records ever popped (this object's, as the consumer). */
|
|
610
707
|
head: i64
|
|
611
708
|
|
|
709
|
+
/**
|
|
710
|
+
* Reads the header of the ring at `base`: its shape, its doorbell and the
|
|
711
|
+
* consumer's position (an `ACQUIRE` load). It checks nothing; a program
|
|
712
|
+
* gets its ring from `initMpscRing` or `attachMpscRing`, which check the
|
|
713
|
+
* place and the magic first.
|
|
714
|
+
* @param region the region the ring lives in
|
|
715
|
+
* @param base the byte offset of the ring's header in `region`
|
|
716
|
+
*/
|
|
612
717
|
constructor(region: SharedRegion, base: i32) {
|
|
613
718
|
this.region = region
|
|
614
719
|
this.base = base
|
|
@@ -626,7 +731,17 @@ export class MpscRing {
|
|
|
626
731
|
|
|
627
732
|
/**
|
|
628
733
|
* Queues `src[off, off + len)` as one record: false when every slot is
|
|
629
|
-
* taken now. A record longer than a slot panics.
|
|
734
|
+
* taken now. A record longer than a slot panics. The producer claims a slot
|
|
735
|
+
* with a `RELAXED` compare-exchange on the enqueue position, after an
|
|
736
|
+
* `ACQUIRE` load of the slot's sequence number; it copies the record in and
|
|
737
|
+
* publishes it with one store of that number, `RELEASE`, or `SEQ_CST`
|
|
738
|
+
* followed by the doorbell check on a doorbell ring.
|
|
739
|
+
* @param src the bytes to queue
|
|
740
|
+
* @param off where in `src` the record starts
|
|
741
|
+
* @param len the record's length in bytes, from 0 to `slotBytes`; a range
|
|
742
|
+
* outside `src` panics
|
|
743
|
+
* @returns true when the record is queued, false when the slot it would
|
|
744
|
+
* take has not been freed by the consumer yet (nothing is written)
|
|
630
745
|
*/
|
|
631
746
|
tryPush(src: readonly u8[], off: i32, len: i32): boolean {
|
|
632
747
|
if (len < 0 || len > this.slotBytes) {
|
|
@@ -684,7 +799,13 @@ export class MpscRing {
|
|
|
684
799
|
/**
|
|
685
800
|
* Moves the next record into `dst` from `off` and answers its length:
|
|
686
801
|
* `RING_EMPTY` when there is none, `RING_TOO_SMALL` (keeping it queued)
|
|
687
|
-
* when it is longer than `dst.length - off`.
|
|
802
|
+
* when it is longer than `dst.length - off`. The slot's sequence number is
|
|
803
|
+
* loaded `ACQUIRE`, and the slot is freed for the producers with a
|
|
804
|
+
* `RELEASE` store of it.
|
|
805
|
+
* @param dst the buffer the record is copied into
|
|
806
|
+
* @param off where in `dst` the record's first byte goes
|
|
807
|
+
* @returns the record's length in bytes, or `RING_EMPTY`, or
|
|
808
|
+
* `RING_TOO_SMALL`
|
|
688
809
|
*/
|
|
689
810
|
tryPop(dst: u8[], off: i32): i32 {
|
|
690
811
|
if (!this.ready(ACQUIRE)) {
|
|
@@ -703,6 +824,15 @@ export class MpscRing {
|
|
|
703
824
|
/**
|
|
704
825
|
* Moves up to `lens.length` records into `dst`, back to back from 0, while
|
|
705
826
|
* they fit, and sets `lens[i]` to each one's length: how many it moved.
|
|
827
|
+
* Unlike `SpscRing.popBatch` it costs what as many `tryPop`s would: an
|
|
828
|
+
* `ACQUIRE` load and a `RELEASE` store per record.
|
|
829
|
+
* @param dst the buffer the records are copied into, with no gap between
|
|
830
|
+
* them; the first record that does not fit the room left ends the batch
|
|
831
|
+
* and stays queued
|
|
832
|
+
* @param lens where each moved record's length is written, from index 0;
|
|
833
|
+
* its length is the most records one call moves
|
|
834
|
+
* @returns how many records it moved, 0 when the ring is empty or the next
|
|
835
|
+
* record does not fit `dst`; `lens[0, count)` holds their lengths
|
|
706
836
|
*/
|
|
707
837
|
popBatch(dst: u8[], lens: i32[]): i32 {
|
|
708
838
|
let count: i32 = 0
|
|
@@ -724,7 +854,20 @@ export class MpscRing {
|
|
|
724
854
|
return count
|
|
725
855
|
}
|
|
726
856
|
|
|
727
|
-
/**
|
|
857
|
+
/**
|
|
858
|
+
* `SpscRing.popWait`, for this ring: `tryPop`, but on an empty ring it polls
|
|
859
|
+
* briefly and then sleeps until a push rings the doorbell, for at most
|
|
860
|
+
* `timeoutMs`, checked between rounds of polling. A ring without a doorbell,
|
|
861
|
+
* or one off Linux, answers `SHM_UNSUPPORTED` after a round that finds
|
|
862
|
+
* nothing.
|
|
863
|
+
* @param dst the buffer the record is copied into
|
|
864
|
+
* @param off where in `dst` the record's first byte goes
|
|
865
|
+
* @param timeoutMs the longest wait in milliseconds, from the call; 0 polls
|
|
866
|
+
* one round, negative waits with no limit
|
|
867
|
+
* @returns what `tryPop` answers for the record (its length, or
|
|
868
|
+
* `RING_TOO_SMALL`), `RING_EMPTY` when the time ran out, or
|
|
869
|
+
* `SHM_UNSUPPORTED`
|
|
870
|
+
*/
|
|
728
871
|
popWait(dst: u8[], off: i32, timeoutMs: i32): i32 {
|
|
729
872
|
const deadline: i64 = ringDeadline(timeoutMs)
|
|
730
873
|
while (true) {
|
|
@@ -752,7 +895,23 @@ export class MpscRing {
|
|
|
752
895
|
/**
|
|
753
896
|
* Writes an `MpscRing`'s header at `base` and answers the ring: `slots`
|
|
754
897
|
* slots, a power of two from 2, of `slotBytes` each, a multiple of 8 from 8,
|
|
755
|
-
* and `doorbell` for `popWait`. A ring already there is reset.
|
|
898
|
+
* and `doorbell` for `popWait`. A ring already there is reset. The header and
|
|
899
|
+
* every slot's sequence number are stored `RELAXED` and the magic last with
|
|
900
|
+
* `RELEASE`, so an attach whose `ACQUIRE` load reads that store sees them
|
|
901
|
+
* all. A reset ring had the magic already, so an attach racing a reset has
|
|
902
|
+
* no such promise.
|
|
903
|
+
* @param region the region the ring lives in
|
|
904
|
+
* @param base the byte offset of the ring's header, a multiple of 64 with
|
|
905
|
+
* `mpscRingBytes(slots, slotBytes)` bytes of the region from it; anything
|
|
906
|
+
* else panics
|
|
907
|
+
* @param slots how many records the ring holds at once, a power of two from
|
|
908
|
+
* 2; anything else panics
|
|
909
|
+
* @param slotBytes the longest record, in bytes, a multiple of 8 from 8;
|
|
910
|
+
* anything else panics
|
|
911
|
+
* @param doorbell whether a consumer's `popWait` may sleep on Linux's futex,
|
|
912
|
+
* at the cost of a `SEQ_CST` store and load on every push
|
|
913
|
+
* @returns a new `MpscRing` on the empty ring, for this process's producer
|
|
914
|
+
* or consumer
|
|
756
915
|
*/
|
|
757
916
|
export const initMpscRing = (
|
|
758
917
|
region: SharedRegion,
|
|
@@ -786,7 +945,18 @@ export const initMpscRing = (
|
|
|
786
945
|
return new MpscRing(region, base)
|
|
787
946
|
}
|
|
788
947
|
|
|
789
|
-
/**
|
|
948
|
+
/**
|
|
949
|
+
* The `MpscRing` an `initMpscRing` wrote at `base`; an error when there is
|
|
950
|
+
* none. The magic is loaded `ACQUIRE`, which pairs with the `RELEASE` store
|
|
951
|
+
* of it that the init function makes last.
|
|
952
|
+
* @param region the region the ring lives in, as this process mapped it
|
|
953
|
+
* @param base the byte offset of the ring's header, a multiple of 64 inside
|
|
954
|
+
* the region; anything else panics, and so does a ring whose slots would
|
|
955
|
+
* run past the region's end
|
|
956
|
+
* @returns a new `MpscRing` that starts from the consumer's position as the
|
|
957
|
+
* header holds it now, or the reason there is no ring at `base`: no MPSC
|
|
958
|
+
* magic, or a shape no `initMpscRing` writes
|
|
959
|
+
*/
|
|
790
960
|
export const attachMpscRing = (region: SharedRegion, base: i32): Result<MpscRing, string> => {
|
|
791
961
|
ringCheckPlace(region, base, toI64(RING_DATA_AT), "an MPSC ring")
|
|
792
962
|
if (region.loadI32(base + RING_MAGIC_AT, ACQUIRE) !== RING_MPSC_MAGIC) {
|