@@ -519,11 +519,12 @@ impl AsyncTaskIntrinsic {
519519 }}
520520 const tasks = {global_task_map}.get(componentIdx);
521521
522+ const callbackFn = getCallbackFn ? getCallbackFn() : null;
522523 const newTask = new {task_class}({{
523524 componentIdx,
524525 isAsync,
525526 entryFnName,
526- callbackFn: getCallbackFn ? getCallbackFn() : null ,
527+ callbackFn,
527528 callbackFnName,
528529 stringEncoding,
529530 getCalleeParamsFn,
@@ -648,10 +649,12 @@ impl AsyncTaskIntrinsic {
648649 #componentIdx;
649650 #state;
650651 #isAsync;
651- #onResolve = null;
652652 #entryFnName = null;
653653 #subtasks = [];
654+
655+ #onResolve = null;
654656 #completionPromise = null;
657+
655658 #memoryIdx = null;
656659
657660 #callbackFn = null;
@@ -665,6 +668,8 @@ impl AsyncTaskIntrinsic {
665668
666669 #parentSubtask = null;
667670
671+ #needsExclusiveLock = false;
672+
668673 cancelled = false;
669674 requested = false;
670675 alwaysTaskReturn = false;
@@ -676,7 +681,6 @@ impl AsyncTaskIntrinsic {
676681 awaitableResume = null;
677682 awaitableCancel = null;
678683
679-
680684 constructor(opts) {{
681685 this.#id = ++{task_class}._ID;
682686
@@ -709,6 +713,17 @@ impl AsyncTaskIntrinsic {
709713 if (opts.stringEncoding) {{ this.#stringEncoding = opts.stringEncoding; }}
710714
711715 if (opts.parentSubtask) {{ this.#parentSubtask = opts.parentSubtask; }}
716+
717+ this.#needsExclusiveLock = this.isSync() || !this.hasCallback();
718+ console.log('CREATED TASK', {{
719+ componentIdx: this.#componentIdx,
720+ entryFnName: this.#entryFnName,
721+ id: this.#id,
722+ sync: this.isSync(),
723+ hasCallback: this.hasCallback(),
724+ needsExclusiveLock: this.#needsExclusiveLock,
725+ }});
726+ console.log();
712727 }}
713728
714729 taskState() {{ return this.#state.slice(); }}
@@ -718,6 +733,11 @@ impl AsyncTaskIntrinsic {
718733 entryFnName() {{ return this.#entryFnName; }}
719734 completionPromise() {{ return this.#completionPromise; }}
720735
736+ isAsync() {{ return this.#isAsync; }}
737+ isSync() {{ return !this.isAsync(); }}
738+
739+ hasCallback() {{ return this.#callbackFn !== null; }}
740+
721741 setMemoryIdx(idx) {{ this.#memoryIdx = idx; }}
722742 getMemoryIdx(idx) {{ return this.#memoryIdx; }}
723743
@@ -857,52 +877,6 @@ impl AsyncTaskIntrinsic {
857877 throw new Error('{task_class}#pollForEvent() not implemented');
858878 }}
859879
860- async blockOn(opts) {{
861- const {{ awaitable, isCancellable, forCallback }} = opts;
862- {debug_log_fn}('[{task_class}#blockOn()] args', {{ taskID: this.#id, awaitable, isCancellable, forCallback }});
863-
864- if (awaitable.resolved() && !{global_async_determinism} && {coin_flip_fn}()) {{
865- return {task_class}.BlockResult.NOT_CANCELLED;
866- }}
867-
868- const cstate = {get_or_create_async_state_fn}(this.#componentIdx);
869- if (forCallback) {{ cstate.exclusiveRelease(); }}
870-
871- let cancelled = await this.onBlock(awaitable);
872- if (cancelled === {task_class}.BlockResult.CANCELLED && !isCancellable) {{
873- const secondCancel = await this.onBlock(awaitable);
874- if (secondCancel !== {task_class}.BlockResult.NOT_CANCELLED) {{
875- throw new Error('uncancellable task was canceled despite second onBlock()');
876- }}
877- }}
878-
879- if (forCallback) {{
880- const acquired = new {awaitable_class}(cstate.exclusiveLock());
881- cancelled = await this.onBlock(acquired);
882- if (cancelled === {task_class}.BlockResult.CANCELLED) {{
883- const secondCancel = await this.onBlock(acquired);
884- if (secondCancel !== {task_class}.BlockResult.NOT_CANCELLED) {{
885- throw new Error('uncancellable callback task was canceled despite second onBlock()');
886- }}
887- }}
888- }}
889-
890- if (cancelled === {task_class}.BlockResult.CANCELLED) {{
891- if (this.#state !== {task_class}.State.INITIAL) {{
892- throw new Error('cancelled task is not at initial state');
893- }}
894- if (isCancellable) {{
895- this.#state = {task_class}.State.CANCELLED;
896- return {task_class}.BlockResult.CANCELLED;
897- }} else {{
898- this.#state = {task_class}.State.CANCEL_PENDING;
899- return {task_class}.BlockResult.NOT_CANCELLED;
900- }}
901- }}
902-
903- return {task_class}.BlockResult.NOT_CANCELLED;
904- }}
905-
906880 async onBlock(awaitable) {{
907881 {debug_log_fn}('[{task_class}#onBlock()] args', {{ taskID: this.#id, awaitable }});
908882 if (!(awaitable instanceof {awaitable_class})) {{
@@ -1078,12 +1052,15 @@ impl AsyncTaskIntrinsic {
10781052 }}
10791053 state.inSyncExportCall = false;
10801054
1081- if (!state.isExclusivelyLocked()) {{
1082- throw new Error('task should have been exclusively locked at end of execution ');
1055+ if (this.needsExclusiveLock() && !state.isExclusivelyLocked()) {{
1056+ throw new Error('task [' + this.#id + '] exit: component [' + this.#componentIdx + '] should have been exclusively locked');
10831057 }}
1058+
10841059 state.exclusiveRelease();
10851060 }}
10861061
1062+ needsExclusiveLock(v) {{ return this.#needsExclusiveLock; }}
1063+
10871064 createSubtask(args) {{
10881065 {debug_log_fn}('[{task_class}#createSubtask()] args', args);
10891066 const {{ componentIdx, memoryIdx, childTask }} = args;
0 commit comments