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.
		
		
		
		
			
				
					105 lines
				
				2.8 KiB
			
		
		
			
		
	
	
					105 lines
				
				2.8 KiB
			| 
								 
											13 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								namespace yii\mutex;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								use Yii;
							 | 
						||
| 
								 | 
							
								use yii\base\InvalidConfigException;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								use yii\helpers\FileHelper;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @author resurtm <resurtm@gmail.com>
							 | 
						||
| 
								 | 
							
								 * @since 2.0
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								class FileMutex extends Mutex
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var string the directory to store mutex files. You may use path alias here.
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * Defaults to the "mutex" subdirectory under the application runtime path.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public $mutexPath = '@runtime/mutex';
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @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;
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var resource[] stores all opened lock files. Keys are lock names and values are file handles.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									private $_files = [];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like
							 | 
						||
| 
								 | 
							
									 * operating systems.
							 | 
						||
| 
								 | 
							
									 * @throws InvalidConfigException
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function init()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (stripos(php_uname('s'), 'win') === 0) {
							 | 
						||
| 
								 
											13 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)) {
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											FileHelper::createDirectory($this->mutexPath, $this->dirMode, true);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * Acquires lock by given name.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param string $name of the lock to be acquired.
							 | 
						||
| 
								 | 
							
									 * @param integer $timeout to wait for lock to become released.
							 | 
						||
| 
								 | 
							
									 * @return boolean acquiring result.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									protected function acquireLock($name, $timeout = 0)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$fileName = $this->mutexPath . '/' . md5($name) . '.lock';
							 | 
						||
| 
								 | 
							
										$file = fopen($fileName, 'w+');
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($file === false) {
							 | 
						||
| 
								 | 
							
											return false;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										if ($this->fileMode !== null) {
							 | 
						||
| 
								 | 
							
											@chmod($fileName, $this->fileMode);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$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;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * Releases lock by given name.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param string $name of the lock to be released.
							 | 
						||
| 
								 | 
							
									 * @return boolean release result.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									protected function releaseLock($name)
							 | 
						||
| 
								 
											13 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;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |