@amritk/nish-aarch64-linux 0.37.0 → 0.39.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 +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +380 -47
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +645 -66
- package/runtime/nish.h +101 -15
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +267 -51
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +170 -8
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +18 -3
- package/runtime/runtime.c +55 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +113 -36
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +7 -4
- package/std/array.ts +266 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/host/text.ts +86 -0
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +142 -16
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +517 -154
- package/std/net/http1-server.ts +508 -25
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +2223 -0
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +129 -11
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +867 -0
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +464 -5
package/runtime/runtime-async.c
CHANGED
|
@@ -49,9 +49,35 @@
|
|
|
49
49
|
* number is reused for. The duplicate is closed when the wait fires.
|
|
50
50
|
*
|
|
51
51
|
* **Regions.** Each spawned task owns a region (runtime-region.c), entered
|
|
52
|
-
* whenever the task runs and
|
|
52
|
+
* whenever the task runs and emptied when it finishes, so whatever a task
|
|
53
53
|
* allocates is given back when it ends. The driver enters and leaves around
|
|
54
|
-
* each resume (`nish_async_enter`, `nish_async_leave`).
|
|
54
|
+
* each resume (`nish_async_enter`, `nish_async_leave`). A node keeps its
|
|
55
|
+
* region from its first task on: `nish_async_done` enters it once more and
|
|
56
|
+
* rewinds it (`nish_reset_arena`) rather than freeing it, so the node's next
|
|
57
|
+
* task (a later spawn of the same async function: a node never changes
|
|
58
|
+
* kind) makes no region, and bumps from the newest chunk the last one left
|
|
59
|
+
* when that chunk is at most 64 KiB (`NISH_ASYNC_KEEP`), so a task that fits
|
|
60
|
+
* it asks `malloc` for nothing. The region keeps its handle and generation:
|
|
61
|
+
* the handle never leaves this file, and a task's region has no children
|
|
62
|
+
* (NL2351), so no copy of it is made stale by living on. A larger chunk is
|
|
63
|
+
* freed, and once the free nodes keep 16 MiB between them
|
|
64
|
+
* (`NISH_ASYNC_KEEP_TOTAL`) a finishing task keeps none and its region starts
|
|
65
|
+
* again from its first chunk size, so a burst of large tasks does not pin its
|
|
66
|
+
* high-water mark. The kept chunk is not zeroed: arena memory never is, and
|
|
67
|
+
* the compiler initialises what the language promises
|
|
68
|
+
* (docs/security/async.md, ASYNC-20).
|
|
69
|
+
*
|
|
70
|
+
* **Cancellation.** `nish_async_cancel` marks a task cancelled
|
|
71
|
+
* (`NISH_TASK_CANCELLED`, a bit beside the queued bit) and fails each wait it
|
|
72
|
+
* has pending, so every parked await answers false and the task is queued; a
|
|
73
|
+
* wait the task makes afterwards is made already failed. Nothing is unwound:
|
|
74
|
+
* the task runs on from each failed await to its end, its `using` bindings
|
|
75
|
+
* dispose as they do on any return, and `nish_async_done` empties its region.
|
|
76
|
+
* `nish_async_cancel_after` arms a deadline that cancels a task when it
|
|
77
|
+
* passes, a timer in the heap that `done` takes out, and
|
|
78
|
+
* `nish_async_wait_fd_within` a descriptor wait that fails when its own
|
|
79
|
+
* deadline passes first. A timer leaves the heap the moment its wait is
|
|
80
|
+
* answered either way (`heap_at`), so the heap holds only live timers.
|
|
55
81
|
*
|
|
56
82
|
* The executor runs on the thread that runs `main`; its state is not shared
|
|
57
83
|
* with the threads of `nish/threads`. */
|
|
@@ -94,11 +120,16 @@ typedef struct nish_task {
|
|
|
94
120
|
struct nish_task *next; /* the ready queue's link, or the free list's */
|
|
95
121
|
int32_t kind; /* the spawned function's kind; -1 for `main` */
|
|
96
122
|
uint32_t gen; /* moves on each time the node is reused */
|
|
97
|
-
uint64_t region; /* the task's region; 0 for `main` and a
|
|
123
|
+
uint64_t region; /* the task's region; 0 for `main` and a node never yet used */
|
|
98
124
|
uint32_t index; /* the node's place in the task table, for ever */
|
|
99
|
-
uint32_t
|
|
125
|
+
uint32_t flags; /* NISH_TASK_QUEUED, NISH_TASK_CANCELLED; on the free list, the kept bytes */
|
|
100
126
|
} nish_task;
|
|
101
127
|
|
|
128
|
+
/* On the ready queue, so it is queued once. */
|
|
129
|
+
#define NISH_TASK_QUEUED 1u
|
|
130
|
+
/* Cancelled: each wait it has, or makes, answers false at once. */
|
|
131
|
+
#define NISH_TASK_CANCELLED 2u
|
|
132
|
+
|
|
102
133
|
_Static_assert(sizeof(nish_task) <= NISH_ASYNC_HEADER, "a node's header holds a task");
|
|
103
134
|
_Static_assert(offsetof(nish_task, kind) == 8, "the driver reads the kind at byte 8");
|
|
104
135
|
|
|
@@ -119,6 +150,8 @@ typedef struct nish_wait {
|
|
|
119
150
|
int32_t fd; /* the registered duplicate, or -1 for a timer */
|
|
120
151
|
int64_t deadline; /* a timer's, in CLOCK_MONOTONIC nanoseconds */
|
|
121
152
|
uint64_t seq; /* a timer's place among equal deadlines */
|
|
153
|
+
int32_t heap_at; /* its place in the timer heap, or -1 */
|
|
154
|
+
int32_t cancels; /* a task's deadline: firing cancels the task, waking nothing */
|
|
122
155
|
} nish_wait;
|
|
123
156
|
|
|
124
157
|
static nish_task nish_async_main_task = {NULL, -1, 1, 0, 0, 0};
|
|
@@ -145,12 +178,26 @@ static uint32_t nish_async_heap_len;
|
|
|
145
178
|
static uint32_t nish_async_heap_cap;
|
|
146
179
|
static uint64_t nish_async_seq;
|
|
147
180
|
|
|
181
|
+
/* Per task index, the slot + 1 of its armed deadline, or 0. */
|
|
182
|
+
static uint32_t *nish_async_deadlines;
|
|
183
|
+
static uint32_t nish_async_deadline_cap;
|
|
184
|
+
|
|
148
185
|
static int nish_async_poller = -1;
|
|
149
186
|
static uint32_t nish_async_fd_waits;
|
|
150
187
|
|
|
151
188
|
static int32_t nish_async_allocated;
|
|
152
189
|
static int32_t nish_async_late;
|
|
153
190
|
static int32_t nish_async_live; /* spawned tasks not yet done, each holding a region */
|
|
191
|
+
/* What a finished task's region may keep for the node's next task: its newest
|
|
192
|
+
chunk when that is no larger than NISH_ASYNC_KEEP, and nothing once the
|
|
193
|
+
free nodes together keep NISH_ASYNC_KEEP_TOTAL, so the most the free lists
|
|
194
|
+
hold is that total and one chunk. A free node's `flags` hold what its
|
|
195
|
+
region kept, from bit NISH_TASK_KEPT_SHIFT up. */
|
|
196
|
+
#define NISH_ASYNC_KEEP ((uint64_t)1 << 16)
|
|
197
|
+
#define NISH_ASYNC_KEEP_TOTAL ((uint64_t)1 << 24)
|
|
198
|
+
#define NISH_TASK_KEPT_SHIFT 8
|
|
199
|
+
static uint64_t nish_async_kept; /* bytes the free nodes' regions keep */
|
|
200
|
+
extern NISH_TLS uint64_t nish_region_chunk; /* runtime.c: the entered region's next chunk size */
|
|
154
201
|
|
|
155
202
|
enum { NISH_ASYNC_OOM, NISH_ASYNC_POLLER, NISH_ASYNC_TOO_MANY, NISH_ASYNC_WAIT };
|
|
156
203
|
#define NISH_ASYNC_MSG(text) { sizeof text - 1, text }
|
|
@@ -183,6 +230,11 @@ static void nish_async_table_add(nish_task *t) {
|
|
|
183
230
|
}
|
|
184
231
|
if (nish_async_task_count == nish_async_task_cap)
|
|
185
232
|
nish_async_tasks = nish_async_grow(nish_async_tasks, &nish_async_task_cap, sizeof *nish_async_tasks);
|
|
233
|
+
while (nish_async_task_count >= nish_async_deadline_cap) {
|
|
234
|
+
uint32_t had = nish_async_deadline_cap;
|
|
235
|
+
nish_async_deadlines = nish_async_grow(nish_async_deadlines, &nish_async_deadline_cap, sizeof *nish_async_deadlines);
|
|
236
|
+
memset(nish_async_deadlines + had, 0, (size_t)(nish_async_deadline_cap - had) * sizeof *nish_async_deadlines);
|
|
237
|
+
}
|
|
186
238
|
t->index = nish_async_task_count;
|
|
187
239
|
nish_async_tasks[nish_async_task_count++] = t;
|
|
188
240
|
}
|
|
@@ -194,8 +246,8 @@ static int64_t nish_async_now(void) {
|
|
|
194
246
|
}
|
|
195
247
|
|
|
196
248
|
static void nish_async_enqueue(nish_task *t) {
|
|
197
|
-
if (t->
|
|
198
|
-
t->
|
|
249
|
+
if (t->flags & NISH_TASK_QUEUED) return;
|
|
250
|
+
t->flags |= NISH_TASK_QUEUED;
|
|
199
251
|
t->next = NULL;
|
|
200
252
|
if (nish_async_tail) nish_async_tail->next = t;
|
|
201
253
|
else nish_async_head = t;
|
|
@@ -228,7 +280,7 @@ void *nish_async_spawn(int32_t kind, uint64_t bytes) {
|
|
|
228
280
|
nish_task *t = (nish_task *)(chunk + i * slab->stride);
|
|
229
281
|
t->gen = 1;
|
|
230
282
|
t->region = 0;
|
|
231
|
-
t->
|
|
283
|
+
t->flags = 0;
|
|
232
284
|
nish_async_table_add(t);
|
|
233
285
|
t->next = slab->free;
|
|
234
286
|
slab->free = t;
|
|
@@ -239,8 +291,9 @@ void *nish_async_spawn(int32_t kind, uint64_t bytes) {
|
|
|
239
291
|
slab->free = t->next;
|
|
240
292
|
t->next = NULL;
|
|
241
293
|
t->kind = kind;
|
|
242
|
-
t->
|
|
243
|
-
t->
|
|
294
|
+
nish_async_kept -= t->flags >> NISH_TASK_KEPT_SHIFT;
|
|
295
|
+
t->flags = 0;
|
|
296
|
+
if (!t->region) t->region = nish_region_new();
|
|
244
297
|
nish_async_live++;
|
|
245
298
|
return t;
|
|
246
299
|
}
|
|
@@ -261,12 +314,40 @@ void nish_async_leave(void *node, void *prev) {
|
|
|
261
314
|
nish_async_current = prev;
|
|
262
315
|
}
|
|
263
316
|
|
|
264
|
-
|
|
317
|
+
static void nish_async_disarm(nish_task *t);
|
|
318
|
+
|
|
319
|
+
/* A task finished: take out its deadline, empty its region for the node's
|
|
320
|
+
next task and put its node back, a generation on. */
|
|
265
321
|
void nish_async_done(void *node) {
|
|
266
322
|
nish_task *t = node;
|
|
267
323
|
if (t->kind < 0) return;
|
|
268
|
-
|
|
269
|
-
|
|
324
|
+
nish_async_disarm(t);
|
|
325
|
+
/* A task that cancelled itself while awaitAll ran its calls was queued by
|
|
326
|
+
its own failed waits and may finish still on the ready queue: take it
|
|
327
|
+
off, so the queue never holds a node on the free list. */
|
|
328
|
+
if (t->flags & NISH_TASK_QUEUED) {
|
|
329
|
+
nish_task **at = &nish_async_head;
|
|
330
|
+
nish_task *before = NULL;
|
|
331
|
+
while (*at && *at != t) {
|
|
332
|
+
before = *at;
|
|
333
|
+
at = &(*at)->next;
|
|
334
|
+
}
|
|
335
|
+
if (*at) {
|
|
336
|
+
*at = t->next;
|
|
337
|
+
if (nish_async_tail == t) nish_async_tail = before;
|
|
338
|
+
}
|
|
339
|
+
t->flags &= ~NISH_TASK_QUEUED;
|
|
340
|
+
}
|
|
341
|
+
nish_region_enter(t->region);
|
|
342
|
+
nish_reset_arena();
|
|
343
|
+
if (nish_arena.cap > (nish_async_kept < NISH_ASYNC_KEEP_TOTAL ? NISH_ASYNC_KEEP : 0)) {
|
|
344
|
+
nish_free_arena();
|
|
345
|
+
nish_region_chunk = 8;
|
|
346
|
+
}
|
|
347
|
+
uint64_t kept = nish_arena.cap;
|
|
348
|
+
nish_region_leave(t->region, NULL);
|
|
349
|
+
nish_async_kept += kept;
|
|
350
|
+
t->flags = (uint32_t)kept << NISH_TASK_KEPT_SHIFT;
|
|
270
351
|
nish_async_live--;
|
|
271
352
|
t->gen = t->gen == UINT32_MAX ? 1 : t->gen + 1;
|
|
272
353
|
nish_slab *slab = &nish_async_slabs[t->kind];
|
|
@@ -311,6 +392,8 @@ static uint32_t nish_async_wait_new(void) {
|
|
|
311
392
|
w->fd = -1;
|
|
312
393
|
w->deadline = 0;
|
|
313
394
|
w->seq = 0;
|
|
395
|
+
w->heap_at = -1;
|
|
396
|
+
w->cancels = 0;
|
|
314
397
|
return slot;
|
|
315
398
|
}
|
|
316
399
|
|
|
@@ -328,11 +411,80 @@ static uint64_t nish_async_fired_now(uint32_t slot, int32_t answer) {
|
|
|
328
411
|
return nish_async_token(slot);
|
|
329
412
|
}
|
|
330
413
|
|
|
414
|
+
/* ---- The timer heap ---------------------------------------------------- */
|
|
415
|
+
|
|
416
|
+
static int nish_async_timer_before(uint32_t a, uint32_t b) {
|
|
417
|
+
nish_wait *x = &nish_async_waits[a];
|
|
418
|
+
nish_wait *y = &nish_async_waits[b];
|
|
419
|
+
return x->deadline < y->deadline || (x->deadline == y->deadline && x->seq < y->seq);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
static void nish_async_heap_set(uint32_t at, uint32_t slot) {
|
|
423
|
+
nish_async_heap[at] = slot;
|
|
424
|
+
nish_async_waits[slot].heap_at = (int32_t)at;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
static void nish_async_heap_up(uint32_t at) {
|
|
428
|
+
uint32_t slot = nish_async_heap[at];
|
|
429
|
+
while (at > 0) {
|
|
430
|
+
uint32_t up = (at - 1) / 2;
|
|
431
|
+
if (!nish_async_timer_before(slot, nish_async_heap[up])) break;
|
|
432
|
+
nish_async_heap_set(at, nish_async_heap[up]);
|
|
433
|
+
at = up;
|
|
434
|
+
}
|
|
435
|
+
nish_async_heap_set(at, slot);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
static void nish_async_heap_down(uint32_t at) {
|
|
439
|
+
uint32_t slot = nish_async_heap[at];
|
|
440
|
+
for (;;) {
|
|
441
|
+
uint32_t kid = at * 2 + 1;
|
|
442
|
+
if (kid >= nish_async_heap_len) break;
|
|
443
|
+
if (kid + 1 < nish_async_heap_len && nish_async_timer_before(nish_async_heap[kid + 1], nish_async_heap[kid]))
|
|
444
|
+
kid++;
|
|
445
|
+
if (!nish_async_timer_before(nish_async_heap[kid], slot)) break;
|
|
446
|
+
nish_async_heap_set(at, nish_async_heap[kid]);
|
|
447
|
+
at = kid;
|
|
448
|
+
}
|
|
449
|
+
nish_async_heap_set(at, slot);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
static void nish_async_heap_push(uint32_t slot) {
|
|
453
|
+
if (nish_async_heap_len == nish_async_heap_cap)
|
|
454
|
+
nish_async_heap = nish_async_grow(nish_async_heap, &nish_async_heap_cap, sizeof *nish_async_heap);
|
|
455
|
+
nish_async_heap_set(nish_async_heap_len++, slot);
|
|
456
|
+
nish_async_heap_up(nish_async_heap_len - 1);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/* Take the timer of `slot` out of the heap, wherever it is. */
|
|
460
|
+
static void nish_async_heap_remove(uint32_t slot) {
|
|
461
|
+
int32_t at = nish_async_waits[slot].heap_at;
|
|
462
|
+
if (at < 0) return;
|
|
463
|
+
nish_async_waits[slot].heap_at = -1;
|
|
464
|
+
uint32_t last = nish_async_heap[--nish_async_heap_len];
|
|
465
|
+
if ((uint32_t)at == nish_async_heap_len) return;
|
|
466
|
+
nish_async_heap_set((uint32_t)at, last);
|
|
467
|
+
nish_async_heap_up((uint32_t)at);
|
|
468
|
+
nish_async_heap_down((uint32_t)nish_async_waits[last].heap_at);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/* ---- Answering a wait ---------------------------------------------------- */
|
|
472
|
+
|
|
473
|
+
static nish_task *nish_async_task_at(uint32_t index, uint32_t gen) {
|
|
474
|
+
nish_task *t = index == 0 ? &nish_async_main_task
|
|
475
|
+
: index < nish_async_task_count ? nish_async_tasks[index] : NULL;
|
|
476
|
+
return t && t->gen == gen ? t : NULL;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
static void nish_async_cancel_task(nish_task *t);
|
|
480
|
+
|
|
331
481
|
/* The wait in `slot` completed with `answer`: queue its task, or drop it when
|
|
332
|
-
the task it was made for has finished.
|
|
482
|
+
the task it was made for has finished. A task's deadline cancels the task
|
|
483
|
+
instead. */
|
|
333
484
|
static void nish_async_fire(uint32_t slot, int32_t answer) {
|
|
334
485
|
nish_wait *w = &nish_async_waits[slot];
|
|
335
486
|
if (w->state != NISH_WAIT_PENDING) return;
|
|
487
|
+
nish_async_heap_remove(slot);
|
|
336
488
|
if (w->fd >= 0) {
|
|
337
489
|
#if defined(__linux__)
|
|
338
490
|
epoll_ctl(nish_async_poller, EPOLL_CTL_DEL, w->fd, NULL);
|
|
@@ -343,9 +495,14 @@ static void nish_async_fire(uint32_t slot, int32_t answer) {
|
|
|
343
495
|
}
|
|
344
496
|
w->state = NISH_WAIT_FIRED;
|
|
345
497
|
w->answer = answer;
|
|
346
|
-
nish_task *t = w->task
|
|
347
|
-
|
|
348
|
-
|
|
498
|
+
nish_task *t = nish_async_task_at(w->task, w->task_gen);
|
|
499
|
+
if (w->cancels) {
|
|
500
|
+
if (nish_async_deadlines[w->task] == slot + 1) nish_async_deadlines[w->task] = 0;
|
|
501
|
+
nish_async_wait_free_slot(slot);
|
|
502
|
+
if (t) nish_async_cancel_task(t);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (!t) {
|
|
349
506
|
nish_async_late++;
|
|
350
507
|
nish_async_wait_free_slot(slot);
|
|
351
508
|
return;
|
|
@@ -353,46 +510,90 @@ static void nish_async_fire(uint32_t slot, int32_t answer) {
|
|
|
353
510
|
nish_async_enqueue(t);
|
|
354
511
|
}
|
|
355
512
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
513
|
+
/* A timer came due: a sleep is over (true), a descriptor wait's deadline
|
|
514
|
+
passed first (false), a task's deadline cancels it. */
|
|
515
|
+
static void nish_async_expire(uint32_t slot) {
|
|
516
|
+
nish_async_fire(slot, nish_async_waits[slot].fd >= 0 ? 0 : 1);
|
|
360
517
|
}
|
|
361
518
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
519
|
+
/* ---- Cancellation -------------------------------------------------------- */
|
|
520
|
+
|
|
521
|
+
/* Mark `t` cancelled and fail each wait it has pending. */
|
|
522
|
+
static void nish_async_cancel_task(nish_task *t) {
|
|
523
|
+
if (t->flags & NISH_TASK_CANCELLED) return;
|
|
524
|
+
t->flags |= NISH_TASK_CANCELLED;
|
|
525
|
+
nish_async_disarm(t);
|
|
526
|
+
for (uint32_t slot = 0; slot < nish_async_wait_count; slot++) {
|
|
527
|
+
nish_wait *w = &nish_async_waits[slot];
|
|
528
|
+
if (w->state == NISH_WAIT_PENDING && !w->cancels && w->task == t->index && w->task_gen == t->gen)
|
|
529
|
+
nish_async_fire(slot, 0);
|
|
371
530
|
}
|
|
372
|
-
nish_async_heap[at] = slot;
|
|
373
531
|
}
|
|
374
532
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
uint32_t
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
return
|
|
533
|
+
/* Take out `t`'s armed deadline, if it has one. */
|
|
534
|
+
static void nish_async_disarm(nish_task *t) {
|
|
535
|
+
if (!nish_async_deadlines || t->index >= nish_async_deadline_cap) return;
|
|
536
|
+
uint32_t armed = nish_async_deadlines[t->index];
|
|
537
|
+
if (!armed) return;
|
|
538
|
+
nish_async_deadlines[t->index] = 0;
|
|
539
|
+
nish_async_heap_remove(armed - 1);
|
|
540
|
+
nish_async_wait_free_slot(armed - 1);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/* The task `handle` names (`nish_async_task_id`), or null for one that has
|
|
544
|
+
finished, `main`, or no task. */
|
|
545
|
+
static nish_task *nish_async_task_of(int64_t handle) {
|
|
546
|
+
uint32_t index = (uint32_t)((uint64_t)handle & 0xffffffffu);
|
|
547
|
+
if (index == 0) return NULL;
|
|
548
|
+
return nish_async_task_at(index, (uint32_t)((uint64_t)handle >> 32));
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/* The running task's handle: its generation, then its index; 0 for `main`. */
|
|
552
|
+
int64_t nish_async_task_id(void) {
|
|
553
|
+
nish_task *t = nish_async_current;
|
|
554
|
+
if (t == &nish_async_main_task) return 0;
|
|
555
|
+
return (int64_t)(((uint64_t)t->gen << 32) | t->index);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/* Cancel the task `handle` names: true when it is still running (a second
|
|
559
|
+
cancel too), false for one that has finished, or `main`. */
|
|
560
|
+
int32_t nish_async_cancel(int64_t handle) {
|
|
561
|
+
nish_task *t = nish_async_task_of(handle);
|
|
562
|
+
if (!t) return 0;
|
|
563
|
+
nish_async_cancel_task(t);
|
|
564
|
+
return 1;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/* Whether the running task is cancelled. */
|
|
568
|
+
int32_t nish_async_cancelled(void) {
|
|
569
|
+
return (nish_async_current->flags & NISH_TASK_CANCELLED) != 0;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/* Cancel the task `handle` names `ms` milliseconds from now, in place of any
|
|
573
|
+
deadline it had; a negative `ms` only takes the old one out. False for a
|
|
574
|
+
task that has finished, or `main`. */
|
|
575
|
+
int32_t nish_async_cancel_after(int64_t handle, int32_t ms) {
|
|
576
|
+
nish_task *t = nish_async_task_of(handle);
|
|
577
|
+
if (!t) return 0;
|
|
578
|
+
nish_async_disarm(t);
|
|
579
|
+
if (ms < 0 || (t->flags & NISH_TASK_CANCELLED)) return 1;
|
|
580
|
+
nish_task *running = nish_async_current;
|
|
581
|
+
nish_async_current = t;
|
|
582
|
+
uint32_t slot = nish_async_wait_new();
|
|
583
|
+
nish_async_current = running;
|
|
584
|
+
nish_wait *w = &nish_async_waits[slot];
|
|
585
|
+
w->cancels = 1;
|
|
586
|
+
w->deadline = nish_async_now() + (int64_t)ms * 1000000;
|
|
587
|
+
w->seq = nish_async_seq++;
|
|
588
|
+
nish_async_heap_push(slot);
|
|
589
|
+
nish_async_deadlines[t->index] = slot + 1;
|
|
590
|
+
return 1;
|
|
390
591
|
}
|
|
391
592
|
|
|
392
593
|
/* A wait for `fd` to be readable (`dir` 0) or writable (`dir` 1). */
|
|
393
594
|
uint64_t nish_async_wait_fd(int32_t fd, int32_t dir) {
|
|
394
595
|
uint32_t slot = nish_async_wait_new();
|
|
395
|
-
if (fd < 0) return nish_async_fired_now(slot, 0);
|
|
596
|
+
if (fd < 0 || (nish_async_current->flags & NISH_TASK_CANCELLED)) return nish_async_fired_now(slot, 0);
|
|
396
597
|
if (nish_async_poller < 0) {
|
|
397
598
|
#if defined(__linux__)
|
|
398
599
|
nish_async_poller = epoll_create1(EPOLL_CLOEXEC);
|
|
@@ -438,10 +639,23 @@ uint64_t nish_async_wait_fd(int32_t fd, int32_t dir) {
|
|
|
438
639
|
return token;
|
|
439
640
|
}
|
|
440
641
|
|
|
441
|
-
/* A wait
|
|
642
|
+
/* A descriptor wait that fails (answers false) when `ms` milliseconds pass
|
|
643
|
+
before `fd` is ready; a negative `ms` waits as long as it takes. */
|
|
644
|
+
uint64_t nish_async_wait_fd_within(int32_t fd, int32_t dir, int32_t ms) {
|
|
645
|
+
uint64_t token = nish_async_wait_fd(fd, dir);
|
|
646
|
+
uint32_t slot = (uint32_t)(token & 0xffffffffu) - 1;
|
|
647
|
+
nish_wait *w = &nish_async_waits[slot];
|
|
648
|
+
if (ms < 0 || w->state != NISH_WAIT_PENDING) return token;
|
|
649
|
+
w->deadline = nish_async_now() + (int64_t)ms * 1000000;
|
|
650
|
+
w->seq = nish_async_seq++;
|
|
651
|
+
nish_async_heap_push(slot);
|
|
652
|
+
return token;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/* A wait for `ms` milliseconds; a negative `ms`, or a cancelled task, answers false at once. */
|
|
442
656
|
uint64_t nish_async_sleep(int32_t ms) {
|
|
443
657
|
uint32_t slot = nish_async_wait_new();
|
|
444
|
-
if (ms < 0) return nish_async_fired_now(slot, 0);
|
|
658
|
+
if (ms < 0 || (nish_async_current->flags & NISH_TASK_CANCELLED)) return nish_async_fired_now(slot, 0);
|
|
445
659
|
nish_async_waits[slot].deadline = nish_async_now() + (int64_t)ms * 1000000;
|
|
446
660
|
nish_async_waits[slot].seq = nish_async_seq++;
|
|
447
661
|
nish_async_heap_push(slot);
|
|
@@ -519,7 +733,7 @@ static void nish_async_block(void) {
|
|
|
519
733
|
}
|
|
520
734
|
int64_t now = nish_async_now();
|
|
521
735
|
while (nish_async_heap_len && nish_async_waits[nish_async_heap[0]].deadline <= now)
|
|
522
|
-
|
|
736
|
+
nish_async_expire(nish_async_heap[0]);
|
|
523
737
|
}
|
|
524
738
|
|
|
525
739
|
/* The next task to resume, waiting on the kernel for one when none is ready;
|
|
@@ -533,7 +747,7 @@ void *nish_async_next(void) {
|
|
|
533
747
|
nish_async_head = t->next;
|
|
534
748
|
if (!nish_async_head) nish_async_tail = NULL;
|
|
535
749
|
t->next = NULL;
|
|
536
|
-
t->
|
|
750
|
+
t->flags &= ~NISH_TASK_QUEUED;
|
|
537
751
|
return t;
|
|
538
752
|
}
|
|
539
753
|
|
|
@@ -546,11 +760,13 @@ void *nish_async_copy_str(const void *s) {
|
|
|
546
760
|
}
|
|
547
761
|
|
|
548
762
|
/* 0: nodes the slabs ever allocated; 1: completions dropped because their task
|
|
549
|
-
had finished; 2: spawned tasks not yet finished, each of which holds a region
|
|
763
|
+
had finished; 2: spawned tasks not yet finished, each of which holds a region;
|
|
764
|
+
3: timers in the heap (sleeps, bounded waits and armed deadlines). */
|
|
550
765
|
int32_t nish_async_stat(int32_t which) {
|
|
551
766
|
if (which == 0) return nish_async_allocated;
|
|
552
767
|
if (which == 1) return nish_async_late;
|
|
553
768
|
if (which == 2) return nish_async_live;
|
|
769
|
+
if (which == 3) return (int32_t)nish_async_heap_len;
|
|
554
770
|
return -1;
|
|
555
771
|
}
|
|
556
772
|
|