Files
2026-07-03 18:29:45 +08:00

64 lines
898 B
PHP

<?php
declare(strict_types=1);
namespace Kiri;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
class Coordinator
{
const string WORKER_START = 'worker:start';
private bool $wait = false;
private ?Channel $channel = null;
/**
* @return void
*/
public function yield(): void
{
if (Coroutine::getCid() > 0) {
if ($this->channel instanceof Channel) {
$this->channel->pop();
}
return;
}
if ($this->wait === false) {
return;
}
while ($this->wait === true) {
usleep(1000);
}
}
/**
* @return void
*/
public function wait(): void
{
Coroutine::getCid() > 0 ? $this->channel = new Channel(1) : $this->wait = true;
}
/**
* @return void
*/
public function done(): void
{
if (Coroutine::getCid() > 0) {
$this->channel?->push(true);
$this->channel->close();
$this->channel = null;
} else {
$this->wait = false;
}
}
}