Skip to content

Commit 19c16ec

Browse files
tw2066huangdijia
andauthored
Add Hyperf\Coroutine\WaitConcurrent (#7263)
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
1 parent c353b3f commit 19c16ec

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/WaitConcurrent.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Coroutine;
14+
15+
/**
16+
* @method bool isFull()
17+
* @method bool isEmpty()
18+
*/
19+
class WaitConcurrent extends Concurrent
20+
{
21+
protected WaitGroup $wg;
22+
23+
public function __construct(protected int $limit)
24+
{
25+
parent::__construct($limit);
26+
$this->wg = new WaitGroup();
27+
}
28+
29+
public function create(callable $callable): void
30+
{
31+
$this->wg->add();
32+
33+
$callable = function () use ($callable) {
34+
try {
35+
$callable();
36+
} finally {
37+
$this->wg->done();
38+
}
39+
};
40+
41+
parent::create($callable);
42+
}
43+
44+
public function wait(float $timeout = -1): bool
45+
{
46+
return $this->wg->wait($timeout);
47+
}
48+
}

0 commit comments

Comments
 (0)