@amritk/nish-x86_64-linux 0.34.1 → 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 +22 -13
- package/bin/nish +0 -0
- package/docs/AI.md +86 -16
- package/package.json +1 -1
- package/runtime/async.d.mts +49 -0
- package/runtime/async.mjs +27 -0
- package/runtime/nish.d.ts +4681 -427
- package/runtime/nish.h +6 -2
- package/runtime/nish.mjs +4 -0
- package/runtime/runtime-net.c +22 -0
- package/runtime/runtime-shm.c +93 -2
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/README.md +131 -131
- package/std/base64.ts +34 -5
- package/std/bytes.ts +258 -42
- 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 +297 -77
- package/std/net/http1-server.ts +430 -94
- 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 +275 -101
- 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 +801 -112
- 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 +264 -52
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +1124 -201
- 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 +430 -40
- package/std/sync/shm.ts +222 -15
- 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,20 +2,22 @@
|
|
|
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
|
|
18
|
-
* length rounded up to 4, plus 4
|
|
19
|
+
* length rounded up to 4, plus 4, and a record written with `reserve` may
|
|
20
|
+
* leave padding at a lap's end (a skip marker, below). `MpscRing` takes any number of producers
|
|
19
21
|
* and one consumer, in fixed slots of `slotBytes` each (Vyukov's bounded
|
|
20
22
|
* queue: a producer claims a slot with a compare-exchange on the enqueue
|
|
21
23
|
* position, and each slot's sequence number says whose turn it is). Neither
|
|
@@ -31,6 +33,44 @@
|
|
|
31
33
|
* multiple of 64 so the header's counters each have a cache line. Several
|
|
32
34
|
* rings may share a region at different bases.
|
|
33
35
|
*
|
|
36
|
+
* **In place.** An `SpscRing` producer may write a record into the ring
|
|
37
|
+
* itself instead of copying one in, and a consumer may read one there:
|
|
38
|
+
*
|
|
39
|
+
* const w: u8[] = ring.reserve(n) // empty when there is no room now
|
|
40
|
+
* copyBytes(w, 0, body, 0, k) // any u8[] writer, within w.length
|
|
41
|
+
* ring.commit(k) // publishes w[0, k) as one record
|
|
42
|
+
*
|
|
43
|
+
* const r: readonly u8[] = ring.peek() // the next record, in the ring
|
|
44
|
+
* if (r.length > 0) {
|
|
45
|
+
* use(r)
|
|
46
|
+
* ring.release() // frees it for the producer
|
|
47
|
+
* } else {
|
|
48
|
+
* ring.tryPop(buf, 0) // none, or one peek cannot lend
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* A window is a `SharedRegion.lend` window: the ring keeps one for its
|
|
52
|
+
* producer and one for its consumer and re-points them on every call, so
|
|
53
|
+
* neither path allocates or makes a system call. `commit`, `abandon` and
|
|
54
|
+
* `release` give the window back, which empties it: a reference kept past
|
|
55
|
+
* them indexes nothing (each index panics) until the next `reserve` or
|
|
56
|
+
* `peek` re-points it, and then sees that call's record. A reserved record
|
|
57
|
+
* is always contiguous: when the rest of the lap is too short, `commit`
|
|
58
|
+
* writes a skip marker there (a length word of -1, "the rest of this lap is
|
|
59
|
+
* padding") and the record starts at the next lap. Every pop steps over a
|
|
60
|
+
* marker. A record `tryPush` wrapped across the end cannot be lent, so `peek`
|
|
61
|
+
* answers an empty window for it and `tryPop` takes it, as it does for a
|
|
62
|
+
* record of length 0. A reservation is at most `capacity / 2 - 4` bytes, so
|
|
63
|
+
* a ring of 8 can never reserve (it panics) and one of 16 allows 4. The
|
|
64
|
+
* reserved bytes are not zeroed: `commit` only what was written. A consumer
|
|
65
|
+
* that polls `peek` on a doorbell ring has no wait call yet (`popWait` takes
|
|
66
|
+
* records, not windows).
|
|
67
|
+
*
|
|
68
|
+
* **The format keeps its magic.** A ring no producer `reserve`s into is
|
|
69
|
+
* byte for byte what it was, and a consumer built before the marker that
|
|
70
|
+
* meets one panics with "the ring is corrupt", never reads padding as a
|
|
71
|
+
* record. Both ends of a ring are normally one build, so a new magic would
|
|
72
|
+
* buy a clearer panic at the cost of every attach of an existing ring.
|
|
73
|
+
*
|
|
34
74
|
* **Who may call what is not checked.** An `SpscRing` with two producers, or
|
|
35
75
|
* either ring with two consumers, loses or repeats records; it never reads or
|
|
36
76
|
* writes outside its region, because every access is a checked `SharedRegion`
|
|
@@ -79,6 +119,8 @@ const RING_MPSC_MAGIC: i32 = 0x6e726d31 // "nrm1"
|
|
|
79
119
|
// of tens of microseconds. 256 put a ping-pong's p99 at 35 µs; 2048 put it at
|
|
80
120
|
// 1.4 µs, and 8192 bought nothing more (docs/wp47-shared-memory.md).
|
|
81
121
|
const RING_SPINS: i32 = 2048
|
|
122
|
+
// An SPSC length word that says the rest of the lap is padding.
|
|
123
|
+
const RING_SKIP: i32 = -1
|
|
82
124
|
// An MPSC slot: [seq i64][len i32][4 unused][payload].
|
|
83
125
|
const RING_SLOT_HEAD: i32 = 16
|
|
84
126
|
// The consumer's sleep flag at RING_SLEEP_AT.
|
|
@@ -100,10 +142,25 @@ const ringCheckPlace = (region: SharedRegion, base: i32, bytes: i64, what: strin
|
|
|
100
142
|
}
|
|
101
143
|
}
|
|
102
144
|
|
|
103
|
-
/**
|
|
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
|
+
*/
|
|
104
152
|
export const spscRingBytes = (capacity: i32): i32 => RING_DATA_AT + capacity
|
|
105
153
|
|
|
106
|
-
/**
|
|
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
|
+
*/
|
|
107
164
|
export const mpscRingBytes = (slots: i32, slotBytes: i32): i32 =>
|
|
108
165
|
RING_DATA_AT + slots * (RING_SLOT_HEAD + slotBytes)
|
|
109
166
|
|
|
@@ -161,21 +218,46 @@ const ringSleep = (region: SharedRegion, base: i32, ready: boolean, waitMs: i32)
|
|
|
161
218
|
*/
|
|
162
219
|
export class SpscRing {
|
|
163
220
|
/** The region the ring lives in. */
|
|
164
|
-
region: SharedRegion
|
|
221
|
+
private region: SharedRegion
|
|
165
222
|
/** Its header's offset in the region. */
|
|
166
|
-
base: i32
|
|
223
|
+
private base: i32
|
|
167
224
|
/** Data bytes, a power of two. */
|
|
168
225
|
capacity: i32
|
|
169
226
|
/** Whether a push rings the consumer's doorbell. */
|
|
170
|
-
doorbell: boolean
|
|
227
|
+
private doorbell: boolean
|
|
171
228
|
/** The producer's position: bytes ever pushed (this object's, as a producer). */
|
|
172
|
-
tail: i64
|
|
229
|
+
private tail: i64
|
|
173
230
|
/** The consumer's position: bytes ever popped (this object's, as a consumer). */
|
|
174
231
|
head: i64
|
|
175
232
|
/** The other side's position, as last loaded. */
|
|
176
|
-
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
|
+
*/
|
|
177
239
|
seenTail: i64
|
|
240
|
+
/** `reserve`'s window, re-pointed by each call; null until the first. */
|
|
241
|
+
private wnd: u8[] | null = null
|
|
242
|
+
/** `peek`'s window, likewise. */
|
|
243
|
+
private view: u8[] | null = null
|
|
244
|
+
/** The open reservation's bytes, or -1 for none. */
|
|
245
|
+
private reserved: i32 = -1
|
|
246
|
+
/** The padding the open reservation skips at the tail. */
|
|
247
|
+
private reservedPad: i32 = 0
|
|
248
|
+
/** The position past the record `peek` lent, or -1 for none. */
|
|
249
|
+
private peekedEnd: i64 = -1
|
|
250
|
+
/** The position of the record `lengthAt` last read, past any skip marker. */
|
|
251
|
+
private found: i64 = 0
|
|
178
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
|
+
*/
|
|
179
261
|
constructor(region: SharedRegion, base: i32) {
|
|
180
262
|
this.region = region
|
|
181
263
|
this.base = base
|
|
@@ -189,9 +271,21 @@ export class SpscRing {
|
|
|
189
271
|
|
|
190
272
|
/**
|
|
191
273
|
* Appends `src[off, off + len)` as one record: false when the ring has no
|
|
192
|
-
* room for it now. A record longer than the ring could ever hold panics
|
|
274
|
+
* room for it now. A record longer than the ring could ever hold panics,
|
|
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)
|
|
193
284
|
*/
|
|
194
285
|
tryPush(src: readonly u8[], off: i32, len: i32): boolean {
|
|
286
|
+
if (this.reserved >= 0) {
|
|
287
|
+
panic("nish/sync/ring: tryPush while a reservation is open; commit or abandon it first")
|
|
288
|
+
}
|
|
195
289
|
const need: i32 = ringRecordBytes(len)
|
|
196
290
|
if (len < 0 || need > this.capacity) {
|
|
197
291
|
panic(`nish/sync/ring: a record of ${len} bytes does not fit a ring of ${this.capacity}`)
|
|
@@ -213,6 +307,12 @@ export class SpscRing {
|
|
|
213
307
|
if (first < len) {
|
|
214
308
|
this.region.write(data, src, off + first, len - first)
|
|
215
309
|
}
|
|
310
|
+
this.publish(end)
|
|
311
|
+
return true
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Moves the producer's position to `end`, which hands the consumer every byte before it. */
|
|
315
|
+
private publish(end: i64): void {
|
|
216
316
|
this.tail = end
|
|
217
317
|
if (this.doorbell) {
|
|
218
318
|
this.region.storeI64(this.base + RING_TAIL_AT, end, SEQ_CST)
|
|
@@ -220,7 +320,105 @@ export class SpscRing {
|
|
|
220
320
|
} else {
|
|
221
321
|
this.region.storeI64(this.base + RING_TAIL_AT, end, RELEASE)
|
|
222
322
|
}
|
|
223
|
-
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** `window` re-pointed at no bytes: length 0, no allocation once it exists. */
|
|
326
|
+
private emptyWindow(window: u8[] | null): u8[] {
|
|
327
|
+
if (window !== null && window.length === 0) {
|
|
328
|
+
return window
|
|
329
|
+
}
|
|
330
|
+
const none: i32 = 0
|
|
331
|
+
const w: u8[] = this.region.lend(this.base + RING_DATA_AT, none, window)
|
|
332
|
+
this.region.giveBack(w)
|
|
333
|
+
return w
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* A window of `n` bytes of the ring's own memory to write a record into,
|
|
338
|
+
* contiguous, without blocking: empty (length 0) when the ring has no room
|
|
339
|
+
* for it now. Nothing is published until `commit`; `abandon` drops it. `n`
|
|
340
|
+
* is from 1 (a window of 0 could not be told from no room; `tryPush` takes
|
|
341
|
+
* an empty record) to `capacity / 2 - 4`, so an empty ring always has room
|
|
342
|
+
* for one; outside that, or while a reservation is open, it panics. A ring
|
|
343
|
+
* of 8 has no such `n` and always panics; one of 16 allows 4. The bytes
|
|
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
|
|
350
|
+
*/
|
|
351
|
+
reserve(n: i32): u8[] {
|
|
352
|
+
if (this.reserved >= 0) {
|
|
353
|
+
panic("nish/sync/ring: reserve while a reservation is open; commit or abandon it first")
|
|
354
|
+
}
|
|
355
|
+
const most: i32 = this.capacity / 2 - 4
|
|
356
|
+
if (most < 1) {
|
|
357
|
+
panic(
|
|
358
|
+
`nish/sync/ring: an SPSC ring of ${this.capacity} bytes cannot reserve; a reservation is at most half the ring less 4`
|
|
359
|
+
)
|
|
360
|
+
}
|
|
361
|
+
if (n < 1 || n > most) {
|
|
362
|
+
panic(`nish/sync/ring: a reservation of ${n} bytes is not from 1 to ${most}, half the ring less 4`)
|
|
363
|
+
}
|
|
364
|
+
const need: i32 = ringRecordBytes(n)
|
|
365
|
+
const at: i32 = toI32(this.tail & toI64(this.capacity - 1))
|
|
366
|
+
// The rest of the lap is padding when the record would cross the end.
|
|
367
|
+
const pad: i32 = need <= this.capacity - at ? 0 : this.capacity - at
|
|
368
|
+
const end: i64 = this.tail + toI64(pad + need)
|
|
369
|
+
if (end - this.seenHead > toI64(this.capacity)) {
|
|
370
|
+
this.seenHead = this.region.loadI64(this.base + RING_HEAD_AT, ACQUIRE)
|
|
371
|
+
if (end - this.seenHead > toI64(this.capacity)) {
|
|
372
|
+
const none: u8[] = this.emptyWindow(this.wnd)
|
|
373
|
+
this.wnd = none
|
|
374
|
+
return none
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
const from: i32 = pad > 0 ? 0 : at
|
|
378
|
+
const w: u8[] = this.region.lend(this.base + RING_DATA_AT + from + 4, n, this.wnd)
|
|
379
|
+
this.wnd = w
|
|
380
|
+
this.reserved = n
|
|
381
|
+
this.reservedPad = pad
|
|
382
|
+
return w
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Ends the open reservation, giving its window back; panics without one. */
|
|
386
|
+
private endReservation(what: string): void {
|
|
387
|
+
const w: u8[] | null = this.wnd
|
|
388
|
+
if (this.reserved < 0 || w === null) {
|
|
389
|
+
panic(`nish/sync/ring: ${what} without a reservation`)
|
|
390
|
+
}
|
|
391
|
+
this.region.giveBack(w)
|
|
392
|
+
this.reserved = -1
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Publishes the reserved window's first `len` bytes as one record, the
|
|
397
|
+
* store `tryPush` makes. The window is given back, so it is empty after.
|
|
398
|
+
* Panics without a reservation, for `len` outside `[0, n]`, or when a
|
|
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
|
|
402
|
+
*/
|
|
403
|
+
commit(len: i32): void {
|
|
404
|
+
const n: i32 = this.reserved
|
|
405
|
+
if (n >= 0 && (len < 0 || len > n)) {
|
|
406
|
+
panic(`nish/sync/ring: commit of ${len} bytes from a reservation of ${n}`)
|
|
407
|
+
}
|
|
408
|
+
this.endReservation("commit")
|
|
409
|
+
const mask: i32 = this.capacity - 1
|
|
410
|
+
const data: i32 = this.base + RING_DATA_AT
|
|
411
|
+
const at: i32 = toI32(this.tail & toI64(mask))
|
|
412
|
+
if (this.reservedPad > 0) {
|
|
413
|
+
this.region.storeI32(data + at, RING_SKIP, RELAXED)
|
|
414
|
+
}
|
|
415
|
+
this.region.storeI32(this.reservedPad > 0 ? data : data + at, len, RELAXED)
|
|
416
|
+
this.publish(this.tail + toI64(this.reservedPad + ringRecordBytes(len)))
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Drops the open reservation, publishing nothing; panics without one. */
|
|
420
|
+
abandon(): void {
|
|
421
|
+
this.endReservation("abandon")
|
|
224
422
|
}
|
|
225
423
|
|
|
226
424
|
/** Whether a record is queued, loading the producer's position if the cache says none. */
|
|
@@ -232,18 +430,36 @@ export class SpscRing {
|
|
|
232
430
|
return this.head !== this.seenTail
|
|
233
431
|
}
|
|
234
432
|
|
|
235
|
-
/**
|
|
236
|
-
|
|
433
|
+
/**
|
|
434
|
+
* The length of the record at `pos`, or at the next lap when a skip marker
|
|
435
|
+
* is at `pos`, checked against the ring and the producer's position `end`.
|
|
436
|
+
* `found` is set to where that record is.
|
|
437
|
+
*/
|
|
438
|
+
private lengthAt(pos: i64, end: i64): i32 {
|
|
237
439
|
const mask: i32 = this.capacity - 1
|
|
238
|
-
const
|
|
440
|
+
const data: i32 = this.base + RING_DATA_AT
|
|
441
|
+
let at: i64 = pos
|
|
442
|
+
let len: i32 = this.region.loadI32(data + toI32(pos & toI64(mask)), RELAXED)
|
|
443
|
+
if (len === RING_SKIP) {
|
|
444
|
+
at = pos + toI64(this.capacity - toI32(pos & toI64(mask)))
|
|
445
|
+
len = this.region.loadI32(data + toI32(at & toI64(mask)), RELAXED)
|
|
446
|
+
}
|
|
239
447
|
// The capacity first: a length a broken producer wrote near 2^31 would
|
|
240
448
|
// overflow the rounding below and panic with the wrong reason.
|
|
241
|
-
if (len < 0 || len > this.capacity - 4 || toI64(ringRecordBytes(len)) >
|
|
242
|
-
panic(`nish/sync/ring: a record of length ${len} at position ${
|
|
449
|
+
if (len < 0 || len > this.capacity - 4 || toI64(ringRecordBytes(len)) > end - at) {
|
|
450
|
+
panic(`nish/sync/ring: a record of length ${len} at position ${at}: the ring is corrupt`)
|
|
243
451
|
}
|
|
452
|
+
this.found = at
|
|
244
453
|
return len
|
|
245
454
|
}
|
|
246
455
|
|
|
456
|
+
/** Panics while `peek` has a record lent, which a pop would take from under it. */
|
|
457
|
+
private notPeeked(what: string): void {
|
|
458
|
+
if (this.peekedEnd >= toI64(0)) {
|
|
459
|
+
panic(`nish/sync/ring: ${what} while peek has a record lent; release it first`)
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
247
463
|
/** Copies the record at `pos`, `len` bytes, into `dst` at `off`. */
|
|
248
464
|
private copyOut(pos: i64, len: i32, dst: u8[], off: i32): void {
|
|
249
465
|
const mask: i32 = this.capacity - 1
|
|
@@ -259,18 +475,26 @@ export class SpscRing {
|
|
|
259
475
|
/**
|
|
260
476
|
* Moves the next record into `dst` from `off` and answers its length:
|
|
261
477
|
* `RING_EMPTY` when there is none, `RING_TOO_SMALL` (keeping it queued)
|
|
262
|
-
* when it is longer than `dst.length - off`.
|
|
478
|
+
* when it is longer than `dst.length - off`. Panics while `peek` has a
|
|
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`
|
|
263
486
|
*/
|
|
264
487
|
tryPop(dst: u8[], off: i32): i32 {
|
|
488
|
+
this.notPeeked("tryPop")
|
|
265
489
|
if (!this.ready(ACQUIRE)) {
|
|
266
490
|
return RING_EMPTY
|
|
267
491
|
}
|
|
268
|
-
const len: i32 = this.lengthAt(this.head, this.seenTail
|
|
492
|
+
const len: i32 = this.lengthAt(this.head, this.seenTail)
|
|
269
493
|
if (len > toI32(dst.length) - off) {
|
|
270
494
|
return RING_TOO_SMALL
|
|
271
495
|
}
|
|
272
|
-
this.copyOut(this.
|
|
273
|
-
this.head = this.
|
|
496
|
+
this.copyOut(this.found, len, dst, off)
|
|
497
|
+
this.head = this.found + toI64(ringRecordBytes(len))
|
|
274
498
|
this.region.storeI64(this.base + RING_HEAD_AT, this.head, RELEASE)
|
|
275
499
|
return len
|
|
276
500
|
}
|
|
@@ -279,9 +503,18 @@ export class SpscRing {
|
|
|
279
503
|
* Moves up to `lens.length` records into `dst`, back to back from 0, while
|
|
280
504
|
* they fit, and sets `lens[i]` to each one's length: how many it moved. One
|
|
281
505
|
* load of the producer's position and one store of the consumer's, however
|
|
282
|
-
* many records it moves.
|
|
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
|
|
283
515
|
*/
|
|
284
516
|
popBatch(dst: u8[], lens: i32[]): i32 {
|
|
517
|
+
this.notPeeked("popBatch")
|
|
285
518
|
// Always the producer's position now, not the cache: a cache that still
|
|
286
519
|
// holds a record would hide every one pushed since it was loaded.
|
|
287
520
|
this.seenTail = this.region.loadI64(this.base + RING_TAIL_AT, ACQUIRE)
|
|
@@ -289,17 +522,17 @@ export class SpscRing {
|
|
|
289
522
|
let used: i32 = 0
|
|
290
523
|
let pos: i64 = this.head
|
|
291
524
|
while (count < toI32(lens.length) && pos !== this.seenTail) {
|
|
292
|
-
const len: i32 = this.lengthAt(pos, this.seenTail
|
|
525
|
+
const len: i32 = this.lengthAt(pos, this.seenTail)
|
|
293
526
|
if (len > toI32(dst.length) - used) {
|
|
294
527
|
break
|
|
295
528
|
}
|
|
296
|
-
this.copyOut(
|
|
529
|
+
this.copyOut(this.found, len, dst, used)
|
|
297
530
|
if (count >= 0 && count < toI32(lens.length)) {
|
|
298
531
|
lens[count] = len
|
|
299
532
|
}
|
|
300
533
|
count = count + 1
|
|
301
534
|
used = used + len
|
|
302
|
-
pos =
|
|
535
|
+
pos = this.found + toI64(ringRecordBytes(len))
|
|
303
536
|
}
|
|
304
537
|
if (count > 0) {
|
|
305
538
|
this.head = pos
|
|
@@ -313,6 +546,17 @@ export class SpscRing {
|
|
|
313
546
|
* push rings the doorbell, for at most `timeoutMs` in all (negative: no
|
|
314
547
|
* limit): `RING_EMPTY` only once that has passed. `SHM_UNSUPPORTED` for a
|
|
315
548
|
* ring without a doorbell, or off Linux, where the caller polls `tryPop`.
|
|
549
|
+
* Panics while `peek` has a record lent, from `tryPop`, so the message
|
|
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`
|
|
316
560
|
*/
|
|
317
561
|
popWait(dst: u8[], off: i32, timeoutMs: i32): i32 {
|
|
318
562
|
const deadline: i64 = ringDeadline(timeoutMs)
|
|
@@ -336,12 +580,74 @@ export class SpscRing {
|
|
|
336
580
|
}
|
|
337
581
|
}
|
|
338
582
|
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* The next record, lent in place as the ring's own bytes, without copying
|
|
586
|
+
* or blocking; `release` frees it for the producer. Empty (length 0) when
|
|
587
|
+
* there is none, and also for a record peek cannot lend: one `tryPush`
|
|
588
|
+
* wrapped across the end of the ring, or one of length 0. On an empty
|
|
589
|
+
* answer nothing is lent, and `tryPop` takes the record if there is one.
|
|
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
|
|
596
|
+
*/
|
|
597
|
+
peek(): readonly u8[] {
|
|
598
|
+
this.notPeeked("peek")
|
|
599
|
+
if (!this.ready(ACQUIRE)) {
|
|
600
|
+
const none: u8[] = this.emptyWindow(this.view)
|
|
601
|
+
this.view = none
|
|
602
|
+
return none
|
|
603
|
+
}
|
|
604
|
+
const len: i32 = this.lengthAt(this.head, this.seenTail)
|
|
605
|
+
const from: i32 = toI32((this.found + toI64(4)) & toI64(this.capacity - 1))
|
|
606
|
+
if (len === 0 || len > this.capacity - from) {
|
|
607
|
+
const none: u8[] = this.emptyWindow(this.view)
|
|
608
|
+
this.view = none
|
|
609
|
+
return none
|
|
610
|
+
}
|
|
611
|
+
const r: u8[] = this.region.lend(this.base + RING_DATA_AT + from, len, this.view)
|
|
612
|
+
this.view = r
|
|
613
|
+
this.peekedEnd = this.found + toI64(ringRecordBytes(len))
|
|
614
|
+
return r
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Frees the record `peek` lent, the store `tryPop` makes, and gives the
|
|
619
|
+
* window back, so it is empty after. Panics when no record is lent.
|
|
620
|
+
*/
|
|
621
|
+
release(): void {
|
|
622
|
+
const r: u8[] | null = this.view
|
|
623
|
+
if (this.peekedEnd < toI64(0) || r === null) {
|
|
624
|
+
panic("nish/sync/ring: release without a record peek lent")
|
|
625
|
+
}
|
|
626
|
+
this.region.giveBack(r)
|
|
627
|
+
this.head = this.peekedEnd
|
|
628
|
+
this.peekedEnd = -1
|
|
629
|
+
this.region.storeI64(this.base + RING_HEAD_AT, this.head, RELEASE)
|
|
630
|
+
}
|
|
339
631
|
}
|
|
340
632
|
|
|
341
633
|
/**
|
|
342
634
|
* Writes an `SpscRing`'s header at `base` and answers the ring: `capacity`
|
|
343
635
|
* data bytes, a power of two from 8, and `doorbell` for `popWait`. The
|
|
344
|
-
* 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
|
|
345
651
|
*/
|
|
346
652
|
export const initSpscRing = (region: SharedRegion, base: i32, capacity: i32, doorbell: boolean): SpscRing => {
|
|
347
653
|
if (capacity < 8 || !ringIsPowerOfTwo(capacity)) {
|
|
@@ -357,7 +663,18 @@ export const initSpscRing = (region: SharedRegion, base: i32, capacity: i32, doo
|
|
|
357
663
|
return new SpscRing(region, base)
|
|
358
664
|
}
|
|
359
665
|
|
|
360
|
-
/**
|
|
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
|
+
*/
|
|
361
678
|
export const attachSpscRing = (region: SharedRegion, base: i32): Result<SpscRing, string> => {
|
|
362
679
|
ringCheckPlace(region, base, toI64(RING_DATA_AT), "an SPSC ring")
|
|
363
680
|
if (region.loadI32(base + RING_MAGIC_AT, ACQUIRE) !== RING_SPSC_MAGIC) {
|
|
@@ -377,18 +694,26 @@ export const attachSpscRing = (region: SharedRegion, base: i32): Result<SpscRing
|
|
|
377
694
|
*/
|
|
378
695
|
export class MpscRing {
|
|
379
696
|
/** The region the ring lives in. */
|
|
380
|
-
region: SharedRegion
|
|
697
|
+
private region: SharedRegion
|
|
381
698
|
/** Its header's offset in the region. */
|
|
382
|
-
base: i32
|
|
699
|
+
private base: i32
|
|
383
700
|
/** Slots, a power of two. */
|
|
384
|
-
slots: i32
|
|
701
|
+
private slots: i32
|
|
385
702
|
/** Each slot's payload bytes, a multiple of 8. */
|
|
386
|
-
slotBytes: i32
|
|
703
|
+
private slotBytes: i32
|
|
387
704
|
/** Whether a push rings the consumer's doorbell. */
|
|
388
|
-
doorbell: boolean
|
|
705
|
+
private doorbell: boolean
|
|
389
706
|
/** The consumer's position: records ever popped (this object's, as the consumer). */
|
|
390
707
|
head: i64
|
|
391
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
|
+
*/
|
|
392
717
|
constructor(region: SharedRegion, base: i32) {
|
|
393
718
|
this.region = region
|
|
394
719
|
this.base = base
|
|
@@ -406,7 +731,17 @@ export class MpscRing {
|
|
|
406
731
|
|
|
407
732
|
/**
|
|
408
733
|
* Queues `src[off, off + len)` as one record: false when every slot is
|
|
409
|
-
* 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)
|
|
410
745
|
*/
|
|
411
746
|
tryPush(src: readonly u8[], off: i32, len: i32): boolean {
|
|
412
747
|
if (len < 0 || len > this.slotBytes) {
|
|
@@ -464,7 +799,13 @@ export class MpscRing {
|
|
|
464
799
|
/**
|
|
465
800
|
* Moves the next record into `dst` from `off` and answers its length:
|
|
466
801
|
* `RING_EMPTY` when there is none, `RING_TOO_SMALL` (keeping it queued)
|
|
467
|
-
* 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`
|
|
468
809
|
*/
|
|
469
810
|
tryPop(dst: u8[], off: i32): i32 {
|
|
470
811
|
if (!this.ready(ACQUIRE)) {
|
|
@@ -483,6 +824,15 @@ export class MpscRing {
|
|
|
483
824
|
/**
|
|
484
825
|
* Moves up to `lens.length` records into `dst`, back to back from 0, while
|
|
485
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
|
|
486
836
|
*/
|
|
487
837
|
popBatch(dst: u8[], lens: i32[]): i32 {
|
|
488
838
|
let count: i32 = 0
|
|
@@ -504,7 +854,20 @@ export class MpscRing {
|
|
|
504
854
|
return count
|
|
505
855
|
}
|
|
506
856
|
|
|
507
|
-
/**
|
|
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
|
+
*/
|
|
508
871
|
popWait(dst: u8[], off: i32, timeoutMs: i32): i32 {
|
|
509
872
|
const deadline: i64 = ringDeadline(timeoutMs)
|
|
510
873
|
while (true) {
|
|
@@ -532,7 +895,23 @@ export class MpscRing {
|
|
|
532
895
|
/**
|
|
533
896
|
* Writes an `MpscRing`'s header at `base` and answers the ring: `slots`
|
|
534
897
|
* slots, a power of two from 2, of `slotBytes` each, a multiple of 8 from 8,
|
|
535
|
-
* 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
|
|
536
915
|
*/
|
|
537
916
|
export const initMpscRing = (
|
|
538
917
|
region: SharedRegion,
|
|
@@ -566,7 +945,18 @@ export const initMpscRing = (
|
|
|
566
945
|
return new MpscRing(region, base)
|
|
567
946
|
}
|
|
568
947
|
|
|
569
|
-
/**
|
|
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
|
+
*/
|
|
570
960
|
export const attachMpscRing = (region: SharedRegion, base: i32): Result<MpscRing, string> => {
|
|
571
961
|
ringCheckPlace(region, base, toI64(RING_DATA_AT), "an MPSC ring")
|
|
572
962
|
if (region.loadI32(base + RING_MAGIC_AT, ACQUIRE) !== RING_MPSC_MAGIC) {
|