Skip to content

Commit 1af7387

Browse files
authored
Added Barrier (#39)
1 parent 6d19310 commit 1af7387

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
}
2222
},
2323
"replace": {
24-
"hyperf/engine": "~2.13.0"
24+
"hyperf/engine": "~2.14.0"
2525
},
2626
"require": {
2727
"php": ">=8.0",
28-
"hyperf/engine-contract": "~1.12.0",
28+
"hyperf/engine-contract": "~1.13.0",
2929
"swow/swow": "^1.0"
3030
},
3131
"require-dev": {
@@ -51,7 +51,7 @@
5151
},
5252
"extra": {
5353
"branch-alias": {
54-
"dev-master": "2.13-dev"
54+
"dev-master": "2.14-dev"
5555
},
5656
"hyperf": {
5757
"config": "Hyperf\\Engine\\ConfigProvider"

src/Barrier.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Engine;
14+
15+
use Hyperf\Engine\Contract\BarrierInterface;
16+
use Swow\Sync\WaitReference;
17+
18+
class Barrier implements BarrierInterface
19+
{
20+
public static function wait(object &$barrier, int $timeout = -1): void
21+
{
22+
WaitReference::wait($barrier, $timeout);
23+
}
24+
25+
public static function create(): object
26+
{
27+
return new WaitReference();
28+
}
29+
}

tests/Cases/BarrierTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Cases;
14+
15+
use Hyperf\Engine\Barrier;
16+
use Hyperf\Engine\Coroutine;
17+
18+
/**
19+
* @internal
20+
* @coversNothing
21+
*/
22+
class BarrierTest extends AbstractTestCase
23+
{
24+
public function testBarrier()
25+
{
26+
$barrier = Barrier::create();
27+
$N = 10;
28+
$count = 0;
29+
for ($i = 0; $i < $N; ++$i) {
30+
Coroutine::create(function () use (&$count, $barrier) {
31+
isset($barrier);
32+
usleep(2000);
33+
++$count;
34+
});
35+
}
36+
Barrier::wait($barrier);
37+
$this->assertSame($N, $count);
38+
}
39+
}

0 commit comments

Comments
 (0)