Skip to content

Commit 99bbf10

Browse files
refactor(bindgen): exclusive locking logic
1 parent 9724188 commit 99bbf10

File tree

3 files changed

+12
-37
lines changed

3 files changed

+12
-37
lines changed

crates/js-component-bindgen/src/intrinsics/component.rs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl ComponentIntrinsic {
159159
#componentIdx;
160160
#callingAsyncImport = false;
161161
#syncImportWait = Promise.withResolvers();
162-
#lock = null;
162+
#locked = false;
163163
#parkedTasks = new Map();
164164
#suspendedTasksByTaskID = new Map();
165165
#suspendedTaskIDs = [];
@@ -244,49 +244,21 @@ impl ComponentIntrinsic {
244244
task.awaitableResume();
245245
}}
246246
247-
async exclusiveLock() {{ // TODO: use atomics
248-
if (this.#lock === null) {{
249-
this.#lock = {{ ticket: 0n }};
250-
}}
251-
252-
// Take a ticket for the next valid usage
253-
const ticket = ++this.#lock.ticket;
254-
255-
{debug_log_fn}('[{class_name}#exclusiveLock()] locking', {{
256-
currentTicket: ticket - 1n,
257-
ticket
258-
}});
259-
260-
// If there is an active promise, then wait for it
261-
let finishedTicket;
262-
while (this.#lock.promise) {{
263-
finishedTicket = await this.#lock.promise;
264-
if (finishedTicket === ticket - 1n) {{ break; }}
265-
}}
266-
267-
const {{ promise, resolve }} = Promise.withResolvers();
268-
this.#lock = {{
269-
ticket,
270-
promise,
271-
resolve,
272-
}};
273-
274-
return this.#lock.promise;
247+
// TODO: we might want to check for pre-locked status here
248+
exclusiveLock() {{
249+
this.#locked = true;
275250
}}
276251
277252
exclusiveRelease() {{
278253
{debug_log_fn}('[{class_name}#exclusiveRelease()] releasing', {{
279-
currentTicket: this.#lock === null ? 'none' : this.#lock.ticket,
254+
locked: this.#locked,
280255
}});
281256
282-
if (this.#lock === null) {{ return; }}
283-
284-
const existingLock = this.#lock;
285-
this.#lock = null;
286-
existingLock.resolve(existingLock.ticket);
257+
if (!this.#locked) {{ throw new Error('not locked'); }}
258+
this.#locked = false
287259
}}
288260
289-
isExclusivelyLocked() {{ return this.#lock !== null; }}
261+
isExclusivelyLocked() {{ return this.#locked === true; }}
290262
291263
#getSuspendedTaskMeta(taskID) {{
292264
return this.#suspendedTasksByTaskID.get(taskID);

crates/js-component-bindgen/src/intrinsics/p3/host.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ impl HostIntrinsic {
230230
Intrinsic::AsyncTask(AsyncTaskIntrinsic::DriverLoop).name();
231231
let subtask_class =
232232
Intrinsic::AsyncTask(AsyncTaskIntrinsic::AsyncSubtaskClass).name();
233+
234+
// TODO: lower here for non-zero param count
235+
// https://github.com/bytecodealliance/wasmtime/blob/69ef9afc11a2846248c9e94affca0223dbd033fc/crates/wasmtime/src/runtime/component/concurrent.rs#L1775
233236
output.push_str(&format!(r#"
234237
function {async_start_call_fn}(args, callee, paramCount, resultCount, flags) {{
235238
const {{ getCallbackFn, callbackIdx, getPostReturnFn, postReturnIdx }} = args;

packages/jco/test/p3/ported/wasmtime/component-async/post-return.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ suite('post-return scenario', () => {
4242
// wasmtime/crates/misc/component-async-tests/tests/scenario/post_return.rs
4343
//
4444
suite('post-return async sleep scenario', () => {
45-
test('caller & callee', async () => {
45+
test.skip('caller & callee', async () => {
4646
const callerPath = join(
4747
COMPONENT_FIXTURES_DIR,
4848
"p3/general/async-sleep-post-return-caller.wasm"

0 commit comments

Comments
 (0)