CoroutineInterface.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Engine\Contract;
  12. use ArrayObject;
  13. use Hyperf\Engine\Exception\CoroutineDestroyedException;
  14. use Hyperf\Engine\Exception\RunningInNonCoroutineException;
  15. interface CoroutineInterface
  16. {
  17. /**
  18. * @param callable $callable [required]
  19. */
  20. public function __construct(callable $callable);
  21. /**
  22. * @param mixed ...$data
  23. */
  24. public function execute(...$data): static;
  25. public function getId(): int;
  26. /**
  27. * @param callable $callable [required]
  28. * @param mixed ...$data
  29. * @return $this
  30. */
  31. public static function create(callable $callable, ...$data): static;
  32. /**
  33. * @return int returns coroutine id from current coroutine, -1 in non coroutine environment
  34. */
  35. public static function id(): int;
  36. /**
  37. * Returns the parent coroutine ID.
  38. * Returns 0 when running in the top level coroutine.
  39. * @throws RunningInNonCoroutineException when running in non-coroutine context
  40. * @throws CoroutineDestroyedException when the coroutine has been destroyed
  41. */
  42. public static function pid(?int $id = null): int;
  43. /**
  44. * Set config to coroutine.
  45. */
  46. public static function set(array $config): void;
  47. /**
  48. * @param null|int $id coroutine id
  49. */
  50. public static function getContextFor(?int $id = null): ?ArrayObject;
  51. /**
  52. * Execute callback when coroutine destruct.
  53. */
  54. public static function defer(callable $callable): void;
  55. /**
  56. * Yield the current coroutine.
  57. * @param mixed $data only Support Swow
  58. * @return bool|mixed Swow:mixed, Swoole:bool
  59. */
  60. public static function yield(mixed $data = null): mixed;
  61. /**
  62. * Resume the coroutine by coroutine Id.
  63. * @param mixed $data only Support Swow
  64. * @return bool|mixed Swow:mixed, Swoole:bool
  65. */
  66. public static function resumeById(int $id, mixed ...$data): mixed;
  67. /**
  68. * Get the coroutine stats.
  69. */
  70. public static function stats(): array;
  71. /**
  72. * Check if a coroutine exists or not.
  73. */
  74. public static function exists(int $id): bool;
  75. }