for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* This file is part of ninja-mutex.
*
* (C) Kamil Dziedzic <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NinjaMutex;
use NinjaMutex\Lock\LockInterface;
* Mutex
* @author Kamil Dziedzic <[email protected]>
class Mutex
{
* Lock implementor
* @var LockInterface
protected $lockImplementor;
* Name of lock
* @var string
protected $name;
* Lock counter to protect against recursive deadlock
* @var integer
protected $counter = 0;
* @param string $name
* @param LockInterface $lockImplementor
public function __construct($name, LockInterface $lockImplementor)
$this->name = $name;
$this->lockImplementor = $lockImplementor;
}
* @param int|null $timeout
* @return bool
public function acquireLock($timeout = null)
if ($this->counter > 0 ||
$this->lockImplementor->acquireLock($this->name, $timeout)) {
$this->counter++;
return true;
return false;
public function releaseLock()
if ($this->counter > 0) {
$this->counter--;
$this->lockImplementor->releaseLock($this->name)) {
* Check if Mutex is acquired
public function isAcquired()
return $this->counter > 0;
public function isLocked()
return $this->lockImplementor->isLocked($this->name);