@waffle-os/runtime 0.0.148 → 0.0.150
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 +6 -3
- package/browser/run-command.ts +18 -7
- 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 +3 -2
- package/dist/browser/run-command.js +8 -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/proc/abi.js +132 -10
- package/dist/proc/command-stdio.js +81 -33
- package/dist/proc/registry.js +8 -8
- package/dist/proc/start-command.js +10 -0
- package/dist/proc/supervisor.js +4 -4
- 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/package.json +1 -1
- package/proc/abi.ts +160 -13
- package/proc/command-stdio.ts +98 -41
- package/proc/registry.ts +13 -19
- package/proc/start-command.ts +29 -0
- package/proc/supervisor.ts +8 -6
- 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
|
+
}
|
|
@@ -256,7 +256,7 @@ export class BrowserProcessBroker {
|
|
|
256
256
|
return this.activation ??= (this.options.activate?.() ?? import("../proc/index.js")).then((proc) => {
|
|
257
257
|
const supervisor = this.supervisor = new proc.ProcessSupervisor({
|
|
258
258
|
registry: this.options.registry,
|
|
259
|
-
|
|
259
|
+
commandPrograms: (rootPid) => rootPid === undefined ? undefined : this.commandRoots?.get(rootPid)?.programs,
|
|
260
260
|
table: this.options.table,
|
|
261
261
|
launch: (context) => this.launchPrepared(context),
|
|
262
262
|
launchCommand: (context) => this.launchPrepared(context),
|
|
@@ -920,7 +920,7 @@ export class BrowserProcessBroker {
|
|
|
920
920
|
this.activateRuntimeRoot(supervisor, transport);
|
|
921
921
|
}
|
|
922
922
|
}
|
|
923
|
-
async adoptCommandRoot(transport, options) {
|
|
923
|
+
async adoptCommandRoot(transport, options, programs) {
|
|
924
924
|
if (this.suspended || !this.supervisor)
|
|
925
925
|
throw new Error("Process broker is unavailable");
|
|
926
926
|
const processCapacity = directoryCapacity(transport.directory);
|
|
@@ -939,6 +939,7 @@ export class BrowserProcessBroker {
|
|
|
939
939
|
transport.data = { filesystem: options.filesystem, pipeRegistry: registry.handle() };
|
|
940
940
|
(this.commandRoots ??= new Map).set(transport.pid, {
|
|
941
941
|
root: transport,
|
|
942
|
+
programs,
|
|
942
943
|
pending: new Set,
|
|
943
944
|
failures: new Map,
|
|
944
945
|
stopped: false
|
|
@@ -197,7 +197,7 @@ class Command {
|
|
|
197
197
|
memory: [],
|
|
198
198
|
tryOutput: (fd, bytes) => this.deliver(fd, bytes),
|
|
199
199
|
stop: () => this.input?.stop()
|
|
200
|
-
});
|
|
200
|
+
}, this.options.programs ?? "byte-backed");
|
|
201
201
|
adopted = true;
|
|
202
202
|
if (this.runtime.disposed)
|
|
203
203
|
return this.disposed();
|
|
@@ -348,6 +348,12 @@ class Command {
|
|
|
348
348
|
}
|
|
349
349
|
return bytes.length;
|
|
350
350
|
}
|
|
351
|
+
leaderExit(leader) {
|
|
352
|
+
const exit = readOutcome(leader.state.outcome);
|
|
353
|
+
if (exit && this.options.onLeaderExit === "stop")
|
|
354
|
+
this.stop();
|
|
355
|
+
return exit;
|
|
356
|
+
}
|
|
351
357
|
async join(owner, root, leader) {
|
|
352
358
|
const wake = workerProcessState(root).wake;
|
|
353
359
|
let exit = null;
|
|
@@ -358,7 +364,7 @@ class Command {
|
|
|
358
364
|
if (this.stopped.aborted || this.ioFailure)
|
|
359
365
|
this.stopInput();
|
|
360
366
|
if (leader && !exit)
|
|
361
|
-
exit =
|
|
367
|
+
exit = this.leaderExit(leader);
|
|
362
368
|
if (owner.checkCommandCleanup(root, !leader || exit !== null)) {
|
|
363
369
|
if (!leader || exit)
|
|
364
370
|
return exit;
|
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
|
}
|