@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
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
claimFileOffset,
|
|
3
|
+
shareFileResource
|
|
4
|
+
} from "../core/open-file.js";
|
|
5
|
+
import { errno as fsErrno, PAGE } from "../fs/protocol.js";
|
|
6
|
+
import {
|
|
7
|
+
cancelTimeout,
|
|
8
|
+
isInstance,
|
|
9
|
+
PromiseConstructor,
|
|
10
|
+
scheduleTimeout,
|
|
11
|
+
typedArrayLength,
|
|
12
|
+
typedArraySubarray
|
|
13
|
+
} from "../proc/intrinsics.js";
|
|
1
14
|
import { ProcessErrno, ProcessError } from "../proc/types.js";
|
|
2
15
|
export const DIAGNOSTIC_STALL_TIMEOUT_MS = 5000;
|
|
3
16
|
const now = performance.now.bind(performance);
|
|
4
|
-
const delay =
|
|
17
|
+
const delay = scheduleTimeout;
|
|
18
|
+
const mathMin = Math.min;
|
|
19
|
+
const mathMax = Math.max;
|
|
20
|
+
const numberIsInteger = Number.isInteger;
|
|
5
21
|
export function drainDiagnostic(bytes, write, active = () => true) {
|
|
6
22
|
let offset = 0, deadline = now() + DIAGNOSTIC_STALL_TIMEOUT_MS;
|
|
7
23
|
const step = () => {
|
|
@@ -40,3 +56,136 @@ export function drainDiagnostic(bytes, write, active = () => true) {
|
|
|
40
56
|
} while (!step());
|
|
41
57
|
})();
|
|
42
58
|
}
|
|
59
|
+
function diagnosticWriteError(error, remaining, total) {
|
|
60
|
+
if (isInstance(error, ProcessError))
|
|
61
|
+
return error;
|
|
62
|
+
const code = fsErrno(error);
|
|
63
|
+
return new ProcessError(code, `diagnostic write failed (${-code}); ${remaining} of ${total} diagnostic bytes unwritten`);
|
|
64
|
+
}
|
|
65
|
+
function settleAsyncDiagnosticChunk(completion, lease, remaining, total, callerSettled, releaseLease, onAccept, onReject) {
|
|
66
|
+
let written = 0;
|
|
67
|
+
try {
|
|
68
|
+
const committed = completion.tryConsumeCommitted();
|
|
69
|
+
const result = committed !== undefined ? committed : completion.consumeInline();
|
|
70
|
+
if (lease.append) {
|
|
71
|
+
const appended = result;
|
|
72
|
+
lease.commit(appended.offset);
|
|
73
|
+
written = appended.written;
|
|
74
|
+
} else {
|
|
75
|
+
written = result;
|
|
76
|
+
if (written > 0)
|
|
77
|
+
lease.commit(lease.position + written);
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
completion.release();
|
|
81
|
+
releaseLease();
|
|
82
|
+
if (!callerSettled)
|
|
83
|
+
onReject(diagnosticWriteError(error, remaining, total));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
completion.release();
|
|
87
|
+
releaseLease();
|
|
88
|
+
if (callerSettled)
|
|
89
|
+
return;
|
|
90
|
+
if (!numberIsInteger(written) || written <= 0 || written > remaining) {
|
|
91
|
+
onReject(new ProcessError(29, `diagnostic write failed (${written}); ${remaining} of ${total} diagnostic bytes unwritten`));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
onAccept(written);
|
|
95
|
+
}
|
|
96
|
+
function dispatchAsyncDiagnosticChunk(fs, handle, lease, bytes, offset, total, deadline, onWritten, onError) {
|
|
97
|
+
const remaining = total - offset;
|
|
98
|
+
let leaseReleased = false;
|
|
99
|
+
const releaseLease = () => {
|
|
100
|
+
if (leaseReleased)
|
|
101
|
+
return;
|
|
102
|
+
leaseReleased = true;
|
|
103
|
+
lease.release();
|
|
104
|
+
};
|
|
105
|
+
let completion;
|
|
106
|
+
try {
|
|
107
|
+
if (now() >= deadline) {
|
|
108
|
+
releaseLease();
|
|
109
|
+
onError(new ProcessError(ProcessErrno.AGAIN, `diagnostic reader made no progress for five seconds; ${remaining} of ${total} diagnostic bytes unwritten`));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const chunk = typedArraySubarray(bytes, offset, mathMin(total, offset + PAGE));
|
|
113
|
+
completion = lease.append ? fs.submit("append", [handle], chunk) : fs.submit("write", [handle, lease.position], chunk);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
releaseLease();
|
|
116
|
+
onError(diagnosticWriteError(error, remaining, total));
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
let callerSettled = false;
|
|
120
|
+
let settled = false;
|
|
121
|
+
const timer = delay(() => {
|
|
122
|
+
if (callerSettled)
|
|
123
|
+
return;
|
|
124
|
+
callerSettled = true;
|
|
125
|
+
onError(new ProcessError(ProcessErrno.AGAIN, `diagnostic reader made no progress for five seconds; ${remaining} of ${total} diagnostic bytes unwritten`));
|
|
126
|
+
}, mathMax(1, deadline - now()));
|
|
127
|
+
fs.trackDeferredWrite(completion, () => {
|
|
128
|
+
if (settled)
|
|
129
|
+
return;
|
|
130
|
+
settled = true;
|
|
131
|
+
cancelTimeout(timer);
|
|
132
|
+
settleAsyncDiagnosticChunk(completion, lease, remaining, total, callerSettled, releaseLease, (written) => {
|
|
133
|
+
if (callerSettled)
|
|
134
|
+
return;
|
|
135
|
+
callerSettled = true;
|
|
136
|
+
onWritten(written);
|
|
137
|
+
}, (error) => {
|
|
138
|
+
if (callerSettled)
|
|
139
|
+
return;
|
|
140
|
+
callerSettled = true;
|
|
141
|
+
onError(error);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
export function drainAsyncFileDiagnostic(fs, resource, bytes, active, stallTimeoutMs = DIAGNOSTIC_STALL_TIMEOUT_MS) {
|
|
146
|
+
return new PromiseConstructor((resolve, reject) => {
|
|
147
|
+
const total = typedArrayLength(bytes);
|
|
148
|
+
if (!total) {
|
|
149
|
+
resolve();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
shareFileResource(resource, fs.connection);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
reject(diagnosticWriteError(error, total, total));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
let offset = 0;
|
|
159
|
+
let deadline = now() + stallTimeoutMs;
|
|
160
|
+
const step = () => {
|
|
161
|
+
if (offset >= total) {
|
|
162
|
+
resolve();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const remaining = total - offset;
|
|
166
|
+
if (!active()) {
|
|
167
|
+
reject(new ProcessError(ProcessErrno.CANCELED, `diagnostic delivery cancelled; ${remaining} of ${total} diagnostic bytes unwritten`));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const claim = claimFileOffset(resource, true);
|
|
171
|
+
if (claim.kind === "failed") {
|
|
172
|
+
reject(new ProcessError(claim.errno, `diagnostic write failed (${-claim.errno}); ${remaining} of ${total} diagnostic bytes unwritten`));
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (claim.kind === "busy") {
|
|
176
|
+
if (now() >= deadline) {
|
|
177
|
+
reject(new ProcessError(ProcessErrno.AGAIN, `diagnostic reader made no progress for five seconds; ${remaining} of ${total} diagnostic bytes unwritten`));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
delay(step, 1);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
dispatchAsyncDiagnosticChunk(fs, resource.handle, claim.lease, bytes, offset, total, deadline, (written) => {
|
|
184
|
+
offset += written;
|
|
185
|
+
deadline = now() + stallTimeoutMs;
|
|
186
|
+
step();
|
|
187
|
+
}, reject);
|
|
188
|
+
};
|
|
189
|
+
step();
|
|
190
|
+
});
|
|
191
|
+
}
|
|
@@ -60,6 +60,8 @@ import {
|
|
|
60
60
|
directoryProcesses,
|
|
61
61
|
directoryAdmissionVersion,
|
|
62
62
|
commitDirectoryAdmission,
|
|
63
|
+
peekDirectoryJob,
|
|
64
|
+
peekDirectoryProcess,
|
|
63
65
|
isCollectedProcess,
|
|
64
66
|
wakeDirectory
|
|
65
67
|
} from "../proc/directory.js";
|
|
@@ -71,6 +73,7 @@ import {
|
|
|
71
73
|
} from "../proc/control.js";
|
|
72
74
|
import { isRevoked } from "../fs/protocol.js";
|
|
73
75
|
import {
|
|
76
|
+
checkedKillSignal,
|
|
74
77
|
checkedSignal,
|
|
75
78
|
checkedProcessCwd,
|
|
76
79
|
snapshotLogicalProcessIdentity,
|
|
@@ -79,6 +82,7 @@ import {
|
|
|
79
82
|
} from "../proc/types.js";
|
|
80
83
|
import { requestSignal, createSignalState } from "../proc/signals.js";
|
|
81
84
|
import {
|
|
85
|
+
OutcomeCell,
|
|
82
86
|
claimOutcome,
|
|
83
87
|
outcomeWord,
|
|
84
88
|
readOutcome,
|
|
@@ -2789,8 +2793,43 @@ export class BrowserProcessBroker {
|
|
|
2789
2793
|
states() {
|
|
2790
2794
|
return Array.from(this.roots.values()).flatMap((root) => Array.from(directoryProcesses(root.directory)));
|
|
2791
2795
|
}
|
|
2796
|
+
isDirectoryBackedJob(entry) {
|
|
2797
|
+
if (entry.shared)
|
|
2798
|
+
return true;
|
|
2799
|
+
const leader = entry.leaderPid === undefined ? undefined : this.options.table.get(entry.leaderPid);
|
|
2800
|
+
return leader !== undefined && isInstance(leader.outcome, OutcomeCell);
|
|
2801
|
+
}
|
|
2802
|
+
hasProcess(pid) {
|
|
2803
|
+
for (const root of this.roots.values())
|
|
2804
|
+
if (peekDirectoryProcess(root.directory, pid))
|
|
2805
|
+
return true;
|
|
2806
|
+
const record = this.options.table.get(pid);
|
|
2807
|
+
if (record && !isInstance(record.outcome, OutcomeCell))
|
|
2808
|
+
return true;
|
|
2809
|
+
const reserved = this.reservedRoots.get(pid);
|
|
2810
|
+
return reserved !== undefined && !isInstance(reserved.outcome, OutcomeCell);
|
|
2811
|
+
}
|
|
2812
|
+
hasJob(job, reserved) {
|
|
2813
|
+
if (reserved.length > 0)
|
|
2814
|
+
return true;
|
|
2815
|
+
for (const root of this.roots.values())
|
|
2816
|
+
if (peekDirectoryJob(root.directory, job))
|
|
2817
|
+
return true;
|
|
2818
|
+
const entry = this.options.table.job(job);
|
|
2819
|
+
if (entry && !this.isDirectoryBackedJob(entry))
|
|
2820
|
+
return true;
|
|
2821
|
+
for (const root of this.reservedRoots.values())
|
|
2822
|
+
if (root.job === job && !isInstance(root.outcome, OutcomeCell))
|
|
2823
|
+
return true;
|
|
2824
|
+
return false;
|
|
2825
|
+
}
|
|
2792
2826
|
async kill(pid, signal) {
|
|
2793
|
-
const sig =
|
|
2827
|
+
const sig = checkedKillSignal(signal);
|
|
2828
|
+
if (sig === 0) {
|
|
2829
|
+
if (this.suspended || this.hasProcess(pid))
|
|
2830
|
+
return;
|
|
2831
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
2832
|
+
}
|
|
2794
2833
|
const found = this.publishProcessSignal(pid, sig);
|
|
2795
2834
|
const supervisor = await this.ready();
|
|
2796
2835
|
if (this.suspended)
|
|
@@ -2833,7 +2872,13 @@ export class BrowserProcessBroker {
|
|
|
2833
2872
|
}
|
|
2834
2873
|
}
|
|
2835
2874
|
async killJob(job, signal, reserved = []) {
|
|
2836
|
-
const sig =
|
|
2875
|
+
const sig = checkedKillSignal(signal);
|
|
2876
|
+
if (sig === 0) {
|
|
2877
|
+
if (this.suspended || this.hasJob(job, reserved))
|
|
2878
|
+
return;
|
|
2879
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
|
|
2880
|
+
}
|
|
2881
|
+
const states = this.states().filter((state) => state.job === job);
|
|
2837
2882
|
const seen = new Set(states.map((state) => state.pid));
|
|
2838
2883
|
const records = Array.from(this.options.table.values()).filter((record) => record.job === job && !seen.has(record.pid));
|
|
2839
2884
|
for (const state of states)
|
package/dist/browser/worker.js
CHANGED
|
@@ -34,7 +34,11 @@ import {
|
|
|
34
34
|
import { checkedSignal, ProcessError } from "../proc/types.js";
|
|
35
35
|
import { defaultSignalTerminates, signalPending } from "../proc/signals.js";
|
|
36
36
|
import { FilesystemClient } from "../fs/client.js";
|
|
37
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
isRevoked,
|
|
39
|
+
revokeFilesystem,
|
|
40
|
+
reapFilesystem
|
|
41
|
+
} from "../fs/protocol.js";
|
|
38
42
|
import { sharedRevoked } from "../fs/shared.js";
|
|
39
43
|
import { registerResolveObservation } from "../fs/resolve-observation.js";
|
|
40
44
|
import { Wasi, WasiExit } from "../core/index.js";
|
|
@@ -44,7 +48,7 @@ import {
|
|
|
44
48
|
hostTerminalStream,
|
|
45
49
|
deliveredTerminalOutput
|
|
46
50
|
} from "./terminal-output.js";
|
|
47
|
-
import { drainDiagnostic } from "./diagnostic.js";
|
|
51
|
+
import { drainAsyncFileDiagnostic, drainDiagnostic } from "./diagnostic.js";
|
|
48
52
|
import { withFileOffset } from "../core/open-file.js";
|
|
49
53
|
import { ExecutableFormatError, startFailure } from "../proc/start-failure.js";
|
|
50
54
|
import { describeStartError } from "../proc/start-diagnostic.js";
|
|
@@ -517,9 +521,14 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
517
521
|
try {
|
|
518
522
|
const bytes = new TextEncoder().encode(`${describeWorkerError(result.error)}
|
|
519
523
|
`);
|
|
520
|
-
const
|
|
521
|
-
if (
|
|
522
|
-
await
|
|
524
|
+
const stderr = wasi?.descriptors.get(2);
|
|
525
|
+
if (stderr && (stderr.rightsBase & 1n << 6n) !== 0n && stderr.resource.kind === "file" && fs.shared.isDirectDisabled()) {
|
|
526
|
+
await drainAsyncFileDiagnostic(fs, stderr.resource, bytes, () => !isRevoked(fs.connection));
|
|
527
|
+
} else {
|
|
528
|
+
const pending = drainDiagnostic(bytes, (data) => writeDiagnostic(data));
|
|
529
|
+
if (pending)
|
|
530
|
+
await pending;
|
|
531
|
+
}
|
|
523
532
|
} catch (error) {
|
|
524
533
|
collect(error);
|
|
525
534
|
}
|
|
@@ -532,6 +541,13 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
532
541
|
attempt(() => executor.diagnostic?.(`${describeWorkerError(result.error)}
|
|
533
542
|
`));
|
|
534
543
|
}
|
|
544
|
+
if (fs.hasDeferredWrites?.()) {
|
|
545
|
+
try {
|
|
546
|
+
await fs.settleDeferredWrites?.();
|
|
547
|
+
} catch (error) {
|
|
548
|
+
collect(error);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
535
551
|
const descriptorsReleased = attempt(() => {
|
|
536
552
|
if (wasi) {
|
|
537
553
|
disposeProcessDescriptors(wasi, discardedStdio);
|
|
@@ -1143,7 +1159,9 @@ function installProcessImports(target, imports, checkActive, observedPoll) {
|
|
|
1143
1159
|
const call = name === "poll" && observedPoll ? observedPoll.wrap(entry[1]) : entry[1];
|
|
1144
1160
|
target[name] = (...args) => {
|
|
1145
1161
|
checkActive();
|
|
1146
|
-
|
|
1162
|
+
const result = call(...args);
|
|
1163
|
+
checkActive();
|
|
1164
|
+
return result;
|
|
1147
1165
|
};
|
|
1148
1166
|
}
|
|
1149
1167
|
}
|
package/dist/core/open-file.js
CHANGED
|
@@ -1,60 +1,100 @@
|
|
|
1
1
|
import { allocateSharedControl } from "../memory/allocator.js";
|
|
2
2
|
import { FilesystemError } from "./filesystem.js";
|
|
3
3
|
import { FilesystemSection, isRevoked } from "../fs/section.js";
|
|
4
|
-
const
|
|
4
|
+
const atomicsLoad = Atomics.load;
|
|
5
|
+
const atomicsStore = Atomics.store;
|
|
6
|
+
const atomicsCompareExchange = Atomics.compareExchange;
|
|
7
|
+
const atomicsOr = Atomics.or;
|
|
8
|
+
const atomicsAnd = Atomics.and;
|
|
9
|
+
const atomicsNotify = Atomics.notify;
|
|
10
|
+
const atomicsWait = Atomics.wait;
|
|
11
|
+
const Int32ArrayConstructor = Int32Array;
|
|
12
|
+
const BigUint64ArrayConstructor = BigUint64Array;
|
|
13
|
+
const Float64ArrayConstructor = Float64Array;
|
|
14
|
+
const WeakMapConstructor = WeakMap;
|
|
15
|
+
const weakMapGet = WeakMap.prototype.get;
|
|
16
|
+
const weakMapSet = WeakMap.prototype.set;
|
|
17
|
+
const objectDefineProperties = Object.defineProperties;
|
|
18
|
+
const typedArrayPrototype = Object.getPrototypeOf(Int32Array.prototype);
|
|
19
|
+
const typedArrayBufferGetter = Object.getOwnPropertyDescriptor(typedArrayPrototype, "buffer").get;
|
|
20
|
+
const reflectApply = Reflect.apply;
|
|
21
|
+
const offsets = new WeakMapConstructor;
|
|
22
|
+
function getSharedOffset(resource) {
|
|
23
|
+
return reflectApply(weakMapGet, offsets, [resource]);
|
|
24
|
+
}
|
|
5
25
|
export function shareFileResource(resource, identity, buffer) {
|
|
6
|
-
const existing =
|
|
26
|
+
const existing = getSharedOffset(resource);
|
|
7
27
|
if (existing)
|
|
8
28
|
return existing.buffer;
|
|
9
29
|
const shared = buffer ?? allocateSharedControl(16);
|
|
10
|
-
const cells = new
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
30
|
+
const cells = new Int32ArrayConstructor(shared);
|
|
31
|
+
const ownerStates = new Int32ArrayConstructor(identity.ownerStates);
|
|
32
|
+
const offset = new BigUint64ArrayConstructor(shared, 8, 1);
|
|
33
|
+
const value = new Float64ArrayConstructor(1);
|
|
34
|
+
const valueBuffer = reflectApply(typedArrayBufferGetter, value, []);
|
|
35
|
+
const bits = new BigUint64ArrayConstructor(valueBuffer);
|
|
36
|
+
const readOffset = () => {
|
|
37
|
+
bits[0] = atomicsLoad(offset, 0);
|
|
38
|
+
return value[0];
|
|
39
|
+
};
|
|
40
|
+
const writeOffset = (next) => {
|
|
41
|
+
value[0] = next;
|
|
42
|
+
atomicsStore(offset, 0, bits[0]);
|
|
43
|
+
};
|
|
44
|
+
const readAppend = () => (atomicsLoad(cells, 1) & 1) !== 0;
|
|
14
45
|
if (!buffer) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
Atomics.store(offset, 0, bits[0]);
|
|
46
|
+
atomicsStore(cells, 1, (resource.fdflags ?? 0) & ~1 | (resource.append ? 1 : 0));
|
|
47
|
+
writeOffset(resource.offset);
|
|
18
48
|
}
|
|
19
|
-
|
|
49
|
+
const state = {
|
|
20
50
|
buffer: shared,
|
|
21
51
|
cells,
|
|
52
|
+
ownerStates,
|
|
22
53
|
identity,
|
|
23
|
-
section: new FilesystemSection(identity)
|
|
24
|
-
|
|
25
|
-
|
|
54
|
+
section: new FilesystemSection(identity),
|
|
55
|
+
readOffset,
|
|
56
|
+
writeOffset,
|
|
57
|
+
readAppend
|
|
58
|
+
};
|
|
59
|
+
const descriptors = {
|
|
60
|
+
__proto__: null,
|
|
26
61
|
offset: {
|
|
62
|
+
__proto__: null,
|
|
27
63
|
enumerable: true,
|
|
28
64
|
configurable: false,
|
|
29
|
-
get:
|
|
30
|
-
|
|
31
|
-
return value[0];
|
|
32
|
-
},
|
|
33
|
-
set: (next) => {
|
|
34
|
-
value[0] = next;
|
|
35
|
-
Atomics.store(offset, 0, bits[0]);
|
|
36
|
-
}
|
|
65
|
+
get: readOffset,
|
|
66
|
+
set: writeOffset
|
|
37
67
|
},
|
|
38
68
|
append: {
|
|
69
|
+
__proto__: null,
|
|
39
70
|
enumerable: true,
|
|
40
71
|
configurable: false,
|
|
41
|
-
get:
|
|
42
|
-
set: (
|
|
72
|
+
get: readAppend,
|
|
73
|
+
set: (flag) => flag ? atomicsOr(cells, 1, 1) : atomicsAnd(cells, 1, ~1)
|
|
43
74
|
},
|
|
44
75
|
fdflags: {
|
|
76
|
+
__proto__: null,
|
|
45
77
|
enumerable: true,
|
|
46
78
|
configurable: false,
|
|
47
|
-
get: () =>
|
|
48
|
-
set: (
|
|
79
|
+
get: () => atomicsLoad(cells, 1),
|
|
80
|
+
set: (flags) => atomicsStore(cells, 1, flags)
|
|
49
81
|
}
|
|
50
|
-
}
|
|
82
|
+
};
|
|
83
|
+
objectDefineProperties(resource, descriptors);
|
|
84
|
+
reflectApply(weakMapSet, offsets, [resource, state]);
|
|
51
85
|
return shared;
|
|
52
86
|
}
|
|
53
|
-
export function claimFileOffset(resource) {
|
|
54
|
-
const
|
|
55
|
-
if (!
|
|
87
|
+
export function claimFileOffset(resource, holdSection = false) {
|
|
88
|
+
const claim = tryClaimFileOffset(resource, holdSection);
|
|
89
|
+
if (!claim)
|
|
56
90
|
throw new Error("async file offsets require a shared description");
|
|
57
|
-
|
|
91
|
+
return claim;
|
|
92
|
+
}
|
|
93
|
+
export function tryClaimFileOffset(resource, holdSection = false) {
|
|
94
|
+
const state = getSharedOffset(resource);
|
|
95
|
+
if (!state)
|
|
96
|
+
return;
|
|
97
|
+
const { cells, ownerStates, identity, section } = state;
|
|
58
98
|
try {
|
|
59
99
|
section.enter();
|
|
60
100
|
} catch (error) {
|
|
@@ -62,39 +102,61 @@ export function claimFileOffset(resource) {
|
|
|
62
102
|
return { kind: "failed", errno: error.errno };
|
|
63
103
|
throw error;
|
|
64
104
|
}
|
|
105
|
+
let handedToLease = false;
|
|
106
|
+
let acquired = false;
|
|
65
107
|
try {
|
|
66
|
-
const owner =
|
|
67
|
-
if (owner === 0)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
108
|
+
const owner = atomicsCompareExchange(cells, 0, 0, identity.owner);
|
|
109
|
+
if (owner === 0) {
|
|
110
|
+
acquired = true;
|
|
111
|
+
const lease = offsetLease(state, holdSection);
|
|
112
|
+
handedToLease = true;
|
|
113
|
+
return { kind: "acquired", lease };
|
|
114
|
+
}
|
|
115
|
+
if (atomicsLoad(ownerStates, owner) === 2)
|
|
116
|
+
atomicsCompareExchange(cells, 0, owner, 0);
|
|
71
117
|
return { kind: "busy", word: cells, observed: owner };
|
|
72
118
|
} finally {
|
|
73
|
-
|
|
119
|
+
if (!handedToLease) {
|
|
120
|
+
if (acquired) {
|
|
121
|
+
atomicsCompareExchange(cells, 0, identity.owner, 0);
|
|
122
|
+
atomicsNotify(cells, 0);
|
|
123
|
+
}
|
|
124
|
+
section.exit();
|
|
125
|
+
} else if (!holdSection) {
|
|
126
|
+
section.exit();
|
|
127
|
+
}
|
|
74
128
|
}
|
|
75
129
|
}
|
|
76
|
-
function offsetLease(
|
|
130
|
+
function offsetLease(state, holdSection) {
|
|
131
|
+
const { cells, identity, section } = state;
|
|
132
|
+
const position = state.readOffset();
|
|
133
|
+
const append = state.readAppend();
|
|
77
134
|
let phase = "held";
|
|
78
135
|
return {
|
|
79
|
-
position
|
|
80
|
-
append
|
|
136
|
+
position,
|
|
137
|
+
append,
|
|
81
138
|
commit(nextOffset) {
|
|
82
139
|
if (phase !== "held")
|
|
83
140
|
throw new Error("file offset lease is not uncommitted");
|
|
84
|
-
|
|
141
|
+
state.writeOffset(nextOffset);
|
|
85
142
|
phase = "committed";
|
|
86
143
|
},
|
|
87
144
|
release() {
|
|
88
145
|
if (phase === "released")
|
|
89
146
|
return;
|
|
90
147
|
phase = "released";
|
|
91
|
-
|
|
92
|
-
|
|
148
|
+
try {
|
|
149
|
+
atomicsCompareExchange(cells, 0, identity.owner, 0);
|
|
150
|
+
atomicsNotify(cells, 0);
|
|
151
|
+
} finally {
|
|
152
|
+
if (holdSection)
|
|
153
|
+
section.exit();
|
|
154
|
+
}
|
|
93
155
|
}
|
|
94
156
|
};
|
|
95
157
|
}
|
|
96
158
|
export function withFileOffset(resource, operation, nonblocking = false) {
|
|
97
|
-
const state =
|
|
159
|
+
const state = getSharedOffset(resource);
|
|
98
160
|
if (!state)
|
|
99
161
|
return operation();
|
|
100
162
|
const { cells, identity, section } = state;
|
|
@@ -116,26 +178,26 @@ export function withFileOffset(resource, operation, nonblocking = false) {
|
|
|
116
178
|
throw error;
|
|
117
179
|
} finally {
|
|
118
180
|
if (acquired) {
|
|
119
|
-
|
|
120
|
-
|
|
181
|
+
atomicsCompareExchange(cells, 0, identity.owner, 0);
|
|
182
|
+
atomicsNotify(cells, 0);
|
|
121
183
|
}
|
|
122
184
|
if (admitted)
|
|
123
185
|
section.exit();
|
|
124
186
|
}
|
|
125
187
|
}
|
|
126
|
-
function acquireFileOffset({ cells, identity }, nonblocking) {
|
|
188
|
+
function acquireFileOffset({ cells, ownerStates, identity }, nonblocking) {
|
|
127
189
|
for (;; ) {
|
|
128
190
|
if (isRevoked(identity))
|
|
129
191
|
return -11;
|
|
130
|
-
const owner =
|
|
192
|
+
const owner = atomicsCompareExchange(cells, 0, 0, identity.owner);
|
|
131
193
|
if (owner === 0)
|
|
132
194
|
return 0;
|
|
133
|
-
if (
|
|
134
|
-
|
|
195
|
+
if (atomicsLoad(ownerStates, owner) === 2) {
|
|
196
|
+
atomicsCompareExchange(cells, 0, owner, 0);
|
|
135
197
|
continue;
|
|
136
198
|
}
|
|
137
199
|
if (nonblocking)
|
|
138
200
|
return -6;
|
|
139
|
-
|
|
201
|
+
atomicsWait(cells, 0, owner, 10);
|
|
140
202
|
}
|
|
141
203
|
}
|