nish 0.42.0 → 0.45.1
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/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/skills/writing-nish/SKILL.md +12 -4
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/sync/ring.ts
CHANGED
|
@@ -104,6 +104,16 @@ import { monotonicNanos } from "nish:process"
|
|
|
104
104
|
export const RING_EMPTY: i32 = -1
|
|
105
105
|
/** `tryPop`, `popWait`: the next record is longer than the room after `off` in `dst`; it stays queued. */
|
|
106
106
|
export const RING_TOO_SMALL: i32 = -2
|
|
107
|
+
/**
|
|
108
|
+
* `MpscRing.take`, `MpscRing.takeWait`: the consumer's slot holds a length no
|
|
109
|
+
* producer writes, so another process scribbled on the ring. Nothing is moved
|
|
110
|
+
* and the slot stays as it is; `tryPop` panics on it instead.
|
|
111
|
+
*/
|
|
112
|
+
export const RING_CORRUPT: i32 = -3
|
|
113
|
+
/** `MpscRing.offer`: every slot is taken now; nothing is written. */
|
|
114
|
+
export const RING_FULL: i32 = -4
|
|
115
|
+
/** `MpscRing.offer`: the record is longer than a slot, or its length is negative; nothing is written. */
|
|
116
|
+
export const RING_TOO_LONG: i32 = -5
|
|
107
117
|
|
|
108
118
|
// The header, from `base`. Each counter has a cache line of its own, so the
|
|
109
119
|
// producer's stores and the consumer's never contend for one.
|
|
@@ -132,16 +142,51 @@ const RING_ASLEEP: i32 = 1
|
|
|
132
142
|
|
|
133
143
|
const ringIsPowerOfTwo = (n: i32): boolean => n > 0 && (n & (n - 1)) === 0
|
|
134
144
|
|
|
145
|
+
/**
|
|
146
|
+
* An offset into the region, added in wrapping u32 so the sum is never a
|
|
147
|
+
* panic site. Every caller adds a header constant or a masked index to a base
|
|
148
|
+
* `ringFits` already placed, so the sum cannot pass the region's size; a
|
|
149
|
+
* region of 2 GiB or more is refused before any ring is made in it.
|
|
150
|
+
*/
|
|
151
|
+
const ringAdd = (a: i32, b: i32): i32 => toI32(toU32(a) + toU32(b))
|
|
152
|
+
|
|
153
|
+
/** The product of two ring sizes in wrapping u32; the shapes it is given were checked first. */
|
|
154
|
+
const ringMul = (a: i32, b: i32): i32 => toI32(toU32(a) * toU32(b))
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A ring position plus a count, in wrapping u64. Positions start at 0 and grow
|
|
158
|
+
* by at most a ring's size per record, so they reach 2^63 only after
|
|
159
|
+
* centuries of traffic; wrapping keeps the sum from being a panic site.
|
|
160
|
+
*/
|
|
161
|
+
const ringPos = (pos: i64, by: i64): i64 => toI64(toU64(pos) + toU64(by))
|
|
162
|
+
|
|
163
|
+
/** `a - b` in wrapping u32, for the same offsets `ringAdd` sums. */
|
|
164
|
+
const ringSub = (a: i32, b: i32): i32 => toI32(toU32(a) - toU32(b))
|
|
165
|
+
|
|
166
|
+
/** How far position `a` is past `b`, in wrapping u64, as `ringPos` moves them. */
|
|
167
|
+
const ringGap = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
|
|
168
|
+
|
|
135
169
|
/** The bytes an SPSC record of `len` takes: its length word and the payload, rounded up to 4. */
|
|
136
|
-
const ringRecordBytes = (len: i32): i32 => ((len + 3) & ~3)
|
|
170
|
+
const ringRecordBytes = (len: i32): i32 => ringAdd(toI32((toU32(len) + toU32(3)) & ~toU32(3)), 4)
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Whether a ring of `bytes` fits its region from `base` with its header
|
|
174
|
+
* 64-aligned. Attaching answers this as an `Err`, since the size it checks
|
|
175
|
+
* was read from shared memory; making a ring panics on it.
|
|
176
|
+
*/
|
|
177
|
+
const ringFits = (region: SharedRegion, base: i32, bytes: i64): boolean =>
|
|
178
|
+
base >= 0 && base % 64 === 0 && bytes >= toI64(0) && bytes <= toI64(region.size()) - toI64(base)
|
|
179
|
+
|
|
180
|
+
/** Why `ringFits` refuses a place, for the `Err` or the panic that reports it. */
|
|
181
|
+
const ringPlaceError = (region: SharedRegion, base: i32, bytes: i64, what: string): string =>
|
|
182
|
+
base < 0 || base % 64 !== 0
|
|
183
|
+
? `nish/sync/ring: ${what} at ${base}: a ring's base is a multiple of 64`
|
|
184
|
+
: `nish/sync/ring: ${what} at ${base} needs ${bytes} bytes; the region has ${region.size()}`
|
|
137
185
|
|
|
138
|
-
/**
|
|
186
|
+
/** `ringFits`, as the panic a ring maker's caller gets for a place it chose. */
|
|
139
187
|
const ringCheckPlace = (region: SharedRegion, base: i32, bytes: i64, what: string): void => {
|
|
140
|
-
if (
|
|
141
|
-
panic(
|
|
142
|
-
}
|
|
143
|
-
if (toI64(base) + bytes > toI64(region.size())) {
|
|
144
|
-
panic(`nish/sync/ring: ${what} at ${base} needs ${bytes} bytes; the region has ${region.size()}`)
|
|
188
|
+
if (!ringFits(region, base, bytes)) {
|
|
189
|
+
panic(ringPlaceError(region, base, bytes, what))
|
|
145
190
|
}
|
|
146
191
|
}
|
|
147
192
|
|
|
@@ -152,7 +197,7 @@ const ringCheckPlace = (region: SharedRegion, base: i32, bytes: i64, what: strin
|
|
|
152
197
|
* @param capacity the ring's data bytes, as `initSpscRing` will be given them
|
|
153
198
|
* @returns how many bytes from the ring's base the region must hold
|
|
154
199
|
*/
|
|
155
|
-
export const spscRingBytes = (capacity: i32): i32 => RING_DATA_AT
|
|
200
|
+
export const spscRingBytes = (capacity: i32): i32 => ringAdd(RING_DATA_AT, capacity)
|
|
156
201
|
|
|
157
202
|
/**
|
|
158
203
|
* The bytes an `MpscRing` of `slots` slots of `slotBytes` takes from its base:
|
|
@@ -165,7 +210,7 @@ export const spscRingBytes = (capacity: i32): i32 => RING_DATA_AT + capacity
|
|
|
165
210
|
* @returns how many bytes from the ring's base the region must hold
|
|
166
211
|
*/
|
|
167
212
|
export const mpscRingBytes = (slots: i32, slotBytes: i32): i32 =>
|
|
168
|
-
RING_DATA_AT
|
|
213
|
+
ringAdd(RING_DATA_AT, ringMul(slots, ringAdd(RING_SLOT_HEAD, slotBytes)))
|
|
169
214
|
|
|
170
215
|
/**
|
|
171
216
|
* Wakes a consumer that said it is sleeping. The caller has published its
|
|
@@ -174,7 +219,7 @@ export const mpscRingBytes = (slots: i32, slotBytes: i32): i32 =>
|
|
|
174
219
|
* producer of many the one that pays for the wake.
|
|
175
220
|
*/
|
|
176
221
|
const ringRing = (region: SharedRegion, base: i32): void => {
|
|
177
|
-
const at: i32 = base
|
|
222
|
+
const at: i32 = ringAdd(base, RING_SLEEP_AT)
|
|
178
223
|
if (
|
|
179
224
|
region.loadI32(at, SEQ_CST) !== RING_AWAKE &&
|
|
180
225
|
region.exchangeI32(at, RING_AWAKE, SEQ_CST) !== RING_AWAKE
|
|
@@ -185,15 +230,15 @@ const ringRing = (region: SharedRegion, base: i32): void => {
|
|
|
185
230
|
|
|
186
231
|
/** `monotonicNanos()` past which a wait of `timeoutMs` gives up; 0 for no limit. */
|
|
187
232
|
const ringDeadline = (timeoutMs: i32): i64 =>
|
|
188
|
-
timeoutMs < 0 ? toI64(0) : monotonicNanos()
|
|
233
|
+
timeoutMs < 0 ? toI64(0) : ringPos(monotonicNanos(), toI64(toU64(timeoutMs) * toU64(1000000)))
|
|
189
234
|
|
|
190
235
|
/** The milliseconds left before `deadline`, rounded up: -1 for no limit, 0 when it has passed. */
|
|
191
236
|
const ringLeftMs = (deadline: i64): i32 => {
|
|
192
237
|
if (deadline === toI64(0)) {
|
|
193
238
|
return -1
|
|
194
239
|
}
|
|
195
|
-
const left: i64 = deadline - monotonicNanos()
|
|
196
|
-
return left <= toI64(0) ? 0 : toI32((left + 999999) / 1000000)
|
|
240
|
+
const left: i64 = toI64(toU64(deadline) - toU64(monotonicNanos()))
|
|
241
|
+
return left <= toI64(0) ? 0 : toI32((toU64(left) + toU64(999999)) / toU64(1000000))
|
|
197
242
|
}
|
|
198
243
|
|
|
199
244
|
/**
|
|
@@ -202,7 +247,7 @@ const ringLeftMs = (deadline: i64): i32 => {
|
|
|
202
247
|
* sleep at most `waitMs` if none did. False when there is no futex here.
|
|
203
248
|
*/
|
|
204
249
|
const ringSleep = (region: SharedRegion, base: i32, ready: boolean, waitMs: i32): boolean => {
|
|
205
|
-
const at: i32 = base
|
|
250
|
+
const at: i32 = ringAdd(base, RING_SLEEP_AT)
|
|
206
251
|
if (ready) {
|
|
207
252
|
region.storeI32(at, RING_AWAKE, RELAXED)
|
|
208
253
|
return true
|
|
@@ -264,10 +309,10 @@ export class SpscRing {
|
|
|
264
309
|
constructor(region: SharedRegion, base: i32) {
|
|
265
310
|
this.region = region
|
|
266
311
|
this.base = base
|
|
267
|
-
this.capacity = region.loadI32(base
|
|
268
|
-
this.doorbell = region.loadI32(base
|
|
269
|
-
this.tail = region.loadI64(base
|
|
270
|
-
this.head = region.loadI64(base
|
|
312
|
+
this.capacity = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
|
|
313
|
+
this.doorbell = region.loadI32(ringAdd(base, RING_BELL_AT), RELAXED) !== 0
|
|
314
|
+
this.tail = region.loadI64(ringAdd(base, RING_TAIL_AT), ACQUIRE)
|
|
315
|
+
this.head = region.loadI64(ringAdd(base, RING_HEAD_AT), ACQUIRE)
|
|
271
316
|
this.seenHead = this.head
|
|
272
317
|
this.seenTail = this.tail
|
|
273
318
|
}
|
|
@@ -289,26 +334,28 @@ export class SpscRing {
|
|
|
289
334
|
if (this.reserved >= 0) {
|
|
290
335
|
panic("nish/sync/ring: tryPush while a reservation is open; commit or abandon it first")
|
|
291
336
|
}
|
|
337
|
+
// The length is tested before it is rounded: within 3 of 2^31 the rounded
|
|
338
|
+
// size wraps negative and would pass the size test alone.
|
|
292
339
|
const need: i32 = ringRecordBytes(len)
|
|
293
|
-
if (len < 0 || need > this.capacity) {
|
|
340
|
+
if (len < 0 || len > ringSub(this.capacity, 4) || need > this.capacity) {
|
|
294
341
|
panic(`nish/sync/ring: a record of ${len} bytes does not fit a ring of ${this.capacity}`)
|
|
295
342
|
}
|
|
296
|
-
const end: i64 = this.tail
|
|
297
|
-
if (end
|
|
298
|
-
this.seenHead = this.region.loadI64(this.base
|
|
299
|
-
if (end
|
|
343
|
+
const end: i64 = ringPos(this.tail, toI64(need))
|
|
344
|
+
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
345
|
+
this.seenHead = this.region.loadI64(ringAdd(this.base, RING_HEAD_AT), ACQUIRE)
|
|
346
|
+
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
300
347
|
return false
|
|
301
348
|
}
|
|
302
349
|
}
|
|
303
|
-
const mask: i32 = this.capacity
|
|
304
|
-
const data: i32 = this.base
|
|
350
|
+
const mask: i32 = ringSub(this.capacity, 1)
|
|
351
|
+
const data: i32 = ringAdd(this.base, RING_DATA_AT)
|
|
305
352
|
const at: i32 = toI32(this.tail & toI64(mask))
|
|
306
|
-
this.region.storeI32(data
|
|
307
|
-
const from: i32 = (at
|
|
308
|
-
const first: i32 = len < this.capacity
|
|
309
|
-
this.region.write(data
|
|
353
|
+
this.region.storeI32(ringAdd(data, at), len, RELAXED)
|
|
354
|
+
const from: i32 = ringAdd(at, 4) & mask
|
|
355
|
+
const first: i32 = len < ringSub(this.capacity, from) ? len : ringSub(this.capacity, from)
|
|
356
|
+
this.region.write(ringAdd(data, from), src, off, first)
|
|
310
357
|
if (first < len) {
|
|
311
|
-
this.region.write(data, src, off
|
|
358
|
+
this.region.write(data, src, ringAdd(off, first), ringSub(len, first))
|
|
312
359
|
}
|
|
313
360
|
this.publish(end)
|
|
314
361
|
return true
|
|
@@ -318,10 +365,10 @@ export class SpscRing {
|
|
|
318
365
|
private publish(end: i64): void {
|
|
319
366
|
this.tail = end
|
|
320
367
|
if (this.doorbell) {
|
|
321
|
-
this.region.storeI64(this.base
|
|
368
|
+
this.region.storeI64(ringAdd(this.base, RING_TAIL_AT), end, SEQ_CST)
|
|
322
369
|
ringRing(this.region, this.base)
|
|
323
370
|
} else {
|
|
324
|
-
this.region.storeI64(this.base
|
|
371
|
+
this.region.storeI64(ringAdd(this.base, RING_TAIL_AT), end, RELEASE)
|
|
325
372
|
}
|
|
326
373
|
}
|
|
327
374
|
|
|
@@ -331,7 +378,7 @@ export class SpscRing {
|
|
|
331
378
|
return window
|
|
332
379
|
}
|
|
333
380
|
const none: i32 = 0
|
|
334
|
-
const w: u8[] = this.region.lend(this.base
|
|
381
|
+
const w: u8[] = this.region.lend(ringAdd(this.base, RING_DATA_AT), none, window)
|
|
335
382
|
this.region.giveBack(w)
|
|
336
383
|
return w
|
|
337
384
|
}
|
|
@@ -355,7 +402,7 @@ export class SpscRing {
|
|
|
355
402
|
if (this.reserved >= 0) {
|
|
356
403
|
panic("nish/sync/ring: reserve while a reservation is open; commit or abandon it first")
|
|
357
404
|
}
|
|
358
|
-
const most: i32 = this.capacity / 2
|
|
405
|
+
const most: i32 = ringSub(this.capacity / 2, 4)
|
|
359
406
|
if (most < 1) {
|
|
360
407
|
panic(
|
|
361
408
|
`nish/sync/ring: an SPSC ring of ${this.capacity} bytes cannot reserve; a reservation is at most half the ring less 4`
|
|
@@ -365,20 +412,20 @@ export class SpscRing {
|
|
|
365
412
|
panic(`nish/sync/ring: a reservation of ${n} bytes is not from 1 to ${most}, half the ring less 4`)
|
|
366
413
|
}
|
|
367
414
|
const need: i32 = ringRecordBytes(n)
|
|
368
|
-
const at: i32 = toI32(this.tail & toI64(this.capacity
|
|
415
|
+
const at: i32 = toI32(this.tail & toI64(ringSub(this.capacity, 1)))
|
|
369
416
|
// The rest of the lap is padding when the record would cross the end.
|
|
370
|
-
const pad: i32 = need <= this.capacity
|
|
371
|
-
const end: i64 = this.tail
|
|
372
|
-
if (end
|
|
373
|
-
this.seenHead = this.region.loadI64(this.base
|
|
374
|
-
if (end
|
|
417
|
+
const pad: i32 = need <= ringSub(this.capacity, at) ? 0 : ringSub(this.capacity, at)
|
|
418
|
+
const end: i64 = ringPos(this.tail, toI64(ringAdd(pad, need)))
|
|
419
|
+
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
420
|
+
this.seenHead = this.region.loadI64(ringAdd(this.base, RING_HEAD_AT), ACQUIRE)
|
|
421
|
+
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
375
422
|
const none: u8[] = this.emptyWindow(this.wnd)
|
|
376
423
|
this.wnd = none
|
|
377
424
|
return none
|
|
378
425
|
}
|
|
379
426
|
}
|
|
380
427
|
const from: i32 = pad > 0 ? 0 : at
|
|
381
|
-
const w: u8[] = this.region.lend(this.base
|
|
428
|
+
const w: u8[] = this.region.lend(ringAdd(ringAdd(this.base, RING_DATA_AT), ringAdd(from, 4)), n, this.wnd)
|
|
382
429
|
this.wnd = w
|
|
383
430
|
this.reserved = n
|
|
384
431
|
this.reservedPad = pad
|
|
@@ -409,14 +456,14 @@ export class SpscRing {
|
|
|
409
456
|
panic(`nish/sync/ring: commit of ${len} bytes from a reservation of ${n}`)
|
|
410
457
|
}
|
|
411
458
|
this.endReservation("commit")
|
|
412
|
-
const mask: i32 = this.capacity
|
|
413
|
-
const data: i32 = this.base
|
|
459
|
+
const mask: i32 = ringSub(this.capacity, 1)
|
|
460
|
+
const data: i32 = ringAdd(this.base, RING_DATA_AT)
|
|
414
461
|
const at: i32 = toI32(this.tail & toI64(mask))
|
|
415
462
|
if (this.reservedPad > 0) {
|
|
416
|
-
this.region.storeI32(data
|
|
463
|
+
this.region.storeI32(ringAdd(data, at), RING_SKIP, RELAXED)
|
|
417
464
|
}
|
|
418
|
-
this.region.storeI32(this.reservedPad > 0 ? data : data
|
|
419
|
-
this.publish(this.tail
|
|
465
|
+
this.region.storeI32(this.reservedPad > 0 ? data : ringAdd(data, at), len, RELAXED)
|
|
466
|
+
this.publish(ringPos(this.tail, toI64(ringAdd(this.reservedPad, ringRecordBytes(len)))))
|
|
420
467
|
}
|
|
421
468
|
|
|
422
469
|
/** Drops the open reservation, publishing nothing; panics without one. */
|
|
@@ -429,7 +476,7 @@ export class SpscRing {
|
|
|
429
476
|
if (this.head !== this.seenTail) {
|
|
430
477
|
return true
|
|
431
478
|
}
|
|
432
|
-
this.seenTail = this.region.loadI64(this.base
|
|
479
|
+
this.seenTail = this.region.loadI64(ringAdd(this.base, RING_TAIL_AT), order)
|
|
433
480
|
return this.head !== this.seenTail
|
|
434
481
|
}
|
|
435
482
|
|
|
@@ -439,17 +486,17 @@ export class SpscRing {
|
|
|
439
486
|
* `found` is set to where that record is.
|
|
440
487
|
*/
|
|
441
488
|
private lengthAt(pos: i64, end: i64): i32 {
|
|
442
|
-
const mask: i32 = this.capacity
|
|
443
|
-
const data: i32 = this.base
|
|
489
|
+
const mask: i32 = ringSub(this.capacity, 1)
|
|
490
|
+
const data: i32 = ringAdd(this.base, RING_DATA_AT)
|
|
444
491
|
let at: i64 = pos
|
|
445
|
-
let len: i32 = this.region.loadI32(data
|
|
492
|
+
let len: i32 = this.region.loadI32(ringAdd(data, toI32(pos & toI64(mask))), RELAXED)
|
|
446
493
|
if (len === RING_SKIP) {
|
|
447
|
-
at = pos
|
|
448
|
-
len = this.region.loadI32(data
|
|
494
|
+
at = ringPos(pos, toI64(ringSub(this.capacity, toI32(pos & toI64(mask)))))
|
|
495
|
+
len = this.region.loadI32(ringAdd(data, toI32(at & toI64(mask))), RELAXED)
|
|
449
496
|
}
|
|
450
497
|
// The capacity first: a length a broken producer wrote near 2^31 would
|
|
451
498
|
// overflow the rounding below and panic with the wrong reason.
|
|
452
|
-
if (len < 0 || len > this.capacity
|
|
499
|
+
if (len < 0 || len > ringSub(this.capacity, 4) || toI64(ringRecordBytes(len)) > ringGap(end, at)) {
|
|
453
500
|
panic(`nish/sync/ring: a record of length ${len} at position ${at}: the ring is corrupt`)
|
|
454
501
|
}
|
|
455
502
|
this.found = at
|
|
@@ -465,13 +512,13 @@ export class SpscRing {
|
|
|
465
512
|
|
|
466
513
|
/** Copies the record at `pos`, `len` bytes, into `dst` at `off`. */
|
|
467
514
|
private copyOut(pos: i64, len: i32, dst: u8[], off: i32): void {
|
|
468
|
-
const mask: i32 = this.capacity
|
|
469
|
-
const data: i32 = this.base
|
|
470
|
-
const from: i32 = toI32((pos
|
|
471
|
-
const first: i32 = len < this.capacity
|
|
472
|
-
this.region.read(data
|
|
515
|
+
const mask: i32 = ringSub(this.capacity, 1)
|
|
516
|
+
const data: i32 = ringAdd(this.base, RING_DATA_AT)
|
|
517
|
+
const from: i32 = toI32(ringPos(pos, toI64(4)) & toI64(mask))
|
|
518
|
+
const first: i32 = len < ringSub(this.capacity, from) ? len : ringSub(this.capacity, from)
|
|
519
|
+
this.region.read(ringAdd(data, from), dst, off, first)
|
|
473
520
|
if (first < len) {
|
|
474
|
-
this.region.read(data, dst, off
|
|
521
|
+
this.region.read(data, dst, ringAdd(off, first), ringSub(len, first))
|
|
475
522
|
}
|
|
476
523
|
}
|
|
477
524
|
|
|
@@ -493,12 +540,12 @@ export class SpscRing {
|
|
|
493
540
|
return RING_EMPTY
|
|
494
541
|
}
|
|
495
542
|
const len: i32 = this.lengthAt(this.head, this.seenTail)
|
|
496
|
-
if (len > toI32(dst.length)
|
|
543
|
+
if (len > ringSub(toI32(dst.length), off)) {
|
|
497
544
|
return RING_TOO_SMALL
|
|
498
545
|
}
|
|
499
546
|
this.copyOut(this.found, len, dst, off)
|
|
500
|
-
this.head = this.found
|
|
501
|
-
this.region.storeI64(this.base
|
|
547
|
+
this.head = ringPos(this.found, toI64(ringRecordBytes(len)))
|
|
548
|
+
this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), this.head, RELEASE)
|
|
502
549
|
return len
|
|
503
550
|
}
|
|
504
551
|
|
|
@@ -520,13 +567,13 @@ export class SpscRing {
|
|
|
520
567
|
this.notPeeked("popBatch")
|
|
521
568
|
// Always the producer's position now, not the cache: a cache that still
|
|
522
569
|
// holds a record would hide every one pushed since it was loaded.
|
|
523
|
-
this.seenTail = this.region.loadI64(this.base
|
|
570
|
+
this.seenTail = this.region.loadI64(ringAdd(this.base, RING_TAIL_AT), ACQUIRE)
|
|
524
571
|
let count: i32 = 0
|
|
525
572
|
let used: i32 = 0
|
|
526
573
|
let pos: i64 = this.head
|
|
527
574
|
while (count < toI32(lens.length) && pos !== this.seenTail) {
|
|
528
575
|
const len: i32 = this.lengthAt(pos, this.seenTail)
|
|
529
|
-
if (len > toI32(dst.length)
|
|
576
|
+
if (len > ringSub(toI32(dst.length), used)) {
|
|
530
577
|
break
|
|
531
578
|
}
|
|
532
579
|
this.copyOut(this.found, len, dst, used)
|
|
@@ -534,12 +581,12 @@ export class SpscRing {
|
|
|
534
581
|
lens[count] = len
|
|
535
582
|
}
|
|
536
583
|
count = count + 1
|
|
537
|
-
used = used
|
|
538
|
-
pos = this.found
|
|
584
|
+
used = ringAdd(used, len)
|
|
585
|
+
pos = ringPos(this.found, toI64(ringRecordBytes(len)))
|
|
539
586
|
}
|
|
540
587
|
if (count > 0) {
|
|
541
588
|
this.head = pos
|
|
542
|
-
this.region.storeI64(this.base
|
|
589
|
+
this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), pos, RELEASE)
|
|
543
590
|
}
|
|
544
591
|
return count
|
|
545
592
|
}
|
|
@@ -577,7 +624,7 @@ export class SpscRing {
|
|
|
577
624
|
if (waitMs === 0) {
|
|
578
625
|
return RING_EMPTY
|
|
579
626
|
}
|
|
580
|
-
this.region.storeI32(this.base
|
|
627
|
+
this.region.storeI32(ringAdd(this.base, RING_SLEEP_AT), RING_ASLEEP, SEQ_CST)
|
|
581
628
|
if (!ringSleep(this.region, this.base, this.ready(SEQ_CST), waitMs)) {
|
|
582
629
|
return SHM_UNSUPPORTED
|
|
583
630
|
}
|
|
@@ -605,15 +652,15 @@ export class SpscRing {
|
|
|
605
652
|
return none
|
|
606
653
|
}
|
|
607
654
|
const len: i32 = this.lengthAt(this.head, this.seenTail)
|
|
608
|
-
const from: i32 = toI32((this.found
|
|
609
|
-
if (len === 0 || len > this.capacity
|
|
655
|
+
const from: i32 = toI32(ringPos(this.found, toI64(4)) & toI64(ringSub(this.capacity, 1)))
|
|
656
|
+
if (len === 0 || len > ringSub(this.capacity, from)) {
|
|
610
657
|
const none: u8[] = this.emptyWindow(this.view)
|
|
611
658
|
this.view = none
|
|
612
659
|
return none
|
|
613
660
|
}
|
|
614
|
-
const r: u8[] = this.region.lend(this.base
|
|
661
|
+
const r: u8[] = this.region.lend(ringAdd(ringAdd(this.base, RING_DATA_AT), from), len, this.view)
|
|
615
662
|
this.view = r
|
|
616
|
-
this.peekedEnd = this.found
|
|
663
|
+
this.peekedEnd = ringPos(this.found, toI64(ringRecordBytes(len)))
|
|
617
664
|
return r
|
|
618
665
|
}
|
|
619
666
|
|
|
@@ -629,7 +676,7 @@ export class SpscRing {
|
|
|
629
676
|
this.region.giveBack(r)
|
|
630
677
|
this.head = this.peekedEnd
|
|
631
678
|
this.peekedEnd = -1
|
|
632
|
-
this.region.storeI64(this.base
|
|
679
|
+
this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), this.head, RELEASE)
|
|
633
680
|
}
|
|
634
681
|
}
|
|
635
682
|
|
|
@@ -657,12 +704,12 @@ export const initSpscRing = (region: SharedRegion, base: i32, capacity: i32, doo
|
|
|
657
704
|
panic(`nish/sync/ring: an SPSC ring's capacity is a power of two from 8, not ${capacity}`)
|
|
658
705
|
}
|
|
659
706
|
ringCheckPlace(region, base, toI64(spscRingBytes(capacity)), "an SPSC ring")
|
|
660
|
-
region.storeI32(base
|
|
661
|
-
region.storeI32(base
|
|
662
|
-
region.storeI64(base
|
|
663
|
-
region.storeI64(base
|
|
664
|
-
region.storeI32(base
|
|
665
|
-
region.storeI32(base
|
|
707
|
+
region.storeI32(ringAdd(base, RING_SIZE_AT), capacity, RELAXED)
|
|
708
|
+
region.storeI32(ringAdd(base, RING_BELL_AT), doorbell ? toI32(1) : toI32(0), RELAXED)
|
|
709
|
+
region.storeI64(ringAdd(base, RING_TAIL_AT), toI64(0), RELAXED)
|
|
710
|
+
region.storeI64(ringAdd(base, RING_HEAD_AT), toI64(0), RELAXED)
|
|
711
|
+
region.storeI32(ringAdd(base, RING_SLEEP_AT), RING_AWAKE, RELAXED)
|
|
712
|
+
region.storeI32(ringAdd(base, RING_MAGIC_AT), RING_SPSC_MAGIC, RELEASE)
|
|
666
713
|
return new SpscRing(region, base)
|
|
667
714
|
}
|
|
668
715
|
|
|
@@ -672,22 +719,27 @@ export const initSpscRing = (region: SharedRegion, base: i32, capacity: i32, doo
|
|
|
672
719
|
* of it that the init function makes last.
|
|
673
720
|
* @param region the region the ring lives in, as this process mapped it
|
|
674
721
|
* @param base the byte offset of the ring's header, a multiple of 64 inside
|
|
675
|
-
* the region
|
|
676
|
-
* past the region's end
|
|
722
|
+
* the region
|
|
677
723
|
* @returns a new `SpscRing` that starts from both positions as the header
|
|
678
|
-
* holds them now, or the reason there is no ring at `base`:
|
|
679
|
-
*
|
|
724
|
+
* holds them now, or the reason there is no ring at `base`: a base that is
|
|
725
|
+
* not a multiple of 64 in the region, no SPSC magic, a capacity no
|
|
726
|
+
* `initSpscRing` writes, or data that would run past the region's end
|
|
680
727
|
*/
|
|
681
728
|
export const attachSpscRing = (region: SharedRegion, base: i32): Result<SpscRing, string> => {
|
|
682
|
-
|
|
683
|
-
|
|
729
|
+
if (!ringFits(region, base, toI64(RING_DATA_AT))) {
|
|
730
|
+
return Err(ringPlaceError(region, base, toI64(RING_DATA_AT), "an SPSC ring"))
|
|
731
|
+
}
|
|
732
|
+
if (region.loadI32(ringAdd(base, RING_MAGIC_AT), ACQUIRE) !== RING_SPSC_MAGIC) {
|
|
684
733
|
return Err(`no SPSC ring at ${base}`)
|
|
685
734
|
}
|
|
686
|
-
const capacity: i32 = region.loadI32(base
|
|
735
|
+
const capacity: i32 = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
|
|
687
736
|
if (capacity < 8 || !ringIsPowerOfTwo(capacity)) {
|
|
688
737
|
return Err(`the SPSC ring at ${base} has capacity ${capacity}`)
|
|
689
738
|
}
|
|
690
|
-
|
|
739
|
+
const bytes: i64 = toI64(RING_DATA_AT) + toI64(capacity)
|
|
740
|
+
if (!ringFits(region, base, bytes)) {
|
|
741
|
+
return Err(ringPlaceError(region, base, bytes, "an SPSC ring"))
|
|
742
|
+
}
|
|
691
743
|
return Ok(new SpscRing(region, base))
|
|
692
744
|
}
|
|
693
745
|
|
|
@@ -720,25 +772,27 @@ export class MpscRing {
|
|
|
720
772
|
constructor(region: SharedRegion, base: i32) {
|
|
721
773
|
this.region = region
|
|
722
774
|
this.base = base
|
|
723
|
-
this.slots = region.loadI32(base
|
|
724
|
-
this.slotBytes = region.loadI32(base
|
|
725
|
-
this.doorbell = region.loadI32(base
|
|
726
|
-
this.head = region.loadI64(base
|
|
775
|
+
this.slots = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
|
|
776
|
+
this.slotBytes = region.loadI32(ringAdd(base, RING_SLOT_AT), RELAXED)
|
|
777
|
+
this.doorbell = region.loadI32(ringAdd(base, RING_BELL_AT), RELAXED) !== 0
|
|
778
|
+
this.head = region.loadI64(ringAdd(base, RING_HEAD_AT), ACQUIRE)
|
|
727
779
|
}
|
|
728
780
|
|
|
729
781
|
/** The offset of position `pos`'s slot. */
|
|
730
782
|
private slotAt(pos: i64): i32 {
|
|
731
|
-
const index: i32 = toI32(pos & toI64(this.slots
|
|
732
|
-
return this.base
|
|
783
|
+
const index: i32 = toI32(pos & toI64(ringSub(this.slots, 1)))
|
|
784
|
+
return ringAdd(ringAdd(this.base, RING_DATA_AT), ringMul(index, ringAdd(RING_SLOT_HEAD, this.slotBytes)))
|
|
733
785
|
}
|
|
734
786
|
|
|
735
787
|
/**
|
|
736
788
|
* Queues `src[off, off + len)` as one record: false when every slot is
|
|
737
|
-
* taken now. A record longer than a slot panics
|
|
738
|
-
*
|
|
739
|
-
* `
|
|
740
|
-
*
|
|
741
|
-
*
|
|
789
|
+
* taken now. A record longer than a slot panics, and so does an enqueue
|
|
790
|
+
* position a peer wrote back; `offer` answers them as `RING_TOO_LONG` and
|
|
791
|
+
* `RING_CORRUPT` instead. The producer claims a slot with a `RELAXED`
|
|
792
|
+
* compare-exchange on the enqueue position, after an `ACQUIRE` load of the
|
|
793
|
+
* slot's sequence number; it copies the record in and publishes it with one
|
|
794
|
+
* store of that number, `RELEASE`, or `SEQ_CST` followed by the doorbell
|
|
795
|
+
* check on a doorbell ring.
|
|
742
796
|
* @param src the bytes to queue
|
|
743
797
|
* @param off where in `src` the record starts
|
|
744
798
|
* @param len the record's length in bytes, from 0 to `slotBytes`; a range
|
|
@@ -747,56 +801,91 @@ export class MpscRing {
|
|
|
747
801
|
* take has not been freed by the consumer yet (nothing is written)
|
|
748
802
|
*/
|
|
749
803
|
tryPush(src: readonly u8[], off: i32, len: i32): boolean {
|
|
750
|
-
|
|
804
|
+
const rc: i32 = this.offer(src, off, len)
|
|
805
|
+
if (rc === RING_TOO_LONG) {
|
|
751
806
|
panic(`nish/sync/ring: a record of ${len} bytes does not fit a slot of ${this.slotBytes}`)
|
|
752
807
|
}
|
|
753
|
-
|
|
808
|
+
if (rc === RING_CORRUPT) {
|
|
809
|
+
panic("nish/sync/ring: the enqueue position went backwards: the ring is corrupt")
|
|
810
|
+
}
|
|
811
|
+
return rc === 0
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* `tryPush`, with a record too long for a slot answered rather than a
|
|
816
|
+
* panic, for a caller built with `--deny-panics` or one whose lengths come
|
|
817
|
+
* from outside. It claims and publishes the slot exactly as `tryPush` does.
|
|
818
|
+
* @param src the bytes to queue
|
|
819
|
+
* @param off where in `src` the record starts
|
|
820
|
+
* @param len the record's length in bytes, from 0 to `slotBytes`; a range
|
|
821
|
+
* outside `src` panics, in `SharedRegion.write`
|
|
822
|
+
* @returns 0 when the record is queued, `RING_FULL` when the slot it would
|
|
823
|
+
* take has not been freed by the consumer yet, `RING_TOO_LONG` for a
|
|
824
|
+
* length outside `[0, slotBytes]`, or `RING_CORRUPT` when the enqueue
|
|
825
|
+
* position in the header went backwards, which no producer does; nothing
|
|
826
|
+
* is written unless it is 0
|
|
827
|
+
*/
|
|
828
|
+
offer(src: readonly u8[], off: i32, len: i32): i32 {
|
|
829
|
+
if (len < 0 || len > this.slotBytes) {
|
|
830
|
+
return RING_TOO_LONG
|
|
831
|
+
}
|
|
832
|
+
const tailAt: i32 = ringAdd(this.base, RING_TAIL_AT)
|
|
754
833
|
let pos: i64 = this.region.loadI64(tailAt, RELAXED)
|
|
755
834
|
while (true) {
|
|
756
835
|
const seq: i64 = this.region.loadI64(this.slotAt(pos), ACQUIRE)
|
|
757
836
|
if (seq === pos) {
|
|
758
|
-
const seen: i64 = this.region.compareExchangeI64(tailAt, pos, pos
|
|
837
|
+
const seen: i64 = this.region.compareExchangeI64(tailAt, pos, ringPos(pos, toI64(1)), RELAXED)
|
|
759
838
|
if (seen === pos) {
|
|
760
839
|
break
|
|
761
840
|
}
|
|
762
841
|
pos = seen
|
|
763
842
|
} else if (seq < pos) {
|
|
764
|
-
return
|
|
843
|
+
return RING_FULL
|
|
765
844
|
} else {
|
|
766
|
-
|
|
845
|
+
// A slot one lap on means some producer moved the position past
|
|
846
|
+
// `pos`, and producers only move it forward: one that reads `pos`
|
|
847
|
+
// again was written back by something that is no producer.
|
|
848
|
+
const again: i64 = this.region.loadI64(tailAt, RELAXED)
|
|
849
|
+
if (again === pos) {
|
|
850
|
+
return RING_CORRUPT
|
|
851
|
+
}
|
|
852
|
+
pos = again
|
|
767
853
|
}
|
|
768
854
|
}
|
|
769
855
|
const slot: i32 = this.slotAt(pos)
|
|
770
|
-
this.region.storeI32(slot
|
|
771
|
-
this.region.write(slot
|
|
856
|
+
this.region.storeI32(ringAdd(slot, 8), len, RELAXED)
|
|
857
|
+
this.region.write(ringAdd(slot, RING_SLOT_HEAD), src, off, len)
|
|
772
858
|
if (this.doorbell) {
|
|
773
|
-
this.region.storeI64(slot, pos
|
|
859
|
+
this.region.storeI64(slot, ringPos(pos, toI64(1)), SEQ_CST)
|
|
774
860
|
ringRing(this.region, this.base)
|
|
775
861
|
} else {
|
|
776
|
-
this.region.storeI64(slot, pos
|
|
862
|
+
this.region.storeI64(slot, ringPos(pos, toI64(1)), RELEASE)
|
|
777
863
|
}
|
|
778
|
-
return
|
|
864
|
+
return 0
|
|
779
865
|
}
|
|
780
866
|
|
|
781
867
|
/** Whether the slot at the consumer's position is filled. */
|
|
782
868
|
private ready(order: i32): boolean {
|
|
783
|
-
return this.region.loadI64(this.slotAt(this.head), order) === this.head
|
|
869
|
+
return this.region.loadI64(this.slotAt(this.head), order) === ringPos(this.head, toI64(1))
|
|
784
870
|
}
|
|
785
871
|
|
|
786
872
|
/** Empties the consumer's slot for the producer one lap on. */
|
|
787
873
|
private release(slot: i32): void {
|
|
788
|
-
this.region.storeI64(slot, this.head
|
|
789
|
-
this.head = this.head
|
|
790
|
-
this.region.storeI64(this.base
|
|
874
|
+
this.region.storeI64(slot, ringPos(this.head, toI64(this.slots)), RELEASE)
|
|
875
|
+
this.head = ringPos(this.head, toI64(1))
|
|
876
|
+
this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), this.head, RELAXED)
|
|
791
877
|
}
|
|
792
878
|
|
|
793
|
-
/** The length in the consumer's slot,
|
|
879
|
+
/** The length in the consumer's slot, or `RING_CORRUPT` for one no producer writes. */
|
|
794
880
|
private lengthAt(slot: i32): i32 {
|
|
795
|
-
const len: i32 = this.region.loadI32(slot
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
881
|
+
const len: i32 = this.region.loadI32(ringAdd(slot, 8), RELAXED)
|
|
882
|
+
return len < 0 || len > this.slotBytes ? RING_CORRUPT : len
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/** The panic `tryPop` and `popWait` give for a slot `take` found corrupt. */
|
|
886
|
+
private corrupt(): void {
|
|
887
|
+
const len: i32 = this.region.loadI32(ringAdd(this.slotAt(this.head), 8), RELAXED)
|
|
888
|
+
panic(`nish/sync/ring: a record of length ${len} at position ${this.head}: the ring is corrupt`)
|
|
800
889
|
}
|
|
801
890
|
|
|
802
891
|
/**
|
|
@@ -811,15 +900,35 @@ export class MpscRing {
|
|
|
811
900
|
* `RING_TOO_SMALL`
|
|
812
901
|
*/
|
|
813
902
|
tryPop(dst: u8[], off: i32): i32 {
|
|
903
|
+
const n: i32 = this.take(dst, off)
|
|
904
|
+
if (n === RING_CORRUPT) {
|
|
905
|
+
this.corrupt()
|
|
906
|
+
}
|
|
907
|
+
return n
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* `tryPop`, with a slot another process scribbled on answered as
|
|
912
|
+
* `RING_CORRUPT` rather than a panic, for a caller built with
|
|
913
|
+
* `--deny-panics`. The loads and stores are `tryPop`'s.
|
|
914
|
+
* @param dst the buffer the record is copied into
|
|
915
|
+
* @param off where in `dst` the record's first byte goes
|
|
916
|
+
* @returns the record's length in bytes, or `RING_EMPTY`,
|
|
917
|
+
* `RING_TOO_SMALL` (it stays queued) or `RING_CORRUPT` (nothing moves)
|
|
918
|
+
*/
|
|
919
|
+
take(dst: u8[], off: i32): i32 {
|
|
814
920
|
if (!this.ready(ACQUIRE)) {
|
|
815
921
|
return RING_EMPTY
|
|
816
922
|
}
|
|
817
923
|
const slot: i32 = this.slotAt(this.head)
|
|
818
924
|
const len: i32 = this.lengthAt(slot)
|
|
819
|
-
if (len
|
|
925
|
+
if (len === RING_CORRUPT) {
|
|
926
|
+
return RING_CORRUPT
|
|
927
|
+
}
|
|
928
|
+
if (len > ringSub(toI32(dst.length), off)) {
|
|
820
929
|
return RING_TOO_SMALL
|
|
821
930
|
}
|
|
822
|
-
this.region.read(slot
|
|
931
|
+
this.region.read(ringAdd(slot, RING_SLOT_HEAD), dst, off, len)
|
|
823
932
|
this.release(slot)
|
|
824
933
|
return len
|
|
825
934
|
}
|
|
@@ -843,16 +952,19 @@ export class MpscRing {
|
|
|
843
952
|
while (count < toI32(lens.length) && this.ready(ACQUIRE)) {
|
|
844
953
|
const slot: i32 = this.slotAt(this.head)
|
|
845
954
|
const len: i32 = this.lengthAt(slot)
|
|
846
|
-
if (len
|
|
955
|
+
if (len === RING_CORRUPT) {
|
|
956
|
+
this.corrupt()
|
|
957
|
+
}
|
|
958
|
+
if (len > ringSub(toI32(dst.length), used)) {
|
|
847
959
|
break
|
|
848
960
|
}
|
|
849
|
-
this.region.read(slot
|
|
961
|
+
this.region.read(ringAdd(slot, RING_SLOT_HEAD), dst, used, len)
|
|
850
962
|
this.release(slot)
|
|
851
963
|
if (count >= 0 && count < toI32(lens.length)) {
|
|
852
964
|
lens[count] = len
|
|
853
965
|
}
|
|
854
966
|
count = count + 1
|
|
855
|
-
used = used
|
|
967
|
+
used = ringAdd(used, len)
|
|
856
968
|
}
|
|
857
969
|
return count
|
|
858
970
|
}
|
|
@@ -872,10 +984,30 @@ export class MpscRing {
|
|
|
872
984
|
* `SHM_UNSUPPORTED`
|
|
873
985
|
*/
|
|
874
986
|
popWait(dst: u8[], off: i32, timeoutMs: i32): i32 {
|
|
987
|
+
const n: i32 = this.takeWait(dst, off, timeoutMs)
|
|
988
|
+
if (n === RING_CORRUPT) {
|
|
989
|
+
this.corrupt()
|
|
990
|
+
}
|
|
991
|
+
return n
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/**
|
|
995
|
+
* `popWait` on `take`: a slot another process scribbled on ends the wait
|
|
996
|
+
* as `RING_CORRUPT` rather than a panic, for a caller built with
|
|
997
|
+
* `--deny-panics`. It polls and sleeps exactly as `popWait` does.
|
|
998
|
+
* @param dst the buffer the record is copied into
|
|
999
|
+
* @param off where in `dst` the record's first byte goes
|
|
1000
|
+
* @param timeoutMs the longest wait in milliseconds, from the call; 0 polls
|
|
1001
|
+
* one round, negative waits with no limit
|
|
1002
|
+
* @returns what `take` answers for the record (its length,
|
|
1003
|
+
* `RING_TOO_SMALL` or `RING_CORRUPT`), `RING_EMPTY` when the time ran
|
|
1004
|
+
* out, or `SHM_UNSUPPORTED`
|
|
1005
|
+
*/
|
|
1006
|
+
takeWait(dst: u8[], off: i32, timeoutMs: i32): i32 {
|
|
875
1007
|
const deadline: i64 = ringDeadline(timeoutMs)
|
|
876
1008
|
while (true) {
|
|
877
1009
|
for (let i: i32 = 0; i < RING_SPINS; i++) {
|
|
878
|
-
const n: i32 = this.
|
|
1010
|
+
const n: i32 = this.take(dst, off)
|
|
879
1011
|
if (n !== RING_EMPTY) {
|
|
880
1012
|
return n
|
|
881
1013
|
}
|
|
@@ -887,7 +1019,7 @@ export class MpscRing {
|
|
|
887
1019
|
if (waitMs === 0) {
|
|
888
1020
|
return RING_EMPTY
|
|
889
1021
|
}
|
|
890
|
-
this.region.storeI32(this.base
|
|
1022
|
+
this.region.storeI32(ringAdd(this.base, RING_SLEEP_AT), RING_ASLEEP, SEQ_CST)
|
|
891
1023
|
if (!ringSleep(this.region, this.base, this.ready(SEQ_CST), waitMs)) {
|
|
892
1024
|
return SHM_UNSUPPORTED
|
|
893
1025
|
}
|
|
@@ -923,29 +1055,61 @@ export const initMpscRing = (
|
|
|
923
1055
|
slotBytes: i32,
|
|
924
1056
|
doorbell: boolean
|
|
925
1057
|
): MpscRing => {
|
|
1058
|
+
const made: Result<MpscRing, string> = tryInitMpscRing(region, base, slots, slotBytes, doorbell)
|
|
1059
|
+
if (made.isErr()) {
|
|
1060
|
+
panic(made.error)
|
|
1061
|
+
}
|
|
1062
|
+
return made.value
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
/** `mpscRingBytes` in wrapping u64, where the most any i32 shape takes is far below 2^63. */
|
|
1066
|
+
const mpscRingBytesWide = (slots: i32, slotBytes: i32): i64 =>
|
|
1067
|
+
toI64(toU64(RING_DATA_AT) + toU64(slots) * (toU64(RING_SLOT_HEAD) + toU64(slotBytes)))
|
|
1068
|
+
|
|
1069
|
+
/**
|
|
1070
|
+
* `initMpscRing`, with a shape or a place it cannot use answered as an `Err`
|
|
1071
|
+
* rather than a panic, for a caller built with `--deny-panics` or one whose
|
|
1072
|
+
* shape comes from outside. It writes the header exactly as `initMpscRing`
|
|
1073
|
+
* does, and writes nothing when it answers an `Err`.
|
|
1074
|
+
* @param region the region the ring lives in
|
|
1075
|
+
* @param base the byte offset of the ring's header, a multiple of 64 with
|
|
1076
|
+
* `mpscRingBytes(slots, slotBytes)` bytes of the region from it
|
|
1077
|
+
* @param slots how many records the ring holds at once, a power of two from 2
|
|
1078
|
+
* @param slotBytes the longest record, in bytes, a multiple of 8 from 8
|
|
1079
|
+
* @param doorbell whether a consumer's `popWait` may sleep on Linux's futex,
|
|
1080
|
+
* at the cost of a `SEQ_CST` store and load on every push
|
|
1081
|
+
* @returns a new `MpscRing` on the empty ring, or the message
|
|
1082
|
+
* `initMpscRing` would panic with
|
|
1083
|
+
*/
|
|
1084
|
+
export const tryInitMpscRing = (
|
|
1085
|
+
region: SharedRegion,
|
|
1086
|
+
base: i32,
|
|
1087
|
+
slots: i32,
|
|
1088
|
+
slotBytes: i32,
|
|
1089
|
+
doorbell: boolean
|
|
1090
|
+
): Result<MpscRing, string> => {
|
|
926
1091
|
if (slots < 2 || !ringIsPowerOfTwo(slots) || slotBytes < 8 || slotBytes % 8 !== 0) {
|
|
927
|
-
|
|
1092
|
+
return Err(
|
|
928
1093
|
`nish/sync/ring: an MPSC ring has a power of two from 2 slots of a multiple of 8 bytes, not ${slots} of ${slotBytes}`
|
|
929
1094
|
)
|
|
930
1095
|
}
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
base,
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
)
|
|
937
|
-
region.storeI32(base
|
|
938
|
-
region.
|
|
939
|
-
region.
|
|
940
|
-
region.
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
const stride: i32 = RING_SLOT_HEAD + slotBytes
|
|
1096
|
+
const bytes: i64 = mpscRingBytesWide(slots, slotBytes)
|
|
1097
|
+
if (!ringFits(region, base, bytes)) {
|
|
1098
|
+
return Err(ringPlaceError(region, base, bytes, "an MPSC ring"))
|
|
1099
|
+
}
|
|
1100
|
+
region.storeI32(ringAdd(base, RING_SIZE_AT), slots, RELAXED)
|
|
1101
|
+
region.storeI32(ringAdd(base, RING_SLOT_AT), slotBytes, RELAXED)
|
|
1102
|
+
region.storeI32(ringAdd(base, RING_BELL_AT), doorbell ? toI32(1) : toI32(0), RELAXED)
|
|
1103
|
+
region.storeI64(ringAdd(base, RING_TAIL_AT), toI64(0), RELAXED)
|
|
1104
|
+
region.storeI64(ringAdd(base, RING_HEAD_AT), toI64(0), RELAXED)
|
|
1105
|
+
region.storeI32(ringAdd(base, RING_SLEEP_AT), RING_AWAKE, RELAXED)
|
|
1106
|
+
const stride: i32 = ringAdd(RING_SLOT_HEAD, slotBytes)
|
|
1107
|
+
const data: i32 = ringAdd(base, RING_DATA_AT)
|
|
944
1108
|
for (let i: i32 = 0; i < slots; i++) {
|
|
945
|
-
region.storeI64(
|
|
1109
|
+
region.storeI64(ringAdd(data, ringMul(i, stride)), toI64(i), RELAXED)
|
|
946
1110
|
}
|
|
947
|
-
region.storeI32(base
|
|
948
|
-
return new MpscRing(region, base)
|
|
1111
|
+
region.storeI32(ringAdd(base, RING_MAGIC_AT), RING_MPSC_MAGIC, RELEASE)
|
|
1112
|
+
return Ok(new MpscRing(region, base))
|
|
949
1113
|
}
|
|
950
1114
|
|
|
951
1115
|
/**
|
|
@@ -954,27 +1118,27 @@ export const initMpscRing = (
|
|
|
954
1118
|
* of it that the init function makes last.
|
|
955
1119
|
* @param region the region the ring lives in, as this process mapped it
|
|
956
1120
|
* @param base the byte offset of the ring's header, a multiple of 64 inside
|
|
957
|
-
* the region
|
|
958
|
-
* run past the region's end
|
|
1121
|
+
* the region
|
|
959
1122
|
* @returns a new `MpscRing` that starts from the consumer's position as the
|
|
960
|
-
* header holds it now, or the reason there is no ring at `base`:
|
|
961
|
-
* magic,
|
|
1123
|
+
* header holds it now, or the reason there is no ring at `base`: a base
|
|
1124
|
+
* that is not a multiple of 64 in the region, no MPSC magic, a shape no
|
|
1125
|
+
* `initMpscRing` writes, or slots that would run past the region's end
|
|
962
1126
|
*/
|
|
963
1127
|
export const attachMpscRing = (region: SharedRegion, base: i32): Result<MpscRing, string> => {
|
|
964
|
-
|
|
965
|
-
|
|
1128
|
+
if (!ringFits(region, base, toI64(RING_DATA_AT))) {
|
|
1129
|
+
return Err(ringPlaceError(region, base, toI64(RING_DATA_AT), "an MPSC ring"))
|
|
1130
|
+
}
|
|
1131
|
+
if (region.loadI32(ringAdd(base, RING_MAGIC_AT), ACQUIRE) !== RING_MPSC_MAGIC) {
|
|
966
1132
|
return Err(`no MPSC ring at ${base}`)
|
|
967
1133
|
}
|
|
968
|
-
const slots: i32 = region.loadI32(base
|
|
969
|
-
const slotBytes: i32 = region.loadI32(base
|
|
1134
|
+
const slots: i32 = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
|
|
1135
|
+
const slotBytes: i32 = region.loadI32(ringAdd(base, RING_SLOT_AT), RELAXED)
|
|
970
1136
|
if (slots < 2 || !ringIsPowerOfTwo(slots) || slotBytes < 8 || slotBytes % 8 !== 0) {
|
|
971
1137
|
return Err(`the MPSC ring at ${base} has ${slots} slots of ${slotBytes} bytes`)
|
|
972
1138
|
}
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
base,
|
|
976
|
-
|
|
977
|
-
"an MPSC ring"
|
|
978
|
-
)
|
|
1139
|
+
const bytes: i64 = mpscRingBytesWide(slots, slotBytes)
|
|
1140
|
+
if (!ringFits(region, base, bytes)) {
|
|
1141
|
+
return Err(ringPlaceError(region, base, bytes, "an MPSC ring"))
|
|
1142
|
+
}
|
|
979
1143
|
return Ok(new MpscRing(region, base))
|
|
980
1144
|
}
|