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.
87 lines
2.2 KiB
87 lines
2.2 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
|
|
||
12 years ago
|
namespace yii\mutex;
|
||
12 years ago
|
|
||
|
use Yii;
|
||
|
use yii\base\InvalidConfigException;
|
||
|
|
||
12 years ago
|
/**
|
||
|
* @author resurtm <resurtm@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class FileMutex extends Mutex
|
||
12 years ago
|
{
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var string the directory to store mutex files. You may use path alias here.
|
||
|
* If not set, it will use the "mutex" subdirectory under the application runtime path.
|
||
|
*/
|
||
|
public $mutexPath = '@runtime/mutex';
|
||
|
/**
|
||
12 years ago
|
* @var resource[] stores all opened lock files. Keys are lock names and values are file handles.
|
||
|
*/
|
||
12 years ago
|
private $_files = array();
|
||
|
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like
|
||
|
* operating systems.
|
||
|
* @throws InvalidConfigException
|
||
|
*/
|
||
12 years ago
|
public function init()
|
||
|
{
|
||
12 years ago
|
if (stripos(php_uname('s'), 'win') === 0) {
|
||
12 years ago
|
throw new InvalidConfigException('FileMutex does not have MS Windows operating system support.');
|
||
|
}
|
||
|
$this->mutexPath = Yii::getAlias($this->mutexPath);
|
||
|
if (!is_dir($this->mutexPath)) {
|
||
|
mkdir($this->mutexPath, 0777, true);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
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
|
protected function acquireLock($name, $timeout = 0)
|
||
12 years ago
|
{
|
||
12 years ago
|
$file = fopen($this->mutexPath . '/' . md5($name) . '.lock', 'w+');
|
||
12 years ago
|
if ($file === false) {
|
||
|
return false;
|
||
|
}
|
||
|
$waitTime = 0;
|
||
|
while (!flock($file, LOCK_EX | LOCK_NB)) {
|
||
|
$waitTime++;
|
||
|
if ($waitTime > $timeout) {
|
||
|
fclose($file);
|
||
|
return false;
|
||
|
}
|
||
|
sleep(1);
|
||
|
}
|
||
|
$this->_files[$name] = $file;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
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
|
protected function releaseLock($name)
|
||
12 years ago
|
{
|
||
|
if (!isset($this->_files[$name]) || !flock($this->_files[$name], LOCK_UN)) {
|
||
|
return false;
|
||
|
} else {
|
||
|
fclose($this->_files[$name]);
|
||
|
unset($this->_files[$name]);
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|