You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.9 KiB
141 lines
3.9 KiB
12 years ago
|
<?php
|
||
12 years ago
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
12 years ago
|
|
||
|
namespace yii\mutex;
|
||
|
|
||
|
use Yii;
|
||
|
use yii\base\Component;
|
||
|
|
||
12 years ago
|
/**
|
||
|
* @author resurtm <resurtm@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
abstract class Mutex extends Component
|
||
|
{
|
||
12 years ago
|
/**
|
||
|
* @var boolean whether all locks acquired in this process (i.e. local locks) must be released automagically
|
||
|
* before finishing script execution. Defaults to true. Setting this property to true
|
||
|
*/
|
||
12 years ago
|
public $autoRelease = true;
|
||
12 years ago
|
/**
|
||
|
* @var string[] names of the locks acquired in the current PHP process.
|
||
|
*/
|
||
12 years ago
|
private $_locks = array();
|
||
|
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Initializes the mutex component.
|
||
|
*/
|
||
12 years ago
|
public function init()
|
||
|
{
|
||
|
if ($this->autoRelease) {
|
||
|
register_shutdown_function(array($this, 'shutdownFunction'));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Never call this method directly under any circumstances. This method is intended for internal use only.
|
||
12 years ago
|
*/
|
||
|
public function shutdownFunction()
|
||
|
{
|
||
|
foreach ($this->_locks as $lock) {
|
||
|
$this->release($lock);
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* @param string $name of the lock to be acquired. Must be unique.
|
||
|
* @param integer $timeout to wait for lock to be released. Defaults to zero meaning that method will return
|
||
|
* false immediately in case lock was already acquired.
|
||
|
* @return boolean lock acquiring result.
|
||
|
*/
|
||
12 years ago
|
public function acquireLock($name, $timeout = 0)
|
||
|
{
|
||
|
if ($this->acquire($name, $timeout)) {
|
||
|
$this->_locks[] = $name;
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Release acquired lock.
|
||
|
* @param string $name of the lock to be released. This lock must be already created.
|
||
|
* @return boolean lock release result.
|
||
|
*/
|
||
12 years ago
|
public function releaseLock($name)
|
||
|
{
|
||
|
if ($this->release($name)) {
|
||
|
unset($this->_locks[array_search($name, $this->_locks)]);
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Checks whether named lock was already opened.
|
||
|
* @param string $name of the lock to be checked. This lock must be already created.
|
||
|
* @return boolean|null whether named lock was already opened. Returns `null` value in case concrete
|
||
|
* mutex implementation does not support this operation.
|
||
|
*/
|
||
12 years ago
|
public function getIsLockAcquired($name)
|
||
|
{
|
||
|
if (in_array($name, $this->_locks)) {
|
||
|
return true;
|
||
|
} else {
|
||
|
return $this->getIsAcquired($name);
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Checks whether given lock is local. In other words local lock means that it was opened in the current
|
||
|
* PHP process.
|
||
|
* @param string $name of the lock to be checked. This lock must be already created.
|
||
|
* @return boolean whether named lock was locally acquired.
|
||
|
*/
|
||
12 years ago
|
public function getIsLockLocal($name)
|
||
|
{
|
||
|
return in_array($name, $this->_locks);
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* This method should be extended by concrete mutex implementations. Acquires lock by given name.
|
||
|
* @param string $name of the lock to be acquired.
|
||
|
* @param integer $timeout to wait for lock to become released.
|
||
|
* @return boolean acquiring result.
|
||
|
*/
|
||
12 years ago
|
abstract protected function acquire($name, $timeout = 0);
|
||
|
|
||
12 years ago
|
/**
|
||
|
* This method should be extended by concrete mutex implementations. Releases lock by given name.
|
||
|
* @param string $name of the lock to be released.
|
||
|
* @return boolean release result.
|
||
|
*/
|
||
12 years ago
|
abstract protected function release($name);
|
||
|
|
||
12 years ago
|
/**
|
||
|
* This method may optionally be extended by concrete mutex implementations. Checks whether lock has been
|
||
|
* already acquired by given name.
|
||
|
* @param string $name of the lock to be released.
|
||
|
* @return null|boolean whether lock has been already acquired. Returns `null` in case this feature
|
||
|
* is not supported by concrete mutex implementation.
|
||
|
*/
|
||
12 years ago
|
protected function getIsAcquired($name)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* This method should be extended by concrete mutex implementations. Returns whether current mutex
|
||
|
* implementation can be used in a distributed environment.
|
||
|
* @return boolean whether current mutex implementation can be used in a distributed environment.
|
||
|
*/
|
||
12 years ago
|
abstract public function getIsDistributed();
|
||
|
}
|