Files
kiri-core/kiri-engine/Coordinator.php
T

55 lines
691 B
PHP
Raw Normal View History

2022-06-17 11:59:19 +08:00
<?php
2023-04-16 01:45:34 +08:00
declare(strict_types=1);
2022-06-17 11:59:19 +08:00
namespace Kiri;
2026-06-12 23:57:25 +08:00
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
2022-06-17 11:59:19 +08:00
class Coordinator
{
2023-12-12 15:35:38 +08:00
const string WORKER_START = 'worker:start';
2022-06-17 11:59:19 +08:00
2026-06-12 23:57:25 +08:00
private bool $wait = true;
private ?Channel $channel = null;
2022-06-17 11:59:19 +08:00
/**
* @return void
*/
public function yield(): void
{
2026-06-12 23:57:25 +08:00
if (Coroutine::getCid() > 0) {
$this->channel = new Channel(1);
$this->channel->pop();
} else {
while ($this->wait) {
usleep(1000);
}
}
2022-06-17 11:59:19 +08:00
}
/**
* @return void
*/
2026-06-12 23:57:25 +08:00
public function wait(): void
2022-06-17 11:59:19 +08:00
{
2026-06-12 23:57:25 +08:00
$this->wait = true;
$this->channel = null;
2022-06-17 11:59:19 +08:00
}
/**
* @return void
*/
public function done(): void
{
2026-06-12 23:57:25 +08:00
$this->wait = false;
$this->channel?->push(true);
2022-06-17 11:59:19 +08:00
}
}