@@ -554,7 +554,6 @@ class Task:
554554 state: State
555555 num_borrows: int
556556 implicit_thread: Optional[Thread]
557- threads: list[Thread]
558557
559558 def __init__ (self , ft , opts , inst , on_start , on_resolve ):
560559 self .ft = ft
@@ -565,7 +564,6 @@ class Task:
565564 self .state = Task.State.INITIAL
566565 self .num_borrows = 0
567566 self .implicit_thread = None
568- self .threads = []
569567```
570568
571569The ` Task.needs_exclusive ` predicate returns whether this task's implicit thread
@@ -627,14 +625,8 @@ exports.
627625 if self .needs_exclusive():
628626 assert (self .inst.exclusive_thread is None )
629627 self .inst.exclusive_thread = self .implicit_thread
630- self .register_thread (self .implicit_thread)
628+ self .implicit_thread.index = self .inst.threads.add (self .implicit_thread)
631629 return True
632-
633- def register_thread (self , thread ):
634- assert (thread not in self .threads and thread.task is self )
635- self .threads.append(thread)
636- assert (thread.index is None )
637- thread.index = self .inst.threads.add(thread)
638630```
639631Since the order in which suspended threads are resumed is nondeterministic (see
640632` Store.tick ` below), once ` Task.enter_implicit_thread ` suspends the task's
@@ -649,32 +641,26 @@ above definition ensures the following properties:
649641 thundering herd of pending tasks).
650642
651643Once a task's implicit thread has cleared the backpressure gate, it is added to
652- the lists of threads running inside the current task and component instance by
653- ` Task.register_thread() ` (which is also called by ` thread.new-indirect ` , below).
644+ the component instance's ` threads ` table (just like the explicit threads created
645+ by ` thread.new-indirect ` , below).
654646
655647Symmetrically, the ` Task.exit_implicit_thread ` method is called before a task's
656- implicit thread returns to reverse the effects of ` Task.enter_implicit_thread ` .
657- In particular, if the ` exclusive_thread ` lock was acquired, it is released.
658- ` Task.unregister_thread ` (which is also called by ` thread.new-indirect ` , below)
659- traps if the task's last thread is unregistered and the task has not yet
660- returned a value to its caller.
648+ implicit thread returns to reverse the effects of ` Task.enter_implicit_thread ` ,
649+ removing the thread from the ` threads ` table and, if the ` exclusive_thread ` lock
650+ was acquired, releasing it.
661651``` python
662652 def exit_implicit_thread (self ):
663653 assert (current_thread() is self .implicit_thread)
664- self .unregister_thread (self .implicit_thread)
654+ self .inst.threads.remove (self .implicit_thread.index )
665655 if self .ft.async_ and self .needs_exclusive():
666656 assert (self .inst.exclusive_thread is self .implicit_thread)
667657 self .inst.exclusive_thread = None
668-
669- def unregister_thread (self , thread ):
670- assert (thread in self .threads and thread.task is self )
671- self .threads.remove(thread)
672- if len (self .threads) == 0 :
673- trap_if(self .state != Task.State.RESOLVED )
674- assert (self .num_borrows == 0 )
675- assert (thread.index is not None )
676- self .inst.threads.remove(thread.index)
677658```
659+ Note that there is no trap when a task's implicit thread (or any other thread)
660+ exits before the task has resolved (by calling ` task.return ` or ` task.cancel ` ),
661+ even if it is the task's last thread. In that case, the task will never
662+ resolve, which, from the caller's perspective, is indistinguishable from the
663+ case where the task's threads are all blocked forever.
678664
679665The ` Task.request_cancellation ` method implements the ` OnCancel ` callback that
680666is called by the ` subtask.cancel ` built-in. If a task's implicit thread is
@@ -3465,7 +3451,8 @@ In both of the `async` cases below (with or without `callback`), the
34653451` task.return ` built-in must be called, providing the return value as core wasm
34663452* parameters* to the ` task.return ` built-in (rather than as core function
34673453results as in the synchronous case). If ` task.return ` is * not* called by the
3468- time the ` Task ` 's last ` Thread ` exits, there is a trap (in ` Task.unregister_thread ` ).
3454+ time the ` Task ` 's last ` Thread ` exits, the task never resolves (as described
3455+ in ` Task.exit_implicit_thread ` above).
34693456
34703457In the ` async ` non-` callback ` ("stackful async") case, there is a single call
34713458to the core wasm callee which must return empty core results. Waiting for async
@@ -4621,10 +4608,10 @@ def canon_thread_new_indirect(ft, ftbl: Table[CoreFuncRef], fi, c):
46214608 return [- 1 ]
46224609 def thread_func ():
46234610 [] = call_and_trap_on_throw(f.callee, [c])
4624- task.unregister_thread (new_thread)
4611+ task.inst.threads.remove (new_thread.index )
46254612 new_thread = Thread(task, thread_func)
46264613 assert (new_thread.suspended())
4627- task.register_thread (new_thread)
4614+ new_thread.index = task.inst.threads.add (new_thread)
46284615 return [new_thread.index]
46294616```
46304617The newly-created thread starts out in a "suspended" state and so, to
0 commit comments