1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 18:17:52 +01:00
parallel/src/Threading/Semaphore.php

49 lines
1.2 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Concurrent\Threading;
use Icicle\Concurrent\Sync\SemaphoreInterface;
/**
* An asynchronous semaphore based on pthreads' synchronization methods.
*
* This is an implementation of a thread-safe semaphore that has non-blocking
* acquire methods. There is a small tradeoff for asynchronous semaphores; you
* may not acquire a lock immediately when one is available and there may be a
* small delay. However, the small delay will not block the thread.
*/
class Semaphore implements SemaphoreInterface
{
/**
2015-08-31 00:52:00 +02:00
* @var Internal\Semaphore
*/
private $semaphore;
/**
2015-08-31 00:52:00 +02:00
* Creates a new semaphore with a given number of locks.
*
2015-08-31 20:49:26 +02:00
* @param int $locks The maximum number of locks that can be acquired from the semaphore.
*/
2015-08-31 20:49:26 +02:00
public function __construct($locks)
{
2015-08-31 00:52:00 +02:00
$this->semaphore = new Internal\Semaphore($locks);
}
/**
* Gets the number of currently available locks.
*
* @return int The number of available locks.
*/
public function count()
{
return $this->semaphore->count();
}
/**
* {@inheritdoc}
*/
public function acquire()
{
return $this->semaphore->acquire();
}
2015-08-31 00:52:00 +02:00
}