@@ -80,19 +80,22 @@ final class CoroutineGen implements CoroutineGenInterface
8080 */
8181 public static function runBlocking (mixed ...$ coroutines ): void
8282 {
83- foreach ($ coroutines as $ coroutine ) {
84- if (is_callable ($ coroutine )) $ coroutine = call_user_func ($ coroutine );
85-
86- if ($ coroutine instanceof CoroutineScope) {
87- self ::schedule ($ coroutine );
88- } else if ($ coroutine instanceof Generator) {
89- self ::schedule (new ChildCoroutine ($ coroutine ));
90- } else {
91- call_user_func (fn () => $ coroutine );
83+ new Async (function () use ($ coroutines ): void {
84+ foreach ($ coroutines as $ coroutine ) {
85+ if (is_callable ($ coroutine )) $ coroutine = call_user_func ($ coroutine );
86+
87+ if ($ coroutine instanceof CoroutineScope) {
88+ self ::schedule ($ coroutine );
89+ } else if ($ coroutine instanceof Generator) {
90+ self ::schedule (new ChildCoroutine ($ coroutine ));
91+ } else {
92+ call_user_func (fn () => $ coroutine );
93+ }
94+ FiberManager::wait ();
9295 }
93- }
9496
95- self ::run ();
97+ self ::run ();
98+ });
9699 }
97100
98101 /**
@@ -151,19 +154,26 @@ private static function schedule(ChildCoroutine|CoroutineScope $childCoroutine):
151154 */
152155 private static function run (): void
153156 {
154- while (self ::$ taskQueue ?->isEmpty() === false ) {
155- $ coroutine = self ::$ taskQueue ->dequeue ();
156-
157- if ($ coroutine instanceof ChildCoroutine) {
158- $ coroutine ->run ();
159- if (!$ coroutine ->isFinished ()) self ::schedule ($ coroutine );
160- }
161-
162- if ($ coroutine instanceof CoroutineScope) {
163- $ coroutine ->run ();
164- if (!$ coroutine ->isFinished ()) self ::schedule ($ coroutine );
157+ new Async (function (): void {
158+ try {
159+ while (self ::$ taskQueue ?->isEmpty() === false ) {
160+ $ coroutine = self ::$ taskQueue ->dequeue ();
161+
162+ if ($ coroutine instanceof ChildCoroutine) {
163+ $ coroutine ->run ();
164+ if (!$ coroutine ->isFinished ()) self ::schedule ($ coroutine );
165+ }
166+
167+ if ($ coroutine instanceof CoroutineScope) {
168+ Async::await ($ coroutine ->run ());
169+ if (!$ coroutine ->isFinished ()) self ::schedule ($ coroutine );
170+ }
171+ FiberManager::wait ();
172+ }
173+ } catch (Throwable $ e ) {
174+ echo $ e ->getMessage ();
165175 }
166- }
176+ });
167177 }
168178
169179}
0 commit comments