55 lines
691 B
PHP
55 lines
691 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 = true;
|
|
|
|
private ?Channel $channel = null;
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function yield(): void
|
|
{
|
|
if (Coroutine::getCid() > 0) {
|
|
$this->channel = new Channel(1);
|
|
$this->channel->pop();
|
|
} else {
|
|
while ($this->wait) {
|
|
usleep(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function wait(): void
|
|
{
|
|
$this->wait = true;
|
|
$this->channel = null;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function done(): void
|
|
{
|
|
$this->wait = false;
|
|
$this->channel?->push(true);
|
|
}
|
|
|
|
}
|