WaitConcurrent.php 958 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\Coroutine;
  12. /**
  13. * @method bool isFull()
  14. * @method bool isEmpty()
  15. */
  16. class WaitConcurrent extends Concurrent
  17. {
  18. protected WaitGroup $wg;
  19. public function __construct(protected int $limit)
  20. {
  21. parent::__construct($limit);
  22. $this->wg = new WaitGroup();
  23. }
  24. public function create(callable $callable): void
  25. {
  26. $this->wg->add();
  27. $callable = function () use ($callable) {
  28. try {
  29. $callable();
  30. } finally {
  31. $this->wg->done();
  32. }
  33. };
  34. parent::create($callable);
  35. }
  36. public function wait(float $timeout = -1): bool
  37. {
  38. return $this->wg->wait($timeout);
  39. }
  40. }