FileMutex.php 2.76 KB
Newer Older
resurtm committed
1
<?php
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
resurtm committed
7

resurtm committed
8
namespace yii\mutex;
resurtm committed
9 10 11

use Yii;
use yii\base\InvalidConfigException;
12
use yii\helpers\FileHelper;
resurtm committed
13

14 15 16 17
/**
 * @author resurtm <resurtm@gmail.com>
 * @since 2.0
 */
resurtm committed
18
class FileMutex extends Mutex
resurtm committed
19
{
resurtm committed
20 21
	/**
	 * @var string the directory to store mutex files. You may use path alias here.
22
	 * Defaults to the "mutex" subdirectory under the application runtime path.
resurtm committed
23 24
	 */
	public $mutexPath = '@runtime/mutex';
25 26 27 28 29 30 31 32 33 34 35 36 37
	/**
	 * @var integer the permission to be set for newly created mutex files.
	 * This value will be used by PHP chmod() function. No umask will be applied.
	 * If not set, the permission will be determined by the current environment.
	 */
	public $fileMode;
	/**
	 * @var integer the permission to be set for newly created directories.
	 * This value will be used by PHP chmod() function. No umask will be applied.
	 * Defaults to 0775, meaning the directory is read-writable by owner and group,
	 * but read-only for other users.
	 */
	public $dirMode = 0775;
38 39 40
	/**
	 * @var resource[] stores all opened lock files. Keys are lock names and values are file handles.
	 */
Alexander Makarov committed
41
	private $_files = [];
resurtm committed
42 43


44 45 46 47 48
	/**
	 * Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like
	 * operating systems.
	 * @throws InvalidConfigException
	 */
resurtm committed
49 50
	public function init()
	{
resurtm committed
51
		if (stripos(php_uname('s'), 'win') === 0) {
resurtm committed
52 53 54 55
			throw new InvalidConfigException('FileMutex does not have MS Windows operating system support.');
		}
		$this->mutexPath = Yii::getAlias($this->mutexPath);
		if (!is_dir($this->mutexPath)) {
56
			FileHelper::createDirectory($this->mutexPath, $this->dirMode, true);
resurtm committed
57
		}
resurtm committed
58 59
	}

60
	/**
61
	 * Acquires lock by given name.
62 63 64 65
	 * @param string $name of the lock to be acquired.
	 * @param integer $timeout to wait for lock to become released.
	 * @return boolean acquiring result.
	 */
resurtm committed
66
	protected function acquireLock($name, $timeout = 0)
resurtm committed
67
	{
68 69
		$fileName = $this->mutexPath . '/' . md5($name) . '.lock';
		$file = fopen($fileName, 'w+');
resurtm committed
70 71 72
		if ($file === false) {
			return false;
		}
73 74 75
		if ($this->fileMode !== null) {
			@chmod($fileName, $this->fileMode);
		}
resurtm committed
76 77 78 79 80 81 82 83 84 85 86 87 88
		$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;
	}

89
	/**
90
	 * Releases lock by given name.
91 92 93
	 * @param string $name of the lock to be released.
	 * @return boolean release result.
	 */
resurtm committed
94
	protected function releaseLock($name)
resurtm committed
95 96 97 98 99 100 101 102 103 104
	{
		if (!isset($this->_files[$name]) || !flock($this->_files[$name], LOCK_UN)) {
			return false;
		} else {
			fclose($this->_files[$name]);
			unset($this->_files[$name]);
			return true;
		}
	}
}