This commit is contained in:
2026-07-03 18:29:45 +08:00
parent 91be2eba20
commit 536e4c9bc5
+17 -8
View File
@@ -11,7 +11,7 @@ class Coordinator
const string WORKER_START = 'worker:start'; const string WORKER_START = 'worker:start';
private bool $wait = true; private bool $wait = false;
private ?Channel $channel = null; private ?Channel $channel = null;
@@ -22,12 +22,17 @@ class Coordinator
public function yield(): void public function yield(): void
{ {
if (Coroutine::getCid() > 0) { if (Coroutine::getCid() > 0) {
$this->channel = new Channel(1); if ($this->channel instanceof Channel) {
$this->channel->pop(); $this->channel->pop();
} else {
while ($this->wait) {
usleep(1000);
} }
return;
}
if ($this->wait === false) {
return;
}
while ($this->wait === true) {
usleep(1000);
} }
} }
@@ -37,8 +42,7 @@ class Coordinator
*/ */
public function wait(): void public function wait(): void
{ {
$this->wait = true; Coroutine::getCid() > 0 ? $this->channel = new Channel(1) : $this->wait = true;
$this->channel = null;
} }
@@ -47,8 +51,13 @@ class Coordinator
*/ */
public function done(): void public function done(): void
{ {
$this->wait = false; if (Coroutine::getCid() > 0) {
$this->channel?->push(true); $this->channel?->push(true);
$this->channel->close();
$this->channel = null;
} else {
$this->wait = false;
}
} }
} }