Mutex.php 2.56 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 8 9 10 11 12

namespace yii\mutex;

use Yii;
use yii\base\Component;

13 14 15 16
/**
 * @author resurtm <resurtm@gmail.com>
 * @since 2.0
 */
resurtm committed
17 18
abstract class Mutex extends Component
{
19 20
	/**
	 * @var boolean whether all locks acquired in this process (i.e. local locks) must be released automagically
resurtm committed
21 22
	 * 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).
23
	 */
resurtm committed
24
	public $autoRelease = true;
25 26 27
	/**
	 * @var string[] names of the locks acquired in the current PHP process.
	 */
Alexander Makarov committed
28
	private $_locks = [];
resurtm committed
29 30


31 32 33
	/**
	 * Initializes the mutex component.
	 */
resurtm committed
34 35 36
	public function init()
	{
		if ($this->autoRelease) {
resurtm committed
37
			$locks = &$this->_locks;
resurtm committed
38
			register_shutdown_function(function () use (&$locks) {
resurtm committed
39
				foreach ($locks as $lock) {
40
					$this->release($lock);
41
				}
resurtm committed
42
			});
resurtm committed
43 44 45
		}
	}

46
	/**
47
	 * Acquires lock by given name.
48 49 50 51 52
	 * @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.
	 */
resurtm committed
53
	public function acquire($name, $timeout = 0)
resurtm committed
54
	{
resurtm committed
55
		if ($this->acquireLock($name, $timeout)) {
resurtm committed
56 57 58 59 60 61 62
			$this->_locks[] = $name;
			return true;
		} else {
			return false;
		}
	}

63
	/**
resurtm committed
64
	 * Release acquired lock. This method will return false in case named lock was not found.
65
	 * @param string $name of the lock to be released. This lock must be already created.
resurtm committed
66
	 * @return boolean lock release result: false in case named lock was not found..
67
	 */
resurtm committed
68
	public function release($name)
resurtm committed
69
	{
resurtm committed
70
		if ($this->releaseLock($name)) {
resurtm committed
71 72 73 74
			$index = array_search($name, $this->_locks);
			if ($index !== false) {
				unset($this->_locks[$index]);
			}
resurtm committed
75 76 77 78 79 80
			return true;
		} else {
			return false;
		}
	}

81 82 83 84 85 86
	/**
	 * 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.
	 */
resurtm committed
87
	abstract protected function acquireLock($name, $timeout = 0);
resurtm committed
88

89 90 91 92 93
	/**
	 * 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.
	 */
resurtm committed
94
	abstract protected function releaseLock($name);
resurtm committed
95
}