diff --git a/kiri-engine/Coordinator.php b/kiri-engine/Coordinator.php index c4544b9b..16353ceb 100644 --- a/kiri-engine/Coordinator.php +++ b/kiri-engine/Coordinator.php @@ -11,7 +11,7 @@ class Coordinator const string WORKER_START = 'worker:start'; - private bool $wait = true; + private bool $wait = false; private ?Channel $channel = null; @@ -22,12 +22,17 @@ class Coordinator public function yield(): void { if (Coroutine::getCid() > 0) { - $this->channel = new Channel(1); - $this->channel->pop(); - } else { - while ($this->wait) { - usleep(1000); + if ($this->channel instanceof Channel) { + $this->channel->pop(); } + return; + } + + if ($this->wait === false) { + return; + } + while ($this->wait === true) { + usleep(1000); } } @@ -37,8 +42,7 @@ class Coordinator */ public function wait(): void { - $this->wait = true; - $this->channel = null; + Coroutine::getCid() > 0 ? $this->channel = new Channel(1) : $this->wait = true; } @@ -47,8 +51,13 @@ class Coordinator */ public function done(): void { - $this->wait = false; - $this->channel?->push(true); + if (Coroutine::getCid() > 0) { + $this->channel?->push(true); + $this->channel->close(); + $this->channel = null; + } else { + $this->wait = false; + } } }