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.
		
		
		
		
			
				
					96 lines
				
				2.6 KiB
			
		
		
			
		
	
	
					96 lines
				
				2.6 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
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace yii\mutex;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use Yii;
							 | 
						||
| 
								 | 
							
								use yii\base\Component;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @author resurtm <resurtm@gmail.com>
							 | 
						||
| 
								 | 
							
								 * @since 2.0
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								abstract class Mutex extends Component
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @var boolean whether all locks acquired in this process (i.e. local locks) must be released automagically
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * before finishing script execution. Defaults to true. Setting this property to true means that all locks
							 | 
						||
| 
								 | 
							
									 * acquire in this process must be released in any case (regardless any kind of errors or exceptions).
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public $autoRelease = true;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @var string[] names of the locks acquired in the current PHP process.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									private $_locks = [];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Initializes the mutex component.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function init()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										if ($this->autoRelease) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$locks = &$this->_locks;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											register_shutdown_function(function () use (&$locks) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												foreach ($locks as $lock) {
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
													$this->release($lock);
							 | 
						||
| 
								 
											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. 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.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function acquire($name, $timeout = 0)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($this->acquireLock($name, $timeout)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$this->_locks[] = $name;
							 | 
						||
| 
								 | 
							
											return true;
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											return false;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * Release acquired lock. This method will return false in case named lock was not found.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param string $name of the lock to be released. This lock must be already created.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return boolean lock release result: false in case named lock was not found..
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function release($name)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($this->releaseLock($name)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$index = array_search($name, $this->_locks);
							 | 
						||
| 
								 | 
							
											if ($index !== false) {
							 | 
						||
| 
								 | 
							
												unset($this->_locks[$index]);
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											return true;
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											return false;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 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.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									abstract protected function acquireLock($name, $timeout = 0);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 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.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									abstract protected function releaseLock($name);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 |