Browse Source

Fixes:

1. Proper auto release shutdown callback.
2. Proper Mutex::releaseLock() method.
tags/2.0.0-beta
resurtm 12 years ago
parent
commit
b98a1236bb
  1. 31
      extensions/mutex/yii/mutex/Mutex.php

31
extensions/mutex/yii/mutex/Mutex.php

@ -33,26 +33,18 @@ abstract class Mutex extends Component
public function init() public function init()
{ {
if ($this->autoRelease) { if ($this->autoRelease) {
$referenceHolder = new stdClass(); $references = new stdClass();
$referenceHolder->mutex = &$this; $references->mutex = $this;
$referenceHolder->locks = &$this->_locks; $references->locks = &$this->_locks;
register_shutdown_function(function ($ref) { register_shutdown_function(function ($refs) {
foreach ($ref->locks as $lock) { foreach ($refs->locks as $lock) {
$ref->mutex->release($lock); $refs->mutex->release($lock);
} }
}, $referenceHolder); }, $references);
} }
} }
/** /**
* Never call this method directly under any circumstances. This method is intended for internal use only.
*/
public function shutdownFunction()
{
}
/**
* @param string $name of the lock to be acquired. Must be unique. * @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 * @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. * false immediately in case lock was already acquired.
@ -69,14 +61,17 @@ abstract class Mutex extends Component
} }
/** /**
* Release acquired lock. * Release acquired lock. This method will return false in case named lock was not found.
* @param string $name of the lock to be released. This lock must be already created. * @param string $name of the lock to be released. This lock must be already created.
* @return boolean lock release result. * @return boolean lock release result: false in case named lock was not found..
*/ */
public function releaseLock($name) public function releaseLock($name)
{ {
if ($this->release($name)) { if ($this->release($name)) {
unset($this->_locks[array_search($name, $this->_locks)]); $index = array_search($name, $this->_locks);
if ($index !== false) {
unset($this->_locks[$index]);
}
return true; return true;
} else { } else {
return false; return false;

Loading…
Cancel
Save