Skip to content

Commit

Permalink
Rename Latch to Barrier
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed May 7, 2020
1 parent fec4d32 commit 613047a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
14 changes: 7 additions & 7 deletions src/Latch.php → src/Barrier.php
Expand Up @@ -6,9 +6,9 @@
use Amp\Promise;

/**
* A latch is a synchronization primitive.
* A barrier is a synchronization primitive.
*
* The latch is initialized with a certain count, which can be increased and decreased until it reaches zero.
* The barrier is initialized with a certain count, which can be increased and decreased until it reaches zero.
*
* A count of one can be used to block multiple coroutines until a certain condition is met.
*
Expand All @@ -17,14 +17,14 @@
* **Example**
*
* ```php
* $latch = new Amp\Sync\Latch(2);
* $latch->arrive();
* $latch->arrive(); // promise returned from Latch::await() is now resolved
* $barrier = new Amp\Sync\Barrier(2);
* $barrier->arrive();
* $barrier->arrive(); // promise returned from Barrier::await() is now resolved
*
* yield $latch->await();
* yield $barrier->await();
* ```
*/
final class Latch
final class Barrier
{
/** @var int */
private $count;
Expand Down
16 changes: 8 additions & 8 deletions src/ConcurrentIterator/functions.php
Expand Up @@ -6,7 +6,7 @@
use Amp\Iterator;
use Amp\Producer;
use Amp\Promise;
use Amp\Sync\Latch;
use Amp\Sync\Barrier;
use Amp\Sync\Lock;
use Amp\Sync\Semaphore;
use function Amp\asyncCall;
Expand All @@ -26,8 +26,8 @@
function transform(Iterator $iterator, Semaphore $semaphore, callable $processor): Iterator
{
return new Producer(static function (callable $emit) use ($iterator, $semaphore, $processor) {
// one dummy item, because we can't start the latch with a count of zero
$latch = new Latch(1);
// one dummy item, because we can't start the barrier with a count of zero
$barrier = new Barrier(1);

/** @var \Throwable|null $error */
$error = null;
Expand All @@ -38,7 +38,7 @@ function transform(Iterator $iterator, Semaphore $semaphore, callable $processor
$processor = static function (Lock $lock, $currentElement) use (
$processor,
$emit,
$latch,
$barrier,
&$locks,
&$error,
&$gc
Expand All @@ -60,7 +60,7 @@ function transform(Iterator $iterator, Semaphore $semaphore, callable $processor
unset($locks[$lock->getId()]);

$lock->release();
$latch->arrive();
$barrier->arrive();
}
};

Expand All @@ -77,13 +77,13 @@ function transform(Iterator $iterator, Semaphore $semaphore, callable $processor
}

$locks[$lock->getId()] = true;
$latch->register();
$barrier->register();

asyncCall($processor, $lock, $iterator->getCurrent());
}

$latch->arrive(); // remove dummy item
yield $latch->await();
$barrier->arrive(); // remove dummy item
yield $barrier->await();

if ($error) {
throw $error;
Expand Down
70 changes: 35 additions & 35 deletions test/LatchTest.php → test/BarrierTest.php
Expand Up @@ -3,54 +3,54 @@
namespace Amp\Sync\Test;

use Amp\PHPUnit\AsyncTestCase;
use Amp\Sync\Latch;
use Amp\Sync\Barrier;

class LatchTest extends AsyncTestCase
class BarrierTest extends AsyncTestCase
{
/** @var Latch */
private $latch;
/** @var Barrier */
private $barrier;

public function testArriveUntilResolved(): void
{
$resolved = false;

$this->latch->await()->onResolve(static function () use (&$resolved) {
$this->barrier->await()->onResolve(static function () use (&$resolved) {
$resolved = true;
});

$this->assertFalse($resolved);

$this->latch->arrive();
$this->assertSame(1, $this->latch->getCount());
$this->barrier->arrive();
$this->assertSame(1, $this->barrier->getCount());

$this->assertFalse($resolved);

$this->latch->arrive();
$this->barrier->arrive();

$this->assertTrue($resolved);
$this->assertSame(0, $this->latch->getCount());
$this->assertSame(0, $this->barrier->getCount());
}

public function testArriveAfterResolved(): void
{
$this->latch->arrive();
$this->latch->arrive();
$this->barrier->arrive();
$this->barrier->arrive();

$this->expectException(\Error::class);
$this->latch->arrive();
$this->barrier->arrive();
}

public function testArriveWithCount(): void
{
$resolved = false;

$this->latch->await()->onResolve(static function () use (&$resolved) {
$this->barrier->await()->onResolve(static function () use (&$resolved) {
$resolved = true;
});

$this->assertFalse($resolved);

$this->latch->arrive(2);
$this->barrier->arrive(2);

$this->assertTrue($resolved);
}
Expand All @@ -59,43 +59,43 @@ public function testArriveWithInvalidCount(): void
{
$this->expectException(\Error::class);

$this->latch->arrive(0);
$this->barrier->arrive(0);
}

public function testArriveTooHighCount(): void
{
$this->expectException(\Error::class);

$this->latch->arrive(3);
$this->barrier->arrive(3);
}

public function testGetCurrentCount(): void
{
$this->latch->arrive();
$this->assertEquals(1, $this->latch->getCount());
$this->barrier->arrive();
$this->assertEquals(1, $this->barrier->getCount());
}

public function testInvalidSignalCountInConstructor(): void
{
$this->expectException(\Error::class);
new Latch(0);
new Barrier(0);
}

public function testRegisterCount(): void
{
$resolved = false;

$this->latch->await()->onResolve(static function () use (&$resolved) {
$this->barrier->await()->onResolve(static function () use (&$resolved) {
$resolved = true;
});

$this->latch->arrive();
$this->latch->register();
$this->latch->arrive();
$this->barrier->arrive();
$this->barrier->register();
$this->barrier->arrive();

$this->assertFalse($resolved);

$this->latch->arrive();
$this->barrier->arrive();

$this->assertTrue($resolved);
}
Expand All @@ -104,21 +104,21 @@ public function testRegisterCountWithCustomCount(): void
{
$resolved = false;

$this->latch->await()->onResolve(static function () use (&$resolved) {
$this->barrier->await()->onResolve(static function () use (&$resolved) {
$resolved = true;
});

$this->latch->arrive();
$this->latch->register(2);
$this->latch->arrive();
$this->barrier->arrive();
$this->barrier->register(2);
$this->barrier->arrive();

$this->assertFalse($resolved);

$this->latch->arrive();
$this->barrier->arrive();

$this->assertFalse($resolved);

$this->latch->arrive();
$this->barrier->arrive();

$this->assertTrue($resolved);
}
Expand All @@ -128,24 +128,24 @@ public function testRegisterCountWithInvalidCount(): void
$this->expectException(\Error::class);
$this->expectExceptionMessage('Count must be at least 1, got 0');

$this->latch->register(0);
$this->barrier->register(0);
}

public function testRegisterCountWithResolvedBarrier(): void
{
$this->latch->arrive();
$this->latch->arrive();
$this->barrier->arrive();
$this->barrier->arrive();

$this->expectException(\Error::class);
$this->expectExceptionMessage('Can\'t increase count, because the barrier already broke');

$this->latch->register(1);
$this->barrier->register(1);
}

protected function setUp(): void
{
parent::setUp();

$this->latch = new Latch(2);
$this->barrier = new Barrier(2);
}
}

0 comments on commit 613047a

Please sign in to comment.