@amritk/nish-aarch64-linux 0.49.1 → 0.51.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 +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +237 -160
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +94 -11
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +14 -11
- package/std/base64.ts +74 -35
- package/std/bytes.ts +207 -97
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +213 -115
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +484 -149
- package/std/crypto/jwt.ts +51 -40
- package/std/crypto/p256.ts +14 -19
- package/std/crypto/pki-roots.ts +35 -12
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1635 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +212 -90
- package/std/crypto/sha512.ts +135 -49
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +236 -119
- package/std/json.ts +81 -46
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +224 -61
- package/std/net/handler.ts +189 -88
- package/std/net/hpack.ts +216 -103
- package/std/net/http-compress.ts +104 -42
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +326 -24
- package/std/net/http1-client-codec.ts +159 -55
- package/std/net/http1-codec.ts +427 -237
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +139 -60
- package/std/net/http3.ts +140 -79
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +97 -64
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +147 -40
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +61 -39
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +36 -27
- package/std/net/quic.ts +84 -54
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +86 -36
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +80 -35
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +194 -50
- package/std/net/upstream.ts +83 -38
- package/std/net/url.ts +170 -94
- package/std/net/websocket.ts +19 -20
- package/std/net/webtransport.ts +123 -33
- package/std/raster.ts +12 -2
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +176 -24
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +38 -16
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/sync/ring.ts
CHANGED
|
@@ -99,6 +99,7 @@
|
|
|
99
99
|
|
|
100
100
|
import { ACQUIRE, RELAXED, RELEASE, SEQ_CST, SHM_UNSUPPORTED, SharedRegion } from "nish/sync/shm"
|
|
101
101
|
import { monotonicNanos } from "nish:process"
|
|
102
|
+
import { wrappingAdd, wrappingSub } from "nish:unsafe"
|
|
102
103
|
|
|
103
104
|
/** `tryPop`, `popWait`: the ring is empty (or the wait ran out). */
|
|
104
105
|
export const RING_EMPTY: i32 = -1
|
|
@@ -143,31 +144,76 @@ const RING_ASLEEP: i32 = 1
|
|
|
143
144
|
const ringIsPowerOfTwo = (n: i32): boolean => n > 0 && (n & (n - 1)) === 0
|
|
144
145
|
|
|
145
146
|
/**
|
|
146
|
-
* An offset
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
147
|
+
* An i64 offset or size as an i32, held at the i32 range's ends instead of
|
|
148
|
+
* wrapped. A held answer is past every region (a region of 2 GiB or more is
|
|
149
|
+
* refused before any ring is made in it), so an offset that ever reached one
|
|
150
|
+
* would be refused by the region's own bounds check, not read at a wrapped place.
|
|
150
151
|
*/
|
|
151
|
-
const
|
|
152
|
+
const ringHeld = (n: i64): i32 => {
|
|
153
|
+
if (n > toI64(2147483647)) {
|
|
154
|
+
return 2147483647
|
|
155
|
+
}
|
|
156
|
+
return n < toI64(-2147483648) ? -2147483648 : toI32(n)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* An offset into the region. Every caller adds a header constant or a masked
|
|
161
|
+
* index to a base `ringFits` already placed, so the sum cannot pass the
|
|
162
|
+
* region's size; it is taken in i64 so it is never a panic site.
|
|
163
|
+
*/
|
|
164
|
+
const ringAdd = (a: i32, b: i32): i32 => ringHeld(toI64(a) + toI64(b))
|
|
165
|
+
|
|
166
|
+
/** `n`, or 0 for a negative one: an offset `ringAdd` held, which a ring `ringFits` placed is never below 0. */
|
|
167
|
+
const ringFloor = (n: i32): integer<0, 2147483647> => (n >= 0 ? n : 0)
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* How many slots position `pos`'s slot is before a ring's last one: `mask -
|
|
171
|
+
* index`, which is `~pos & mask` for a mask one less than a power of two.
|
|
172
|
+
*/
|
|
173
|
+
const ringBack = (pos: i64, mask: integer<0, 1073741823>): integer<0, 1073741823> => {
|
|
174
|
+
const back: i32 = ~toI32(pos) & mask
|
|
175
|
+
// Always true (`back` is at most `mask`); stated for the prover, and folded away.
|
|
176
|
+
return back >= 0 && back <= 1073741823 ? back : 0
|
|
177
|
+
}
|
|
152
178
|
|
|
153
|
-
/**
|
|
154
|
-
|
|
179
|
+
/**
|
|
180
|
+
* The offset `back` slots of `stride` bytes before `last`, an MPSC slot's
|
|
181
|
+
* field in the ring's last slot. `ringFits` placed every slot inside the
|
|
182
|
+
* region, so the difference is never below 0; one that were answers -1, which
|
|
183
|
+
* the region's bounds check refuses.
|
|
184
|
+
*/
|
|
185
|
+
const ringBackAt = (
|
|
186
|
+
last: integer<0, 2147483647>,
|
|
187
|
+
back: integer<0, 1073741823>,
|
|
188
|
+
stride: integer<0, 2147483647>
|
|
189
|
+
): i32 => {
|
|
190
|
+
const at: i64 = toI64(last) - toI64(back) * toI64(stride)
|
|
191
|
+
return at >= toI64(0) ? toI32(at) : -1
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The product of two ring sizes, held like `ringAdd`'s sum; the shapes it is given were checked first. */
|
|
195
|
+
const ringMul = (a: i32, b: i32): i32 => ringHeld(toI64(a) * toI64(b))
|
|
155
196
|
|
|
156
197
|
/**
|
|
157
|
-
* A ring position plus a count
|
|
158
|
-
*
|
|
159
|
-
*
|
|
198
|
+
* A ring position plus a count. Positions are sequence counters that the
|
|
199
|
+
* producer and consumer compare only by their difference (`ringGap`), so
|
|
200
|
+
* they count modulo 2^64 by design: `wrappingAdd` is the counter's answer,
|
|
201
|
+
* not a hidden overflow, and they reach 2^63 only after centuries of traffic.
|
|
160
202
|
*/
|
|
161
|
-
const ringPos = (pos: i64, by: i64): i64 =>
|
|
203
|
+
const ringPos = (pos: i64, by: i64): i64 => wrappingAdd(pos, by)
|
|
162
204
|
|
|
163
|
-
/** `a - b
|
|
164
|
-
const ringSub = (a: i32, b: i32): i32 =>
|
|
205
|
+
/** `a - b`, for the same offsets `ringAdd` sums, held like its sum. */
|
|
206
|
+
const ringSub = (a: i32, b: i32): i32 => ringHeld(toI64(a) - toI64(b))
|
|
165
207
|
|
|
166
|
-
/** How far position `a` is past `b
|
|
167
|
-
const ringGap = (a: i64, b: i64): i64 =>
|
|
208
|
+
/** How far position `a` is past `b`: the difference of two `ringPos` counters, modulo 2^64 as they count. */
|
|
209
|
+
const ringGap = (a: i64, b: i64): i64 => wrappingSub(a, b)
|
|
168
210
|
|
|
169
|
-
/**
|
|
170
|
-
|
|
211
|
+
/**
|
|
212
|
+
* The bytes an SPSC record of `len` takes: its length word and the payload,
|
|
213
|
+
* rounded up to 4. Its callers have checked `len` is from 0 to `capacity - 4`,
|
|
214
|
+
* so the sum cannot leave i64.
|
|
215
|
+
*/
|
|
216
|
+
const ringRecordWide = (len: i32): i64 => (toI64(len) + toI64(7)) & toI64(-4)
|
|
171
217
|
|
|
172
218
|
/**
|
|
173
219
|
* Whether a ring of `bytes` fits its region from `base` with its header
|
|
@@ -195,7 +241,10 @@ const ringCheckPlace = (region: SharedRegion, base: i32, bytes: i64, what: strin
|
|
|
195
241
|
* 256-byte header and the data. Nothing is checked here; `initSpscRing`
|
|
196
242
|
* refuses a capacity that is not a power of two from 8.
|
|
197
243
|
* @param capacity the ring's data bytes, as `initSpscRing` will be given them
|
|
198
|
-
* @returns how many bytes from the ring's base the region must hold
|
|
244
|
+
* @returns how many bytes from the ring's base the region must hold; a
|
|
245
|
+
* capacity whose size passes the i32 range answers 2147483647, which no
|
|
246
|
+
* power-of-two capacity `initSpscRing` takes reaches, and which `ringFits`
|
|
247
|
+
* refuses as a place in any region
|
|
199
248
|
*/
|
|
200
249
|
export const spscRingBytes = (capacity: i32): i32 => ringAdd(RING_DATA_AT, capacity)
|
|
201
250
|
|
|
@@ -207,7 +256,9 @@ export const spscRingBytes = (capacity: i32): i32 => ringAdd(RING_DATA_AT, capac
|
|
|
207
256
|
* will be given it
|
|
208
257
|
* @param slotBytes the longest record, in bytes, as `initMpscRing` will be
|
|
209
258
|
* given it
|
|
210
|
-
* @returns how many bytes from the ring's base the region must hold
|
|
259
|
+
* @returns how many bytes from the ring's base the region must hold; a shape
|
|
260
|
+
* whose size passes the i32 range answers 2147483647, and `initMpscRing`
|
|
261
|
+
* and `tryInitMpscRing` refuse that shape through `ringFits`
|
|
211
262
|
*/
|
|
212
263
|
export const mpscRingBytes = (slots: i32, slotBytes: i32): i32 =>
|
|
213
264
|
ringAdd(RING_DATA_AT, ringMul(slots, ringAdd(RING_SLOT_HEAD, slotBytes)))
|
|
@@ -228,17 +279,28 @@ const ringRing = (region: SharedRegion, base: i32): void => {
|
|
|
228
279
|
}
|
|
229
280
|
}
|
|
230
281
|
|
|
231
|
-
/**
|
|
282
|
+
/**
|
|
283
|
+
* `monotonicNanos()` past which a wait of `timeoutMs` gives up; 0 for no limit.
|
|
284
|
+
* The clock is read only through `ringLeftMs`'s difference, so the deadline
|
|
285
|
+
* counts modulo 2^64 like a `ringPos` position.
|
|
286
|
+
*/
|
|
232
287
|
const ringDeadline = (timeoutMs: i32): i64 =>
|
|
233
|
-
timeoutMs < 0 ? toI64(0) :
|
|
288
|
+
timeoutMs < 0 ? toI64(0) : wrappingAdd(monotonicNanos(), toI64(timeoutMs) * toI64(1000000))
|
|
234
289
|
|
|
235
290
|
/** The milliseconds left before `deadline`, rounded up: -1 for no limit, 0 when it has passed. */
|
|
236
291
|
const ringLeftMs = (deadline: i64): i32 => {
|
|
237
292
|
if (deadline === toI64(0)) {
|
|
238
293
|
return -1
|
|
239
294
|
}
|
|
240
|
-
const left: i64 =
|
|
241
|
-
|
|
295
|
+
const left: i64 = wrappingSub(deadline, monotonicNanos())
|
|
296
|
+
if (left <= toI64(0)) {
|
|
297
|
+
return 0
|
|
298
|
+
}
|
|
299
|
+
const ms: i32 = toI32(left / toI64(1000000))
|
|
300
|
+
if (ms < 0 || ms > 2147483646) {
|
|
301
|
+
return 2147483647
|
|
302
|
+
}
|
|
303
|
+
return left % toI64(1000000) === toI64(0) ? ms : ms + 1
|
|
242
304
|
}
|
|
243
305
|
|
|
244
306
|
/**
|
|
@@ -271,8 +333,6 @@ export class SpscRing {
|
|
|
271
333
|
private base: i32
|
|
272
334
|
/** Data bytes, a power of two. */
|
|
273
335
|
capacity: i32
|
|
274
|
-
/** Whether a push rings the consumer's doorbell. */
|
|
275
|
-
private doorbell: boolean
|
|
276
336
|
/** The producer's position: bytes ever pushed (this object's, as a producer). */
|
|
277
337
|
private tail: i64
|
|
278
338
|
/** The consumer's position: bytes ever popped (this object's, as a consumer). */
|
|
@@ -297,6 +357,22 @@ export class SpscRing {
|
|
|
297
357
|
private peekedEnd: i64 = -1
|
|
298
358
|
/** The position of the record `lengthAt` last read, past any skip marker. */
|
|
299
359
|
private found: i64 = 0
|
|
360
|
+
/** `capacity - 1`, which turns a position into an index; 0 for a capacity no ring has. */
|
|
361
|
+
private mask: integer<0, 1073741823> = 0
|
|
362
|
+
/** The data's offset in the region. */
|
|
363
|
+
private dataAt: integer<0, 2147483647> = 0
|
|
364
|
+
/**
|
|
365
|
+
* The offset of the data's last byte. An index is reached counting back
|
|
366
|
+
* from it by `mask - index`, which `~pos & mask` gives: an i32 difference
|
|
367
|
+
* of a place `ringFits` checked and a masked count, so it needs no hold.
|
|
368
|
+
*/
|
|
369
|
+
private lastAt: integer<0, 2147483647> = 0
|
|
370
|
+
/** The producer's position's offset in the region. */
|
|
371
|
+
private tailAt: i32 = 0
|
|
372
|
+
/** The consumer's position's offset in the region. */
|
|
373
|
+
private headAt: i32 = 0
|
|
374
|
+
/** Whether a push rings the consumer's doorbell. */
|
|
375
|
+
private doorbell: boolean
|
|
300
376
|
|
|
301
377
|
/**
|
|
302
378
|
* Reads the header of the ring at `base`: its capacity and doorbell, and
|
|
@@ -310,9 +386,15 @@ export class SpscRing {
|
|
|
310
386
|
this.region = region
|
|
311
387
|
this.base = base
|
|
312
388
|
this.capacity = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
|
|
389
|
+
const mask: i32 = ringSub(this.capacity, 1)
|
|
390
|
+
this.mask = mask >= 0 && mask <= 1073741823 ? mask : 0
|
|
391
|
+
this.dataAt = ringFloor(ringAdd(base, RING_DATA_AT))
|
|
392
|
+
this.lastAt = ringFloor(ringAdd(this.dataAt, this.mask))
|
|
393
|
+
this.tailAt = ringAdd(base, RING_TAIL_AT)
|
|
394
|
+
this.headAt = ringAdd(base, RING_HEAD_AT)
|
|
313
395
|
this.doorbell = region.loadI32(ringAdd(base, RING_BELL_AT), RELAXED) !== 0
|
|
314
|
-
this.tail = region.loadI64(
|
|
315
|
-
this.head = region.loadI64(
|
|
396
|
+
this.tail = region.loadI64(this.tailAt, ACQUIRE)
|
|
397
|
+
this.head = region.loadI64(this.headAt, ACQUIRE)
|
|
316
398
|
this.seenHead = this.head
|
|
317
399
|
this.seenTail = this.tail
|
|
318
400
|
}
|
|
@@ -334,28 +416,28 @@ export class SpscRing {
|
|
|
334
416
|
if (this.reserved >= 0) {
|
|
335
417
|
panic("nish/sync/ring: tryPush while a reservation is open; commit or abandon it first")
|
|
336
418
|
}
|
|
337
|
-
|
|
338
|
-
//
|
|
339
|
-
|
|
340
|
-
if (len < 0 || len > ringSub(this.capacity, 4) || need > this.capacity) {
|
|
419
|
+
const mask: integer<0, 1073741823> = this.mask
|
|
420
|
+
// A length past `capacity - 4` (`mask - 3`) needs more than `capacity` bytes.
|
|
421
|
+
if (len < 0 || len > mask - 3) {
|
|
341
422
|
panic(`nish/sync/ring: a record of ${len} bytes does not fit a ring of ${this.capacity}`)
|
|
342
423
|
}
|
|
343
|
-
const end: i64 = ringPos(this.tail,
|
|
424
|
+
const end: i64 = ringPos(this.tail, ringRecordWide(len))
|
|
344
425
|
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
345
|
-
this.seenHead = this.region.loadI64(
|
|
426
|
+
this.seenHead = this.region.loadI64(this.headAt, ACQUIRE)
|
|
346
427
|
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
347
428
|
return false
|
|
348
429
|
}
|
|
349
430
|
}
|
|
350
|
-
const
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const
|
|
355
|
-
const
|
|
356
|
-
|
|
431
|
+
const last: integer<0, 2147483647> = this.lastAt
|
|
432
|
+
const back: i32 = ~toI32(this.tail) & mask
|
|
433
|
+
this.region.storeI32(last - back, len, RELAXED)
|
|
434
|
+
// The payload starts 4 past the length word: 4 fewer to count back, mod the lap.
|
|
435
|
+
const fromBack: i32 = (back - 4) & mask
|
|
436
|
+
const room: i32 = fromBack + 1
|
|
437
|
+
const first: i32 = len < room ? len : room
|
|
438
|
+
this.region.write(last - fromBack, src, off, first)
|
|
357
439
|
if (first < len) {
|
|
358
|
-
this.region.write(
|
|
440
|
+
this.region.write(this.dataAt, src, ringAdd(off, first), ringSub(len, first))
|
|
359
441
|
}
|
|
360
442
|
this.publish(end)
|
|
361
443
|
return true
|
|
@@ -365,10 +447,10 @@ export class SpscRing {
|
|
|
365
447
|
private publish(end: i64): void {
|
|
366
448
|
this.tail = end
|
|
367
449
|
if (this.doorbell) {
|
|
368
|
-
this.region.storeI64(
|
|
450
|
+
this.region.storeI64(this.tailAt, end, SEQ_CST)
|
|
369
451
|
ringRing(this.region, this.base)
|
|
370
452
|
} else {
|
|
371
|
-
this.region.storeI64(
|
|
453
|
+
this.region.storeI64(this.tailAt, end, RELEASE)
|
|
372
454
|
}
|
|
373
455
|
}
|
|
374
456
|
|
|
@@ -378,7 +460,7 @@ export class SpscRing {
|
|
|
378
460
|
return window
|
|
379
461
|
}
|
|
380
462
|
const none: i32 = 0
|
|
381
|
-
const w: u8[] = this.region.lend(
|
|
463
|
+
const w: u8[] = this.region.lend(this.dataAt, none, window)
|
|
382
464
|
this.region.giveBack(w)
|
|
383
465
|
return w
|
|
384
466
|
}
|
|
@@ -411,21 +493,24 @@ export class SpscRing {
|
|
|
411
493
|
if (n < 1 || n > most) {
|
|
412
494
|
panic(`nish/sync/ring: a reservation of ${n} bytes is not from 1 to ${most}, half the ring less 4`)
|
|
413
495
|
}
|
|
414
|
-
const need:
|
|
415
|
-
const
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
const
|
|
496
|
+
const need: i64 = ringRecordWide(n)
|
|
497
|
+
const mask: integer<0, 1073741823> = this.mask
|
|
498
|
+
const back: i32 = ~toI32(this.tail) & mask
|
|
499
|
+
// The rest of the lap, `back + 1` bytes, is padding when the record would cross the end.
|
|
500
|
+
const rest: i32 = back + 1
|
|
501
|
+
const pad: i32 = need <= toI64(rest) ? 0 : rest
|
|
502
|
+
const end: i64 = ringPos(ringPos(this.tail, toI64(pad)), need)
|
|
419
503
|
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
420
|
-
this.seenHead = this.region.loadI64(
|
|
504
|
+
this.seenHead = this.region.loadI64(this.headAt, ACQUIRE)
|
|
421
505
|
if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
|
|
422
506
|
const none: u8[] = this.emptyWindow(this.wnd)
|
|
423
507
|
this.wnd = none
|
|
424
508
|
return none
|
|
425
509
|
}
|
|
426
510
|
}
|
|
427
|
-
const
|
|
428
|
-
const
|
|
511
|
+
const last: integer<0, 2147483647> = this.lastAt
|
|
512
|
+
const lengthWord: i32 = pad > 0 ? this.dataAt : last - back
|
|
513
|
+
const w: u8[] = this.region.lend(ringAdd(lengthWord, 4), n, this.wnd)
|
|
429
514
|
this.wnd = w
|
|
430
515
|
this.reserved = n
|
|
431
516
|
this.reservedPad = pad
|
|
@@ -456,14 +541,14 @@ export class SpscRing {
|
|
|
456
541
|
panic(`nish/sync/ring: commit of ${len} bytes from a reservation of ${n}`)
|
|
457
542
|
}
|
|
458
543
|
this.endReservation("commit")
|
|
459
|
-
const
|
|
460
|
-
const
|
|
461
|
-
const
|
|
544
|
+
const last: integer<0, 2147483647> = this.lastAt
|
|
545
|
+
const mask: integer<0, 1073741823> = this.mask
|
|
546
|
+
const here: i32 = last - (~toI32(this.tail) & mask)
|
|
462
547
|
if (this.reservedPad > 0) {
|
|
463
|
-
this.region.storeI32(
|
|
548
|
+
this.region.storeI32(here, RING_SKIP, RELAXED)
|
|
464
549
|
}
|
|
465
|
-
this.region.storeI32(this.reservedPad > 0 ?
|
|
466
|
-
this.publish(ringPos(this.tail, toI64(
|
|
550
|
+
this.region.storeI32(this.reservedPad > 0 ? this.dataAt : here, len, RELAXED)
|
|
551
|
+
this.publish(ringPos(ringPos(this.tail, toI64(this.reservedPad)), ringRecordWide(len)))
|
|
467
552
|
}
|
|
468
553
|
|
|
469
554
|
/** Drops the open reservation, publishing nothing; panics without one. */
|
|
@@ -476,7 +561,7 @@ export class SpscRing {
|
|
|
476
561
|
if (this.head !== this.seenTail) {
|
|
477
562
|
return true
|
|
478
563
|
}
|
|
479
|
-
this.seenTail = this.region.loadI64(
|
|
564
|
+
this.seenTail = this.region.loadI64(this.tailAt, order)
|
|
480
565
|
return this.head !== this.seenTail
|
|
481
566
|
}
|
|
482
567
|
|
|
@@ -486,17 +571,18 @@ export class SpscRing {
|
|
|
486
571
|
* `found` is set to where that record is.
|
|
487
572
|
*/
|
|
488
573
|
private lengthAt(pos: i64, end: i64): i32 {
|
|
489
|
-
const mask:
|
|
490
|
-
const
|
|
574
|
+
const mask: integer<0, 1073741823> = this.mask
|
|
575
|
+
const last: integer<0, 2147483647> = this.lastAt
|
|
491
576
|
let at: i64 = pos
|
|
492
|
-
|
|
577
|
+
const back: i32 = ~toI32(pos) & mask
|
|
578
|
+
let len: i32 = this.region.loadI32(last - back, RELAXED)
|
|
493
579
|
if (len === RING_SKIP) {
|
|
494
|
-
|
|
495
|
-
|
|
580
|
+
// The rest of the lap, `back + 1` bytes, is padding.
|
|
581
|
+
at = ringPos(pos, toI64(back + 1))
|
|
582
|
+
len = this.region.loadI32(this.dataAt, RELAXED)
|
|
496
583
|
}
|
|
497
|
-
//
|
|
498
|
-
|
|
499
|
-
if (len < 0 || len > ringSub(this.capacity, 4) || toI64(ringRecordBytes(len)) > ringGap(end, at)) {
|
|
584
|
+
// A length past `capacity - 4` (`mask - 3`) needs more than `capacity` bytes.
|
|
585
|
+
if (len < 0 || len > mask - 3 || ringRecordWide(len) > ringGap(end, at)) {
|
|
500
586
|
panic(`nish/sync/ring: a record of length ${len} at position ${at}: the ring is corrupt`)
|
|
501
587
|
}
|
|
502
588
|
this.found = at
|
|
@@ -512,13 +598,14 @@ export class SpscRing {
|
|
|
512
598
|
|
|
513
599
|
/** Copies the record at `pos`, `len` bytes, into `dst` at `off`. */
|
|
514
600
|
private copyOut(pos: i64, len: i32, dst: u8[], off: i32): void {
|
|
515
|
-
const mask:
|
|
516
|
-
const
|
|
517
|
-
const
|
|
518
|
-
const first: i32 = len <
|
|
519
|
-
|
|
601
|
+
const mask: integer<0, 1073741823> = this.mask
|
|
602
|
+
const fromBack: i32 = ((~toI32(pos) & mask) - 4) & mask
|
|
603
|
+
const room: i32 = fromBack + 1
|
|
604
|
+
const first: i32 = len < room ? len : room
|
|
605
|
+
const last: integer<0, 2147483647> = this.lastAt
|
|
606
|
+
this.region.read(last - fromBack, dst, off, first)
|
|
520
607
|
if (first < len) {
|
|
521
|
-
this.region.read(
|
|
608
|
+
this.region.read(this.dataAt, dst, ringAdd(off, first), ringSub(len, first))
|
|
522
609
|
}
|
|
523
610
|
}
|
|
524
611
|
|
|
@@ -540,12 +627,12 @@ export class SpscRing {
|
|
|
540
627
|
return RING_EMPTY
|
|
541
628
|
}
|
|
542
629
|
const len: i32 = this.lengthAt(this.head, this.seenTail)
|
|
543
|
-
if (len >
|
|
630
|
+
if (toI64(len) + toI64(off) > toI64(dst.length)) {
|
|
544
631
|
return RING_TOO_SMALL
|
|
545
632
|
}
|
|
546
633
|
this.copyOut(this.found, len, dst, off)
|
|
547
|
-
this.head = ringPos(this.found,
|
|
548
|
-
this.region.storeI64(
|
|
634
|
+
this.head = ringPos(this.found, ringRecordWide(len))
|
|
635
|
+
this.region.storeI64(this.headAt, this.head, RELEASE)
|
|
549
636
|
return len
|
|
550
637
|
}
|
|
551
638
|
|
|
@@ -567,13 +654,13 @@ export class SpscRing {
|
|
|
567
654
|
this.notPeeked("popBatch")
|
|
568
655
|
// Always the producer's position now, not the cache: a cache that still
|
|
569
656
|
// holds a record would hide every one pushed since it was loaded.
|
|
570
|
-
this.seenTail = this.region.loadI64(
|
|
657
|
+
this.seenTail = this.region.loadI64(this.tailAt, ACQUIRE)
|
|
571
658
|
let count: i32 = 0
|
|
572
659
|
let used: i32 = 0
|
|
573
660
|
let pos: i64 = this.head
|
|
574
661
|
while (count < toI32(lens.length) && pos !== this.seenTail) {
|
|
575
662
|
const len: i32 = this.lengthAt(pos, this.seenTail)
|
|
576
|
-
if (len >
|
|
663
|
+
if (toI64(len) + toI64(used) > toI64(dst.length)) {
|
|
577
664
|
break
|
|
578
665
|
}
|
|
579
666
|
this.copyOut(this.found, len, dst, used)
|
|
@@ -582,11 +669,11 @@ export class SpscRing {
|
|
|
582
669
|
}
|
|
583
670
|
count = count + 1
|
|
584
671
|
used = ringAdd(used, len)
|
|
585
|
-
pos = ringPos(this.found,
|
|
672
|
+
pos = ringPos(this.found, ringRecordWide(len))
|
|
586
673
|
}
|
|
587
674
|
if (count > 0) {
|
|
588
675
|
this.head = pos
|
|
589
|
-
this.region.storeI64(
|
|
676
|
+
this.region.storeI64(this.headAt, pos, RELEASE)
|
|
590
677
|
}
|
|
591
678
|
return count
|
|
592
679
|
}
|
|
@@ -652,15 +739,17 @@ export class SpscRing {
|
|
|
652
739
|
return none
|
|
653
740
|
}
|
|
654
741
|
const len: i32 = this.lengthAt(this.head, this.seenTail)
|
|
655
|
-
const
|
|
656
|
-
|
|
742
|
+
const mask: integer<0, 1073741823> = this.mask
|
|
743
|
+
const fromBack: i32 = ((~toI32(this.found) & mask) - 4) & mask
|
|
744
|
+
if (len === 0 || len > fromBack + 1) {
|
|
657
745
|
const none: u8[] = this.emptyWindow(this.view)
|
|
658
746
|
this.view = none
|
|
659
747
|
return none
|
|
660
748
|
}
|
|
661
|
-
const
|
|
749
|
+
const last: integer<0, 2147483647> = this.lastAt
|
|
750
|
+
const r: u8[] = this.region.lend(last - fromBack, len, this.view)
|
|
662
751
|
this.view = r
|
|
663
|
-
this.peekedEnd = ringPos(this.found,
|
|
752
|
+
this.peekedEnd = ringPos(this.found, ringRecordWide(len))
|
|
664
753
|
return r
|
|
665
754
|
}
|
|
666
755
|
|
|
@@ -676,7 +765,7 @@ export class SpscRing {
|
|
|
676
765
|
this.region.giveBack(r)
|
|
677
766
|
this.head = this.peekedEnd
|
|
678
767
|
this.peekedEnd = -1
|
|
679
|
-
this.region.storeI64(
|
|
768
|
+
this.region.storeI64(this.headAt, this.head, RELEASE)
|
|
680
769
|
}
|
|
681
770
|
}
|
|
682
771
|
|
|
@@ -760,6 +849,22 @@ export class MpscRing {
|
|
|
760
849
|
private doorbell: boolean
|
|
761
850
|
/** The consumer's position: records ever popped (this object's, as the consumer). */
|
|
762
851
|
head: i64
|
|
852
|
+
/** `slots - 1`, which turns a position into a slot's index; 0 for a count no ring has. */
|
|
853
|
+
private mask: integer<0, 1073741823> = 0
|
|
854
|
+
/** The bytes from one slot to the next: its head and its payload. */
|
|
855
|
+
private stride: integer<0, 2147483647> = 0
|
|
856
|
+
/**
|
|
857
|
+
* The offsets of the last slot's sequence number, length word and payload.
|
|
858
|
+
* A slot is reached counting back from them by `(mask - index) * stride`
|
|
859
|
+
* bytes, with `ringBack` and `ringBackAt`.
|
|
860
|
+
*/
|
|
861
|
+
private lastSeqAt: integer<0, 2147483647> = 0
|
|
862
|
+
private lastLenAt: integer<0, 2147483647> = 0
|
|
863
|
+
private lastPayloadAt: integer<0, 2147483647> = 0
|
|
864
|
+
/** The producers' position's offset in the region. */
|
|
865
|
+
private tailAt: i32 = 0
|
|
866
|
+
/** The consumer's position's offset in the region. */
|
|
867
|
+
private headAt: i32 = 0
|
|
763
868
|
|
|
764
869
|
/**
|
|
765
870
|
* Reads the header of the ring at `base`: its shape, its doorbell and the
|
|
@@ -774,14 +879,16 @@ export class MpscRing {
|
|
|
774
879
|
this.base = base
|
|
775
880
|
this.slots = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
|
|
776
881
|
this.slotBytes = region.loadI32(ringAdd(base, RING_SLOT_AT), RELAXED)
|
|
882
|
+
const mask: i32 = ringSub(this.slots, 1)
|
|
883
|
+
this.mask = mask >= 0 && mask <= 1073741823 ? mask : 0
|
|
884
|
+
this.stride = ringFloor(ringAdd(RING_SLOT_HEAD, this.slotBytes))
|
|
885
|
+
this.lastSeqAt = ringFloor(ringAdd(ringAdd(base, RING_DATA_AT), ringMul(this.mask, this.stride)))
|
|
886
|
+
this.lastLenAt = ringFloor(ringAdd(this.lastSeqAt, 8))
|
|
887
|
+
this.lastPayloadAt = ringFloor(ringAdd(this.lastSeqAt, RING_SLOT_HEAD))
|
|
888
|
+
this.tailAt = ringAdd(base, RING_TAIL_AT)
|
|
889
|
+
this.headAt = ringAdd(base, RING_HEAD_AT)
|
|
777
890
|
this.doorbell = region.loadI32(ringAdd(base, RING_BELL_AT), RELAXED) !== 0
|
|
778
|
-
this.head = region.loadI64(
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
/** The offset of position `pos`'s slot. */
|
|
782
|
-
private slotAt(pos: i64): i32 {
|
|
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)))
|
|
891
|
+
this.head = region.loadI64(this.headAt, ACQUIRE)
|
|
785
892
|
}
|
|
786
893
|
|
|
787
894
|
/**
|
|
@@ -829,10 +936,13 @@ export class MpscRing {
|
|
|
829
936
|
if (len < 0 || len > this.slotBytes) {
|
|
830
937
|
return RING_TOO_LONG
|
|
831
938
|
}
|
|
832
|
-
const tailAt: i32 =
|
|
939
|
+
const tailAt: i32 = this.tailAt
|
|
833
940
|
let pos: i64 = this.region.loadI64(tailAt, RELAXED)
|
|
834
941
|
while (true) {
|
|
835
|
-
const seq: i64 = this.region.loadI64(
|
|
942
|
+
const seq: i64 = this.region.loadI64(
|
|
943
|
+
ringBackAt(this.lastSeqAt, ringBack(pos, this.mask), this.stride),
|
|
944
|
+
ACQUIRE
|
|
945
|
+
)
|
|
836
946
|
if (seq === pos) {
|
|
837
947
|
const seen: i64 = this.region.compareExchangeI64(tailAt, pos, ringPos(pos, toI64(1)), RELAXED)
|
|
838
948
|
if (seen === pos) {
|
|
@@ -852,9 +962,10 @@ export class MpscRing {
|
|
|
852
962
|
pos = again
|
|
853
963
|
}
|
|
854
964
|
}
|
|
855
|
-
const
|
|
856
|
-
this.
|
|
857
|
-
this.region.
|
|
965
|
+
const by: integer<0, 1073741823> = ringBack(pos, this.mask)
|
|
966
|
+
const slot: i32 = ringBackAt(this.lastSeqAt, by, this.stride)
|
|
967
|
+
this.region.storeI32(ringBackAt(this.lastLenAt, by, this.stride), len, RELAXED)
|
|
968
|
+
this.region.write(ringBackAt(this.lastPayloadAt, by, this.stride), src, off, len)
|
|
858
969
|
if (this.doorbell) {
|
|
859
970
|
this.region.storeI64(slot, ringPos(pos, toI64(1)), SEQ_CST)
|
|
860
971
|
ringRing(this.region, this.base)
|
|
@@ -866,25 +977,31 @@ export class MpscRing {
|
|
|
866
977
|
|
|
867
978
|
/** Whether the slot at the consumer's position is filled. */
|
|
868
979
|
private ready(order: i32): boolean {
|
|
869
|
-
return
|
|
980
|
+
return (
|
|
981
|
+
this.region.loadI64(ringBackAt(this.lastSeqAt, ringBack(this.head, this.mask), this.stride), order) ===
|
|
982
|
+
ringPos(this.head, toI64(1))
|
|
983
|
+
)
|
|
870
984
|
}
|
|
871
985
|
|
|
872
986
|
/** Empties the consumer's slot for the producer one lap on. */
|
|
873
987
|
private release(slot: i32): void {
|
|
874
988
|
this.region.storeI64(slot, ringPos(this.head, toI64(this.slots)), RELEASE)
|
|
875
989
|
this.head = ringPos(this.head, toI64(1))
|
|
876
|
-
this.region.storeI64(
|
|
990
|
+
this.region.storeI64(this.headAt, this.head, RELAXED)
|
|
877
991
|
}
|
|
878
992
|
|
|
879
|
-
/** The length in the consumer's slot, or `RING_CORRUPT` for one no producer writes. */
|
|
880
|
-
private lengthAt(
|
|
881
|
-
const len: i32 = this.region.loadI32(
|
|
993
|
+
/** The length in the consumer's slot, whose length word is at `at`, or `RING_CORRUPT` for one no producer writes. */
|
|
994
|
+
private lengthAt(at: i32): i32 {
|
|
995
|
+
const len: i32 = this.region.loadI32(at, RELAXED)
|
|
882
996
|
return len < 0 || len > this.slotBytes ? RING_CORRUPT : len
|
|
883
997
|
}
|
|
884
998
|
|
|
885
999
|
/** The panic `tryPop` and `popWait` give for a slot `take` found corrupt. */
|
|
886
1000
|
private corrupt(): void {
|
|
887
|
-
const len: i32 = this.region.loadI32(
|
|
1001
|
+
const len: i32 = this.region.loadI32(
|
|
1002
|
+
ringBackAt(this.lastLenAt, ringBack(this.head, this.mask), this.stride),
|
|
1003
|
+
RELAXED
|
|
1004
|
+
)
|
|
888
1005
|
panic(`nish/sync/ring: a record of length ${len} at position ${this.head}: the ring is corrupt`)
|
|
889
1006
|
}
|
|
890
1007
|
|
|
@@ -920,16 +1037,16 @@ export class MpscRing {
|
|
|
920
1037
|
if (!this.ready(ACQUIRE)) {
|
|
921
1038
|
return RING_EMPTY
|
|
922
1039
|
}
|
|
923
|
-
const
|
|
924
|
-
const len: i32 = this.lengthAt(
|
|
1040
|
+
const by: integer<0, 1073741823> = ringBack(this.head, this.mask)
|
|
1041
|
+
const len: i32 = this.lengthAt(ringBackAt(this.lastLenAt, by, this.stride))
|
|
925
1042
|
if (len === RING_CORRUPT) {
|
|
926
1043
|
return RING_CORRUPT
|
|
927
1044
|
}
|
|
928
|
-
if (len >
|
|
1045
|
+
if (toI64(len) + toI64(off) > toI64(dst.length)) {
|
|
929
1046
|
return RING_TOO_SMALL
|
|
930
1047
|
}
|
|
931
|
-
this.region.read(
|
|
932
|
-
this.release(
|
|
1048
|
+
this.region.read(ringBackAt(this.lastPayloadAt, by, this.stride), dst, off, len)
|
|
1049
|
+
this.release(ringBackAt(this.lastSeqAt, by, this.stride))
|
|
933
1050
|
return len
|
|
934
1051
|
}
|
|
935
1052
|
|
|
@@ -950,16 +1067,16 @@ export class MpscRing {
|
|
|
950
1067
|
let count: i32 = 0
|
|
951
1068
|
let used: i32 = 0
|
|
952
1069
|
while (count < toI32(lens.length) && this.ready(ACQUIRE)) {
|
|
953
|
-
const
|
|
954
|
-
const len: i32 = this.lengthAt(
|
|
1070
|
+
const by: integer<0, 1073741823> = ringBack(this.head, this.mask)
|
|
1071
|
+
const len: i32 = this.lengthAt(ringBackAt(this.lastLenAt, by, this.stride))
|
|
955
1072
|
if (len === RING_CORRUPT) {
|
|
956
1073
|
this.corrupt()
|
|
957
1074
|
}
|
|
958
|
-
if (len >
|
|
1075
|
+
if (toI64(len) + toI64(used) > toI64(dst.length)) {
|
|
959
1076
|
break
|
|
960
1077
|
}
|
|
961
|
-
this.region.read(
|
|
962
|
-
this.release(
|
|
1078
|
+
this.region.read(ringBackAt(this.lastPayloadAt, by, this.stride), dst, used, len)
|
|
1079
|
+
this.release(ringBackAt(this.lastSeqAt, by, this.stride))
|
|
963
1080
|
if (count >= 0 && count < toI32(lens.length)) {
|
|
964
1081
|
lens[count] = len
|
|
965
1082
|
}
|
|
@@ -1062,9 +1179,9 @@ export const initMpscRing = (
|
|
|
1062
1179
|
return made.value
|
|
1063
1180
|
}
|
|
1064
1181
|
|
|
1065
|
-
/** `mpscRingBytes` in
|
|
1182
|
+
/** `mpscRingBytes` in i64, where the most any i32 shape takes is far below 2^63. */
|
|
1066
1183
|
const mpscRingBytesWide = (slots: i32, slotBytes: i32): i64 =>
|
|
1067
|
-
toI64(
|
|
1184
|
+
toI64(RING_DATA_AT) + toI64(slots) * toI64(RING_SLOT_HEAD) + toI64(slots) * toI64(slotBytes)
|
|
1068
1185
|
|
|
1069
1186
|
/**
|
|
1070
1187
|
* `initMpscRing`, with a shape or a place it cannot use answered as an `Err`
|
package/std/sync/shm.ts
CHANGED
|
@@ -157,11 +157,18 @@ export class ShmError {
|
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
/**
|
|
160
|
-
* The errno a failed runtime call answered, negated back to positive
|
|
161
|
-
*
|
|
162
|
-
*
|
|
160
|
+
* The errno a failed runtime call answered, negated back to positive: `-rc`,
|
|
161
|
+
* spelled `~rc + 1`. The runtime only answers small errnos; a stray
|
|
162
|
+
* `-2147483648` from C, whose negation is past an `i32`, answers `2147483647`
|
|
163
|
+
* instead of overflowing, and the test with the literal is what proves the sum.
|
|
163
164
|
*/
|
|
164
|
-
const shmErrno = (rc: i32): i32 =>
|
|
165
|
+
const shmErrno = (rc: i32): i32 => {
|
|
166
|
+
const m: i32 = ~rc
|
|
167
|
+
if (m < 0 || m > 2147483646) {
|
|
168
|
+
return 2147483647
|
|
169
|
+
}
|
|
170
|
+
return m + 1
|
|
171
|
+
}
|
|
165
172
|
|
|
166
173
|
/** `Ok()` for 0, the error for a negated errno. */
|
|
167
174
|
const shmDone = (rc: i32): Result<void, ShmError> => {
|