@waffle-os/runtime 0.0.149 → 0.0.151
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/browser/diagnostic.ts +234 -2
- package/browser/process-broker.ts +42 -4
- package/browser/worker.ts +34 -5
- package/core/open-file.ts +126 -52
- package/dist/browser/diagnostic.js +150 -1
- package/dist/browser/process-broker.js +47 -2
- package/dist/browser/worker.js +24 -6
- package/dist/core/open-file.js +112 -50
- package/dist/fs/client.js +290 -46
- package/dist/fs/protocol.js +7 -4
- package/dist/fs/service.js +77 -16
- package/dist/fs/shared.js +6 -6
- package/dist/interrupt/rewriter-internal.js +2 -1
- package/dist/proc/abi.js +152 -16
- package/dist/proc/command-stdio.js +81 -33
- package/dist/proc/directory.js +111 -0
- package/dist/proc/errors.js +1 -0
- package/dist/proc/supervisor.js +130 -24
- package/dist/proc/table.js +9 -1
- package/dist/proc/types.js +5 -0
- package/dist/proc/worker-client.js +34 -5
- package/dist/runtime.js +3 -0
- package/dist/thread/service.js +85 -10
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +298 -44
- package/fs/protocol.ts +9 -4
- package/fs/service.ts +99 -19
- package/fs/shared.ts +7 -7
- package/interrupt/rewriter-internal.ts +2 -1
- package/package.json +1 -1
- package/proc/abi.ts +185 -17
- package/proc/command-stdio.ts +98 -41
- package/proc/directory.ts +131 -0
- package/proc/errors.ts +1 -0
- package/proc/supervisor.ts +173 -36
- package/proc/table.ts +11 -0
- package/proc/types.ts +6 -0
- package/proc/worker-client.ts +36 -5
- package/runtime.ts +3 -0
- package/thread/service.ts +125 -7
- package/tooling/program/program-transformer.json +1 -1
package/dist/fs/service.js
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
retainResponseBytes,
|
|
8
8
|
captureRequestMemory,
|
|
9
9
|
closeRequestMemory,
|
|
10
|
+
requestMemoryRetired,
|
|
10
11
|
retireRequestMemory,
|
|
11
12
|
requestTransportError,
|
|
12
13
|
requestLengths,
|
|
@@ -79,6 +80,9 @@ const mutations = new Set([
|
|
|
79
80
|
]);
|
|
80
81
|
let connectObserved;
|
|
81
82
|
const RETAINED_DECODE_BYTES = 512;
|
|
83
|
+
function isCommittedScalarWrite(operation, state) {
|
|
84
|
+
return (operation === "write" || operation === "append") && state === State.Complete;
|
|
85
|
+
}
|
|
82
86
|
function sessionRevoked(session) {
|
|
83
87
|
return isRevoked(session.connection) || !!requestTransportError(session.requests);
|
|
84
88
|
}
|
|
@@ -195,6 +199,7 @@ export class FilesystemService {
|
|
|
195
199
|
stages: new Map,
|
|
196
200
|
retainedRequests: new Map,
|
|
197
201
|
pending: new Map,
|
|
202
|
+
inFlightWrites: new Set,
|
|
198
203
|
privateInputs: new Map,
|
|
199
204
|
preparingHandles: new Map,
|
|
200
205
|
preparations: new Set,
|
|
@@ -984,7 +989,8 @@ export class FilesystemService {
|
|
|
984
989
|
try {
|
|
985
990
|
this.finishSocketOperation(session, operation, result, args);
|
|
986
991
|
const pending = session.pending.get(id);
|
|
987
|
-
|
|
992
|
+
const scalarWrite = operation === "write" || operation === "append";
|
|
993
|
+
if (!pending || !scalarWrite && (sessionRevoked(session) || !this.sessions.has(session) || requestCancelled(pending)))
|
|
988
994
|
throw new FilesystemError(11);
|
|
989
995
|
if (operation === "preopen" || operation === "create" && typeof result === "number")
|
|
990
996
|
this.pin(session, result, operation === "preopen");
|
|
@@ -1062,6 +1068,7 @@ export class FilesystemService {
|
|
|
1062
1068
|
let opened = false;
|
|
1063
1069
|
let preparing = false;
|
|
1064
1070
|
let staging = false;
|
|
1071
|
+
let committedWrite = false;
|
|
1065
1072
|
let admission;
|
|
1066
1073
|
let dirtyRetry;
|
|
1067
1074
|
const epoch = session.epoch;
|
|
@@ -1098,10 +1105,13 @@ export class FilesystemService {
|
|
|
1098
1105
|
}
|
|
1099
1106
|
const payload = bytes.subarray(inputLength, inputLength + dataLength);
|
|
1100
1107
|
const preparedRead = prepared?.read && (!this.backend.isPreparedReadValid || this.backend.isPreparedReadValid()) ? prepared.read : undefined;
|
|
1108
|
+
if (operation === "write" || operation === "append")
|
|
1109
|
+
session.inFlightWrites.add(id);
|
|
1101
1110
|
const value = preparedRead ? this.finishResult(session, id, operation, preparedRead.value) : local && operation !== "flush" && operation !== "sync" ? this.executePublication(session, id, operation, args, payload, admission) : await this.execute(session, id, operation, args, payload);
|
|
1102
1111
|
this.announceSegments(this.backend.takeSharedSegments?.() ?? [], this.sessions);
|
|
1103
1112
|
state = this.writeResponse(session, id, cells, response, value, bytes);
|
|
1104
1113
|
staging = operation === "stageWrite";
|
|
1114
|
+
committedWrite = isCommittedScalarWrite(operation, state);
|
|
1105
1115
|
} catch (error) {
|
|
1106
1116
|
this.discardResponse(session, id);
|
|
1107
1117
|
const dirtyChange = this.dirtyRetryChange(error);
|
|
@@ -1126,22 +1136,20 @@ export class FilesystemService {
|
|
|
1126
1136
|
this.cancelPublication(session, admission);
|
|
1127
1137
|
this.releasePrepared(session, prepared);
|
|
1128
1138
|
cells[5] = Atomics.load(epoch, 0);
|
|
1129
|
-
|
|
1130
|
-
this.discardResponse(session, id);
|
|
1131
|
-
state = State.Cancelled;
|
|
1132
|
-
}
|
|
1133
|
-
if (staging && !session.stages.has(id))
|
|
1134
|
-
state = State.Cancelled;
|
|
1139
|
+
state = this.settleCancelledState(session, id, buffer, state, staging, committedWrite);
|
|
1135
1140
|
this.settleWalk(session, id, state === State.Complete);
|
|
1136
1141
|
this.settleWatch(session, id, state === State.Complete || state === State.Overflow);
|
|
1137
1142
|
Atomics.store(cells, 0, state);
|
|
1138
1143
|
Atomics.notify(cells, 0);
|
|
1139
1144
|
wake(session.connection);
|
|
1145
|
+
session.inFlightWrites.delete(id);
|
|
1140
1146
|
session.pending.delete(id);
|
|
1141
1147
|
session.privateInputs.delete(id);
|
|
1142
1148
|
this.retainRequestReply(session, id, buffer);
|
|
1143
1149
|
session.connection.port.postMessage({ completed: id });
|
|
1144
1150
|
this.sweep();
|
|
1151
|
+
} else {
|
|
1152
|
+
session.inFlightWrites.delete(id);
|
|
1145
1153
|
}
|
|
1146
1154
|
}
|
|
1147
1155
|
if (dirtyRetry !== undefined && prepared) {
|
|
@@ -1149,6 +1157,16 @@ export class FilesystemService {
|
|
|
1149
1157
|
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1150
1158
|
}
|
|
1151
1159
|
}
|
|
1160
|
+
settleCancelledState(session, id, buffer, state, staging, committedWrite) {
|
|
1161
|
+
if (sessionRevoked(session) || requestCancelled(buffer)) {
|
|
1162
|
+
this.discardResponse(session, id);
|
|
1163
|
+
if (!committedWrite)
|
|
1164
|
+
state = State.Cancelled;
|
|
1165
|
+
}
|
|
1166
|
+
if (staging && !session.stages.has(id))
|
|
1167
|
+
return State.Cancelled;
|
|
1168
|
+
return state;
|
|
1169
|
+
}
|
|
1152
1170
|
holdResponseMemory(session, id, release) {
|
|
1153
1171
|
const prior = session.responseMemory.get(id);
|
|
1154
1172
|
session.responseMemory.set(id, () => {
|
|
@@ -1524,6 +1542,16 @@ export class FilesystemService {
|
|
|
1524
1542
|
this.backend.shared.publication?.releasePin(id);
|
|
1525
1543
|
session.publicationPins?.clear();
|
|
1526
1544
|
}
|
|
1545
|
+
cancelPendingRequests(session) {
|
|
1546
|
+
for (const [id, buffer] of session.pending) {
|
|
1547
|
+
const cells = requestCells(buffer);
|
|
1548
|
+
Atomics.store(cells, 6, 1);
|
|
1549
|
+
Atomics.store(cells, 0, State.Cancelled);
|
|
1550
|
+
Atomics.notify(cells, 0);
|
|
1551
|
+
session.connection.port.postMessage({ completed: id });
|
|
1552
|
+
}
|
|
1553
|
+
session.pending.clear();
|
|
1554
|
+
}
|
|
1527
1555
|
releaseSessionPins(session) {
|
|
1528
1556
|
this.cancelSessionWatchResponses(session);
|
|
1529
1557
|
this.watchService?.closeOwner(session.connection.owner);
|
|
@@ -1549,18 +1577,13 @@ export class FilesystemService {
|
|
|
1549
1577
|
}));
|
|
1550
1578
|
return;
|
|
1551
1579
|
}
|
|
1580
|
+
if (session.inFlightWrites.size)
|
|
1581
|
+
return;
|
|
1552
1582
|
if (ownRevocation === 2)
|
|
1553
1583
|
this.backend.revokeOwner?.(session.connection.owner, session.epoch);
|
|
1554
1584
|
for (const id of Array.from(session.stages.keys()))
|
|
1555
1585
|
this.settleStage(session, id);
|
|
1556
|
-
|
|
1557
|
-
const cells = requestCells(buffer);
|
|
1558
|
-
Atomics.store(cells, 6, 1);
|
|
1559
|
-
Atomics.store(cells, 0, State.Cancelled);
|
|
1560
|
-
Atomics.notify(cells, 0);
|
|
1561
|
-
session.connection.port.postMessage({ completed: id });
|
|
1562
|
-
}
|
|
1563
|
-
session.pending.clear();
|
|
1586
|
+
this.cancelPendingRequests(session);
|
|
1564
1587
|
wake(session.connection);
|
|
1565
1588
|
for (const id of session.walkTransactions.keys())
|
|
1566
1589
|
this.settleWalk(session, id, false);
|
|
@@ -1706,7 +1729,45 @@ export class FilesystemService {
|
|
|
1706
1729
|
}
|
|
1707
1730
|
async shutdownFence(session) {
|
|
1708
1731
|
revokeFilesystemService(session.connection);
|
|
1709
|
-
|
|
1732
|
+
const quiesced = waitForFilesystemQuiescence(session.connection);
|
|
1733
|
+
let done = false;
|
|
1734
|
+
quiesced.finally(() => {
|
|
1735
|
+
done = true;
|
|
1736
|
+
wake(session.connection);
|
|
1737
|
+
});
|
|
1738
|
+
const signal = new Int32Array(session.connection.signal);
|
|
1739
|
+
while (!done) {
|
|
1740
|
+
const observed = Atomics.load(signal, 0);
|
|
1741
|
+
if (requestTransportError(session.requests)) {
|
|
1742
|
+
session.transportFailed = true;
|
|
1743
|
+
await this.drainFailedSessionRequests(session);
|
|
1744
|
+
break;
|
|
1745
|
+
}
|
|
1746
|
+
const waiting = atomicsWaitAsync(signal, 0, observed);
|
|
1747
|
+
if (waiting.async)
|
|
1748
|
+
await waiting.value;
|
|
1749
|
+
}
|
|
1750
|
+
await quiesced;
|
|
1751
|
+
}
|
|
1752
|
+
async drainFailedSessionRequests(session) {
|
|
1753
|
+
await waitForRequestQuiescence(session.connection, session.requests);
|
|
1754
|
+
for (;; ) {
|
|
1755
|
+
const queue = this.queue;
|
|
1756
|
+
await queue;
|
|
1757
|
+
const pending = Array.from(this.runningRequests).filter((done) => this.work.get(done) === session);
|
|
1758
|
+
if (pending.length)
|
|
1759
|
+
await Promise.allSettled(pending);
|
|
1760
|
+
if (session.preparations.size)
|
|
1761
|
+
await Promise.allSettled(Array.from(session.preparations));
|
|
1762
|
+
const stillRunning = Array.from(this.runningRequests).some((done) => this.work.get(done) === session);
|
|
1763
|
+
if (queue === this.queue && !stillRunning && !session.preparations.size && !session.inFlightWrites.size)
|
|
1764
|
+
break;
|
|
1765
|
+
}
|
|
1766
|
+
this.cancelPendingRequests(session);
|
|
1767
|
+
const failure = requestTransportError(session.requests);
|
|
1768
|
+
if (failure?.reason !== "stale" && !requestMemoryRetired(session.requests))
|
|
1769
|
+
retireRequestMemory(session.requests);
|
|
1770
|
+
wake(session.connection);
|
|
1710
1771
|
}
|
|
1711
1772
|
async drainShutdown(owner, fences) {
|
|
1712
1773
|
const failures = [];
|
package/dist/fs/shared.js
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
readIdentityWords,
|
|
12
12
|
writeIdentityWords
|
|
13
13
|
} from "./file-identity.js";
|
|
14
|
-
import { typedArraySet } from "../proc/intrinsics.js";
|
|
14
|
+
import { Int32ArrayConstructor, atomicsLoad, typedArraySet } from "../proc/intrinsics.js";
|
|
15
15
|
import {
|
|
16
16
|
PublicationDomain
|
|
17
17
|
} from "./publication.js";
|
|
@@ -213,10 +213,10 @@ export class SharedFilesystemState {
|
|
|
213
213
|
Atomics.store(this.cells, CLIENT_MODE, mode | 0);
|
|
214
214
|
}
|
|
215
215
|
isDirectDisabled() {
|
|
216
|
-
return (
|
|
216
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_DIRECT_IO) !== 0;
|
|
217
217
|
}
|
|
218
218
|
isClientCacheDisabled() {
|
|
219
|
-
return (
|
|
219
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_CLIENT_CACHE) !== 0;
|
|
220
220
|
}
|
|
221
221
|
resetForRemount(retainedSegments = []) {
|
|
222
222
|
Atomics.store(this.cells, GLOBAL_LOCK, 0);
|
|
@@ -1978,12 +1978,12 @@ export class SharedFilesystemState {
|
|
|
1978
1978
|
}
|
|
1979
1979
|
}
|
|
1980
1980
|
export function sharedRevoked(connection) {
|
|
1981
|
-
const words = [new
|
|
1981
|
+
const words = [new Int32ArrayConstructor(connection.ownerStates, 0, 1)].concat(connection.revocations.map((buffer) => new Int32ArrayConstructor(buffer)));
|
|
1982
1982
|
if (connection.publication)
|
|
1983
|
-
words.push(new
|
|
1983
|
+
words.push(new Int32ArrayConstructor(connection.publication.buffers.control, 0, 1));
|
|
1984
1984
|
return () => {
|
|
1985
1985
|
for (let i = 0;i < words.length; i++)
|
|
1986
|
-
if (
|
|
1986
|
+
if (atomicsLoad(words[i], 0) !== 0)
|
|
1987
1987
|
return true;
|
|
1988
1988
|
return false;
|
|
1989
1989
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const INTERRUPT_REWRITER_REVISION =
|
|
1
|
+
export const INTERRUPT_REWRITER_REVISION = 8;
|
|
2
2
|
const encoder = new TextEncoder;
|
|
3
3
|
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
4
4
|
const HEADER = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]);
|
|
@@ -27,6 +27,7 @@ const runtimeProcSignatures = new Map([
|
|
|
27
27
|
["wait_job", { params: [127, 127, 127], results: [127] }],
|
|
28
28
|
["kill", { params: [127, 127], results: [127] }],
|
|
29
29
|
["kill_job", { params: [127, 127], results: [127] }],
|
|
30
|
+
["probe_process", { params: [127], results: [127] }],
|
|
30
31
|
["start_status", { params: [127], results: [127] }],
|
|
31
32
|
["poll", { params: [127, 127, 127, 127], results: [127] }],
|
|
32
33
|
["interrupted", { params: [], results: [127] }],
|
package/dist/proc/abi.js
CHANGED
|
@@ -29,7 +29,10 @@ import { controlLoad, controlWait } from "./control.js";
|
|
|
29
29
|
import {
|
|
30
30
|
MAX_DESCRIPTOR
|
|
31
31
|
} from "../core/descriptors.js";
|
|
32
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
tryClaimFileOffset,
|
|
34
|
+
withFileOffset
|
|
35
|
+
} from "../core/open-file.js";
|
|
33
36
|
import { PipeController } from "./pipes.js";
|
|
34
37
|
import { readOutcome } from "./outcome.js";
|
|
35
38
|
import { WasiExit } from "../core/wasi.js";
|
|
@@ -50,6 +53,7 @@ import {
|
|
|
50
53
|
ProcessError,
|
|
51
54
|
Signal,
|
|
52
55
|
checkedJobLifetime,
|
|
56
|
+
checkedKillSignal,
|
|
53
57
|
checkedProcessCwd,
|
|
54
58
|
checkedProcessKind,
|
|
55
59
|
snapshotLogicalProcessIdentity
|
|
@@ -94,6 +98,9 @@ function releasePollWakes(unregister) {
|
|
|
94
98
|
function sameWord(first, second) {
|
|
95
99
|
return first !== undefined && typedArrayBuffer(first) === typedArrayBuffer(second) && typedArrayByteOffset(first) === typedArrayByteOffset(second) && typedArrayLength(first) === 1;
|
|
96
100
|
}
|
|
101
|
+
function validWakeWord(word) {
|
|
102
|
+
return word && isSharedArrayBuffer(typedArrayBuffer(word)) && typedArrayLength(word) === 1 ? word : undefined;
|
|
103
|
+
}
|
|
97
104
|
|
|
98
105
|
export class ProcessABI {
|
|
99
106
|
options;
|
|
@@ -140,17 +147,30 @@ export class ProcessABI {
|
|
|
140
147
|
wait: (pid, ptr) => this.result(() => this.wait(pid, ptr)),
|
|
141
148
|
wait_job: (job, flags, ptr) => this.result(() => this.waitJob(job, flags, ptr)),
|
|
142
149
|
kill: (pid, signal) => this.result(() => {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
150
|
+
const sig = checkedKillSignal(signal);
|
|
151
|
+
const allowed = sig === 0 && options.service.canProbe ? options.service.canProbe(options.pid, pid) : options.service.canSignal?.(options.pid, pid);
|
|
152
|
+
if (allowed === false)
|
|
153
|
+
return -ProcessErrno.PERM;
|
|
154
|
+
if (sig === 0 && allowed === true)
|
|
155
|
+
return 0;
|
|
156
|
+
options.service.kill(pid, sig);
|
|
146
157
|
return 0;
|
|
147
158
|
}),
|
|
148
159
|
kill_job: (job, signal) => this.result(() => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
160
|
+
const sig = checkedKillSignal(signal);
|
|
161
|
+
const allowed = sig === 0 && options.service.canProbeJob ? options.service.canProbeJob(options.pid, job) : options.service.canSignalJob?.(options.pid, job);
|
|
162
|
+
if (allowed === false)
|
|
163
|
+
return -ProcessErrno.PERM;
|
|
164
|
+
if (sig === 0 && allowed === true)
|
|
165
|
+
return 0;
|
|
166
|
+
options.service.killJob(job, sig);
|
|
152
167
|
return 0;
|
|
153
168
|
}),
|
|
169
|
+
probe_process: (pid) => this.result(() => {
|
|
170
|
+
if (!options.service.canSignalProcess)
|
|
171
|
+
return -NOSYS;
|
|
172
|
+
return options.service.canSignalProcess(options.pid, pid) ? 0 : -ProcessErrno.PERM;
|
|
173
|
+
}),
|
|
154
174
|
start_status: (pid) => this.result(() => options.service.startStatus(options.pid, pid)),
|
|
155
175
|
poll: (ptr, count, timeout, out) => this.result(() => this.poll(ptr, count, timeout, out)),
|
|
156
176
|
interrupted: () => this.interrupted(),
|
|
@@ -387,6 +407,84 @@ export class ProcessABI {
|
|
|
387
407
|
throw new ProcessError(NOTCAPABLE, "descriptor rights denied");
|
|
388
408
|
return entry;
|
|
389
409
|
}
|
|
410
|
+
settleDeferredWriteLease(fs, completion, lease, append) {
|
|
411
|
+
const finish = () => {
|
|
412
|
+
try {
|
|
413
|
+
const committed = completion.tryConsumeCommitted();
|
|
414
|
+
if (committed !== undefined) {
|
|
415
|
+
if (append)
|
|
416
|
+
lease.commit(committed.offset);
|
|
417
|
+
else if (typeof committed === "number" && committed > 0)
|
|
418
|
+
lease.commit(lease.position + committed);
|
|
419
|
+
}
|
|
420
|
+
} catch {} finally {
|
|
421
|
+
completion.release();
|
|
422
|
+
lease.release();
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
if (fs.trackDeferredWrite)
|
|
426
|
+
fs.trackDeferredWrite(completion, finish);
|
|
427
|
+
else
|
|
428
|
+
completion.onBackendSettled(finish);
|
|
429
|
+
}
|
|
430
|
+
directDisabledFileIO(write, resource, bytes, fs) {
|
|
431
|
+
this.checkFatal();
|
|
432
|
+
if (this.pending())
|
|
433
|
+
return -ProcessErrno.AGAIN;
|
|
434
|
+
const claim = tryClaimFileOffset(resource, true);
|
|
435
|
+
if (claim?.kind === "failed") {
|
|
436
|
+
this.checkFatal();
|
|
437
|
+
return -claim.errno;
|
|
438
|
+
}
|
|
439
|
+
if (claim?.kind === "busy")
|
|
440
|
+
return -ProcessErrno.AGAIN;
|
|
441
|
+
const lease = claim?.kind === "acquired" ? claim.lease : undefined;
|
|
442
|
+
let releaseLease = true;
|
|
443
|
+
let outcome;
|
|
444
|
+
try {
|
|
445
|
+
const append = write && (lease ? lease.append : resource.append);
|
|
446
|
+
const position = lease ? lease.position : resource.offset;
|
|
447
|
+
const op = append ? "append" : write ? "write" : "read";
|
|
448
|
+
const commitOffset = (result) => {
|
|
449
|
+
if (typeof result !== "number") {
|
|
450
|
+
if (lease)
|
|
451
|
+
lease.commit(result.offset);
|
|
452
|
+
else
|
|
453
|
+
resource.offset = result.offset;
|
|
454
|
+
return result.written;
|
|
455
|
+
}
|
|
456
|
+
if (result > 0) {
|
|
457
|
+
if (lease)
|
|
458
|
+
lease.commit(position + result);
|
|
459
|
+
else
|
|
460
|
+
resource.offset = position + result;
|
|
461
|
+
}
|
|
462
|
+
return result;
|
|
463
|
+
};
|
|
464
|
+
const result = fs.directDisabledIO(op, resource.handle, bytes, position, {
|
|
465
|
+
checkFatal: () => this.checkFatal(),
|
|
466
|
+
pending: () => this.pending(),
|
|
467
|
+
wait: (ms, word, expected) => {
|
|
468
|
+
assertTerminalWaitAllowed();
|
|
469
|
+
if (this.options.wait)
|
|
470
|
+
this.options.wait(ms, word, expected);
|
|
471
|
+
else
|
|
472
|
+
controlWait(word, 0, expected, ms);
|
|
473
|
+
},
|
|
474
|
+
commitWrite: commitOffset,
|
|
475
|
+
deferWrite: lease ? (completion) => {
|
|
476
|
+
releaseLease = false;
|
|
477
|
+
this.settleDeferredWriteLease(fs, completion, lease, append);
|
|
478
|
+
} : undefined
|
|
479
|
+
});
|
|
480
|
+
outcome = commitOffset(result);
|
|
481
|
+
} finally {
|
|
482
|
+
if (lease && releaseLease)
|
|
483
|
+
lease.release();
|
|
484
|
+
}
|
|
485
|
+
this.checkFatal();
|
|
486
|
+
return outcome;
|
|
487
|
+
}
|
|
390
488
|
io(write, fd, ptr, length) {
|
|
391
489
|
const entry = this.descriptor(fd, write ? WRITE : READ), resource = entry.resource;
|
|
392
490
|
if (resource.kind === "directory")
|
|
@@ -396,6 +494,8 @@ export class ProcessABI {
|
|
|
396
494
|
return 0;
|
|
397
495
|
if (resource.kind === "file") {
|
|
398
496
|
const fs = this.options.fs;
|
|
497
|
+
if (fs.directDisabled && fs.directDisabledIO)
|
|
498
|
+
return this.directDisabledFileIO(write, resource, bytes, fs);
|
|
399
499
|
return withFileOffset(resource, () => {
|
|
400
500
|
if (write && resource.append) {
|
|
401
501
|
const result = fs.tryAppend ? fs.tryAppend(resource.handle, [bytes]) : fs.append(resource.handle, [bytes]);
|
|
@@ -725,6 +825,41 @@ export class ProcessABI {
|
|
|
725
825
|
}
|
|
726
826
|
return fullyNotified;
|
|
727
827
|
}
|
|
828
|
+
pollFilesystemWait(sub) {
|
|
829
|
+
if (sub.kind === 3)
|
|
830
|
+
return this.options.service.filesystemReady ? "none" : "pending";
|
|
831
|
+
if (sub.kind !== 0 && sub.kind !== 1)
|
|
832
|
+
return "none";
|
|
833
|
+
const resource = this.descriptor(sub.id, POLL).resource;
|
|
834
|
+
if (resource.kind !== "file")
|
|
835
|
+
return "none";
|
|
836
|
+
const fs = this.options.fs;
|
|
837
|
+
return fs.ioPending?.(resource.handle, sub.kind === 1) ? "pending" : "file";
|
|
838
|
+
}
|
|
839
|
+
scanPollSubscriptions(subscriptions, timeout, hasFsSignal) {
|
|
840
|
+
const ready = [];
|
|
841
|
+
let waitOnFs = false;
|
|
842
|
+
let sawFile = false;
|
|
843
|
+
let allFs = subscriptions.length > 0;
|
|
844
|
+
for (const sub of subscriptions) {
|
|
845
|
+
const state = this.readiness(sub, timeout);
|
|
846
|
+
if (state && ((sub.kind === 1 ? state.writable : state.readable) || state.hangup)) {
|
|
847
|
+
ready.push({ sub, flags: state.hangup ? 1 : 0 });
|
|
848
|
+
continue;
|
|
849
|
+
}
|
|
850
|
+
if (!hasFsSignal)
|
|
851
|
+
continue;
|
|
852
|
+
const fsWait = this.pollFilesystemWait(sub);
|
|
853
|
+
if (fsWait === "none")
|
|
854
|
+
allFs = false;
|
|
855
|
+
else if (fsWait === "pending") {
|
|
856
|
+
waitOnFs = true;
|
|
857
|
+
sawFile = true;
|
|
858
|
+
} else
|
|
859
|
+
sawFile = true;
|
|
860
|
+
}
|
|
861
|
+
return { ready, waitOnFs, sawFile, allFs };
|
|
862
|
+
}
|
|
728
863
|
poll(ptr, count, timeout, out) {
|
|
729
864
|
count >>>= 0;
|
|
730
865
|
if (!Number.isInteger(timeout) || timeout < -1)
|
|
@@ -749,20 +884,17 @@ export class ProcessABI {
|
|
|
749
884
|
return 0;
|
|
750
885
|
const unregister = [];
|
|
751
886
|
const wake = this.options.wake;
|
|
887
|
+
const fsWord = validWakeWord(this.options.fs.signal);
|
|
752
888
|
try {
|
|
753
889
|
const fullyNotified = this.registerPollWakes(subscriptions, wake, unregister);
|
|
754
890
|
const deadline = timeout === -1 ? 1 / 0 : this.now() + timeout;
|
|
755
891
|
for (;; ) {
|
|
756
892
|
const word = wake ?? this.options.signals.interrupt;
|
|
757
893
|
const sequence = controlLoad(word, 0);
|
|
894
|
+
const fsSequence = fsWord ? controlLoad(fsWord, 0) : 0;
|
|
758
895
|
if (this.pending())
|
|
759
896
|
return 0;
|
|
760
|
-
const ready =
|
|
761
|
-
for (const sub of subscriptions) {
|
|
762
|
-
const state = this.readiness(sub, timeout);
|
|
763
|
-
if (state && ((sub.kind === 1 ? state.writable : state.readable) || state.hangup))
|
|
764
|
-
ready.push({ sub, flags: state.hangup ? 1 : 0 });
|
|
765
|
-
}
|
|
897
|
+
const { ready, waitOnFs, sawFile, allFs } = this.scanPollSubscriptions(subscriptions, timeout, fsWord !== undefined);
|
|
766
898
|
if (ready.length) {
|
|
767
899
|
const output = this.view(out, ready.length * 16);
|
|
768
900
|
ready.forEach(({ sub, flags }, index) => {
|
|
@@ -778,13 +910,17 @@ export class ProcessABI {
|
|
|
778
910
|
const remaining = deadline - this.now();
|
|
779
911
|
if (remaining <= 0)
|
|
780
912
|
return 0;
|
|
913
|
+
if (allFs && sawFile && fsWord && controlLoad(fsWord, 0) !== fsSequence)
|
|
914
|
+
continue;
|
|
915
|
+
const useFsWake = allFs && waitOnFs && fsWord !== undefined;
|
|
916
|
+
const waitWord = useFsWake ? fsWord : word;
|
|
917
|
+
const expected = useFsWake ? fsSequence : sequence;
|
|
781
918
|
const milliseconds = fullyNotified ? remaining : Math.min(remaining, 10);
|
|
782
|
-
const expected = sequence;
|
|
783
919
|
assertTerminalWaitAllowed();
|
|
784
920
|
if (this.options.wait)
|
|
785
|
-
this.options.wait(milliseconds,
|
|
921
|
+
this.options.wait(milliseconds, waitWord, expected);
|
|
786
922
|
else
|
|
787
|
-
controlWait(
|
|
923
|
+
controlWait(waitWord, 0, expected, milliseconds);
|
|
788
924
|
}
|
|
789
925
|
} finally {
|
|
790
926
|
for (let index = unregister.length - 1;index >= 0; index--)
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { atomicsWaitAsync } from "./intrinsics.js";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
claimFileOffset,
|
|
4
|
+
withFileOffset,
|
|
5
|
+
shareFileResource
|
|
6
|
+
} from "../core/open-file.js";
|
|
3
7
|
import { FilesystemClient } from "../fs/client.js";
|
|
4
8
|
import { errno } from "../fs/protocol.js";
|
|
5
9
|
import { CommandStdioError } from "./command.js";
|
|
@@ -107,30 +111,81 @@ export class CommandStdio {
|
|
|
107
111
|
await this.wait(words, observed);
|
|
108
112
|
}
|
|
109
113
|
}
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
const
|
|
113
|
-
lease.commit(
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
commitAsyncWrite(lease, result) {
|
|
115
|
+
if (lease.append) {
|
|
116
|
+
const appended = result;
|
|
117
|
+
lease.commit(appended.offset);
|
|
118
|
+
return appended.written;
|
|
119
|
+
}
|
|
120
|
+
const written = result;
|
|
121
|
+
if (written > 0)
|
|
122
|
+
lease.commit(lease.position + written);
|
|
123
|
+
return written;
|
|
124
|
+
}
|
|
125
|
+
deferAsyncWrite(filesystem, completion, lease) {
|
|
126
|
+
filesystem.trackDeferredWrite(completion, () => {
|
|
127
|
+
try {
|
|
128
|
+
const settled = completion.tryConsumeCommitted();
|
|
129
|
+
if (settled !== undefined)
|
|
130
|
+
this.commitAsyncWrite(lease, settled);
|
|
131
|
+
} catch {} finally {
|
|
132
|
+
completion.release();
|
|
133
|
+
lease.release();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async writeAsyncFileLease(filesystem, resource, lease, bytes, onDefer) {
|
|
138
|
+
const completion = lease.append ? filesystem.submit("append", [resource.handle], bytes) : filesystem.submit("write", [resource.handle, lease.position], bytes);
|
|
139
|
+
let handedOff = false;
|
|
140
|
+
try {
|
|
141
|
+
const result = await completion.value();
|
|
142
|
+
return this.commitAsyncWrite(lease, result);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
const committed = completion.tryConsumeCommitted();
|
|
145
|
+
if (committed !== undefined) {
|
|
146
|
+
this.commitAsyncWrite(lease, committed);
|
|
147
|
+
} else {
|
|
148
|
+
handedOff = true;
|
|
149
|
+
onDefer();
|
|
150
|
+
this.deferAsyncWrite(filesystem, completion, lease);
|
|
151
|
+
}
|
|
152
|
+
throw error;
|
|
153
|
+
} finally {
|
|
154
|
+
if (!handedOff)
|
|
155
|
+
completion.release();
|
|
116
156
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
157
|
+
}
|
|
158
|
+
async runLeasedTransfer(filesystem, resource, lease, bytes, writing) {
|
|
159
|
+
let releaseLease = true;
|
|
160
|
+
const defer = () => {
|
|
161
|
+
releaseLease = false;
|
|
162
|
+
};
|
|
163
|
+
try {
|
|
164
|
+
if (writing) {
|
|
165
|
+
const written = await this.writeAsyncFileLease(filesystem, resource, lease, bytes, defer);
|
|
166
|
+
lease.release();
|
|
167
|
+
releaseLease = false;
|
|
168
|
+
this.check();
|
|
169
|
+
return written;
|
|
170
|
+
}
|
|
171
|
+
const read = await filesystem.asyncCall("read", [
|
|
172
|
+
resource.handle,
|
|
173
|
+
bytes.length,
|
|
174
|
+
lease.position
|
|
175
|
+
]);
|
|
176
|
+
bytes.set(read, 0);
|
|
177
|
+
if (read.length > 0)
|
|
178
|
+
lease.commit(lease.position + read.length);
|
|
121
179
|
this.check();
|
|
122
|
-
return
|
|
180
|
+
return read.length;
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (error instanceof CommandStdioError)
|
|
183
|
+
throw error;
|
|
184
|
+
throw stdioError(-errno(error));
|
|
185
|
+
} finally {
|
|
186
|
+
if (releaseLease)
|
|
187
|
+
lease.release();
|
|
123
188
|
}
|
|
124
|
-
const read = await filesystem.asyncCall("read", [
|
|
125
|
-
resource.handle,
|
|
126
|
-
bytes.length,
|
|
127
|
-
lease.position
|
|
128
|
-
]);
|
|
129
|
-
bytes.set(read, 0);
|
|
130
|
-
if (read.length > 0)
|
|
131
|
-
lease.commit(lease.position + read.length);
|
|
132
|
-
this.check();
|
|
133
|
-
return read.length;
|
|
134
189
|
}
|
|
135
190
|
async transferAsyncFile(resource, bytes, writing) {
|
|
136
191
|
const filesystem = this.filesystem;
|
|
@@ -139,23 +194,14 @@ export class CommandStdio {
|
|
|
139
194
|
shareFileResource(resource, filesystem.connection);
|
|
140
195
|
for (;; ) {
|
|
141
196
|
this.check();
|
|
142
|
-
const claim = claimFileOffset(resource);
|
|
197
|
+
const claim = claimFileOffset(resource, writing);
|
|
143
198
|
if (claim.kind === "failed")
|
|
144
199
|
throw stdioError(-claim.errno);
|
|
145
200
|
if (claim.kind === "busy") {
|
|
146
201
|
await this.wait([claim.word], [claim.observed]);
|
|
147
202
|
continue;
|
|
148
203
|
}
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
return await this.runAsyncFileLease(filesystem, resource, lease, bytes, writing);
|
|
152
|
-
} catch (error) {
|
|
153
|
-
if (error instanceof CommandStdioError)
|
|
154
|
-
throw error;
|
|
155
|
-
throw stdioError(-errno(error));
|
|
156
|
-
} finally {
|
|
157
|
-
lease.release();
|
|
158
|
-
}
|
|
204
|
+
return await this.runLeasedTransfer(filesystem, resource, claim.lease, bytes, writing);
|
|
159
205
|
}
|
|
160
206
|
}
|
|
161
207
|
notifications(resource) {
|
|
@@ -211,6 +257,8 @@ export class CommandStdio {
|
|
|
211
257
|
async settle() {
|
|
212
258
|
while (this.pending.size)
|
|
213
259
|
await Promise.allSettled(Array.from(this.pending));
|
|
260
|
+
if (this.filesystem?.hasDeferredWrites())
|
|
261
|
+
await this.filesystem.settleDeferredWrites();
|
|
214
262
|
await this.filesystem?.stopResidentPublication();
|
|
215
263
|
this.filesystem?.dispose();
|
|
216
264
|
}
|