core: writer-owned relocation
The worker no longer copies rows when an array cannot grow in place. It reserves a zeroed region, returns the staged descriptor with a commit flag, and queues every other request until the main thread (the only writer) has copied the old bytes and sent commit-rows. Writes that raced the old worker-side copy were lost; lights in the glTF viewer regularly ended up at the origin because of it. Amp-Thread-ID: https://ampcode.com/threads/T-01a0b2dd-db32-712d-99ce-12ce4a86d979 Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
+20
-1
@@ -2,10 +2,13 @@ import initWasm, { Core } from "./pkg/yawn_core.js";
|
||||
|
||||
let core;
|
||||
let profileTimer;
|
||||
// While a relocation is staged, every request except its commit waits here.
|
||||
let awaitingCommit = false;
|
||||
const queue = [];
|
||||
|
||||
const fail = code => { throw new Error(code); };
|
||||
|
||||
addEventListener("message", async ({ data: message }) => {
|
||||
async function handle(message) {
|
||||
try {
|
||||
let result;
|
||||
if (message?.type === "init") {
|
||||
@@ -21,15 +24,22 @@ addEventListener("message", async ({ data: message }) => {
|
||||
switch (message?.type) {
|
||||
case "create-rows":
|
||||
result = JSON.parse(core.create_rows(message.name, message.rows, message.stride, message.format));
|
||||
awaitingCommit = result.commit;
|
||||
break;
|
||||
case "create-rows-batch":
|
||||
result = JSON.parse(core.create_rows_batch(JSON.stringify(message.rows)));
|
||||
awaitingCommit = result.commit.length > 0;
|
||||
break;
|
||||
case "commit-rows":
|
||||
awaitingCommit = false;
|
||||
core.commit_rows(JSON.stringify(message.names));
|
||||
break;
|
||||
case "delete-rows":
|
||||
core.delete_rows(message.name);
|
||||
break;
|
||||
case "allocate-object":
|
||||
result = JSON.parse(core.allocate_object(message.name, message.count ?? 1));
|
||||
awaitingCommit = result.commit;
|
||||
break;
|
||||
case "delete-object":
|
||||
core.delete_object(message.name, message.id);
|
||||
@@ -74,4 +84,13 @@ addEventListener("message", async ({ data: message }) => {
|
||||
} catch (error) {
|
||||
postMessage({ request: message?.request, error: error?.message ?? "CORE_ERROR" });
|
||||
}
|
||||
}
|
||||
|
||||
addEventListener("message", async ({ data: message }) => {
|
||||
if (awaitingCommit && message?.type !== "commit-rows") {
|
||||
queue.push(message);
|
||||
return;
|
||||
}
|
||||
await handle(message);
|
||||
while (!awaitingCommit && queue.length) await handle(queue.shift());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user