123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Engine;
- use Hyperf\Engine\Contract\CoroutineInterface;
- use Hyperf\Engine\Exception\CoroutineDestroyedException;
- use Hyperf\Engine\Exception\RunningInNonCoroutineException;
- use Hyperf\Engine\Exception\RuntimeException;
- use Swoole\Coroutine as SwooleCo;
- class Coroutine implements CoroutineInterface
- {
- /**
- * @var callable
- */
- private $callable;
- /**
- * @var int
- */
- private $id;
- public function __construct(callable $callable)
- {
- $this->callable = $callable;
- }
- public static function create(callable $callable, ...$data)
- {
- $coroutine = new static($callable);
- $coroutine->execute(...$data);
- return $coroutine;
- }
- public function execute(...$data)
- {
- $this->id = SwooleCo::create($this->callable, ...$data);
- return $this;
- }
- public function getId()
- {
- if (is_null($this->id)) {
- throw new RuntimeException('Coroutine was not be executed.');
- }
- return $this->id;
- }
- public static function id()
- {
- return SwooleCo::getCid();
- }
- public static function pid(?int $id = null)
- {
- if ($id) {
- $cid = SwooleCo::getPcid($id);
- if ($cid === false) {
- throw new CoroutineDestroyedException(sprintf('Coroutine #%d has been destroyed.', $id));
- }
- } else {
- $cid = SwooleCo::getPcid();
- }
- if ($cid === false) {
- throw new RunningInNonCoroutineException('Non-Coroutine environment don\'t has parent coroutine id.');
- }
- return max(0, $cid);
- }
- public static function set(array $config)
- {
- SwooleCo::set($config);
- }
- /**
- * @return null|\ArrayObject
- */
- public static function getContextFor(?int $id = null)
- {
- if ($id === null) {
- return SwooleCo::getContext();
- }
- return SwooleCo::getContext($id);
- }
- public static function defer(callable $callable)
- {
- SwooleCo::defer($callable);
- }
- }
|