Skip to content

Commit

Permalink
A better fix for the Atomics.wait with costrol value
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Oct 11, 2023
1 parent 7891cd9 commit 5e0c12f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion esm/channel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ⚠️ AUTOMATICALLY GENERATED - DO NOT CHANGE
export const CHANNEL = '8db0a6b1-4d79-4ecd-8b7a-7ea5c1811764';
export const CHANNEL = '0d0ac1f5-3d55-4854-86ec-2e8653dd16d4';

export const MAIN = 'M' + CHANNEL;
export const THREAD = 'T' + CHANNEL;
24 changes: 16 additions & 8 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ const coincident = (self, {parse = JSON.parse, stringify = JSON.stringify, trans
const id = uid++;

// first contact: just ask for how big the buffer should be
let sb = new Int32Array(new SharedArrayBuffer(I32_BYTES));
// the value would be stored at index [1] while [0] is just control
let sb = new Int32Array(new SharedArrayBuffer(I32_BYTES * 2));

// if a transfer list has been passed, drop it from args
let transfer = [];
Expand All @@ -101,10 +102,10 @@ const coincident = (self, {parse = JSON.parse, stringify = JSON.stringify, trans
clearTimeout(deadlock);

// commit transaction using the returned / needed buffer length
const length = sb[0];
const length = sb[1];

// filter undefined results
if (length < 0) return;
if (!length) return;

// calculate the needed ui16 bytes length to store the result string
const bytes = UI16_BYTES * length;
Expand Down Expand Up @@ -134,6 +135,7 @@ const coincident = (self, {parse = JSON.parse, stringify = JSON.stringify, trans
self.addEventListener('message', async (event) => {
// grub the very same library CHANNEL; ignore otherwise
const details = event.data?.[CHANNEL];
let error;
if (isArray(details)) {
// if early enough, avoid leaking data to other listeners
event.stopImmediatePropagation();
Expand All @@ -146,27 +148,32 @@ const coincident = (self, {parse = JSON.parse, stringify = JSON.stringify, trans
try {
// await for result either sync or async and serialize it
const result = await actions.get(action)(...args);
if (result === void 0)
sb[0] = -1; // @see https://github.com/WebReflection/coincident/issues/26
else {
if (result !== void 0) {
const serialized = stringify(transform ? transform(result) : result);
// store the result for "the very next" event listener call
results.set(id, serialized);
// communicate the required SharedArrayBuffer length out of the
// resulting serialized string
sb[0] = serialized.length;
sb[1] = serialized.length;
}
}
catch (_) {
error = _;
}
finally {
seppuku = false;
}
}
// unknown action should be notified as missing on the main thread
else {
throw new Error(`Unsupported action: ${action}`);
error = new Error(`Unsupported action: ${action}`);
}
// unlock the wait lock later on
sb[0] = 1;
}
// no action means: get results out of the well known `id`
// wait lock automatically unlocked here as no `0` value would
// possibly ever land at index `0`
else {
const result = results.get(id);
results.delete(id);
Expand All @@ -176,6 +183,7 @@ const coincident = (self, {parse = JSON.parse, stringify = JSON.stringify, trans
}
// release te worker waiting either the length or the result
notify(sb, 0);
if (error) throw error;
}
});
}
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"rollup:uhtml": "rollup --config rollup/uhtml.config.js",
"rollup:window": "rollup --config rollup/window.config.js",
"test": "c8 node test/index.js",
"test:integration": "static-handler --cors --coop --coep --corp . 2>/dev/null & SH_PID=$!; EXIT_CODE=0; playwright test test/ || EXIT_CODE=$?; kill $SH_PID 2>/dev/null; exit $EXIT_CODE",
"test:integration": "static-handler --coi . 2>/dev/null & SH_PID=$!; EXIT_CODE=0; playwright test test/ || EXIT_CODE=$?; kill $SH_PID 2>/dev/null; exit $EXIT_CODE",
"test:server": "node test/server/main.cjs",
"ts": "tsc -p .",
"server": "npx static-handler --cors --coop --coep --corp .",
"server": "npx static-handler --coi .",
"size": "echo -e \"\\x1b[1mfile size\\x1b[0m\"; echo \"es $(cat es.js | brotli | wc -c)\"; echo \"structured $(cat structured.js | brotli | wc -c)\"; echo \"window $(cat window.js | brotli | wc -c)\"; echo \"server $(cat server.js | brotli | wc -c)\"; echo \"uhtml $(cat uhtml.js | brotli | wc -c)\";",
"coverage": "mkdir -p ./coverage; c8 report --reporter=text-lcov > ./coverage/lcov.info"
},
Expand All @@ -29,13 +29,13 @@
"author": "Andrea Giammarchi",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.38.1",
"@playwright/test": "^1.39.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"ascjs": "^6.0.2",
"c8": "^8.0.1",
"rollup": "^4.0.2",
"static-handler": "^0.4.2",
"static-handler": "^0.4.3",
"typescript": "^5.2.2",
"uhtml": "^3.2.2"
},
Expand Down
8 changes: 4 additions & 4 deletions test/issue-26/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ console.log('worker.js');
import coincident from '../../es.js';
const proxy = coincident(self);

(async () => {
for (let i = 0; i < 100000; i++)
proxy.func();
})();
for (let i = 0; i < 100000; i++)
proxy.func();

console.log('DONE');

0 comments on commit 5e0c12f

Please sign in to comment.