2015-08-07 22:32:18 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Concurrent\Sync;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-08-10 05:30:11 +02:00
|
|
|
class ThreadedSemaphore implements SemaphoreInterface
|
2015-08-07 22:32:18 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-08-10 05:30:11 +02:00
|
|
|
* @var \Icicle\Concurrent\Sync\InternalThreadedSemaphore
|
2015-08-07 22:32:18 +02:00
|
|
|
*/
|
2015-08-10 05:30:11 +02:00
|
|
|
private $semaphore;
|
2015-08-07 22:32:18 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-10 05:30:11 +02:00
|
|
|
* @param int $maxLocks
|
2015-08-07 22:32:18 +02:00
|
|
|
*/
|
|
|
|
public function __construct($maxLocks)
|
|
|
|
{
|
2015-08-10 05:30:11 +02:00
|
|
|
$this->semaphore = new InternalThreadedSemaphore($maxLocks);
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of currently available locks.
|
|
|
|
*
|
|
|
|
* @return int The number of available locks.
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2015-08-10 05:30:11 +02:00
|
|
|
return $this->semaphore->count();
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function acquire()
|
|
|
|
{
|
2015-08-10 05:30:11 +02:00
|
|
|
return $this->semaphore->acquire();
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|
2015-08-10 05:30:11 +02:00
|
|
|
}
|