|
|
|
@ -11,9 +11,9 @@ use Yii;
|
|
|
|
|
use yii\base\Component; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Mutex component allows mutual execution of the concurrent processes, preventing "race conditions". |
|
|
|
|
* This is achieved by using "lock" mechanism. Each possibly concurrent thread cooperates by acquiring |
|
|
|
|
* the lock before accessing the corresponding data. |
|
|
|
|
* The Mutex component allows mutual execution of concurrent processes in order to prevent "race conditions". |
|
|
|
|
* This is achieved by using a "lock" mechanism. Each possibly concurrent thread cooperates by acquiring |
|
|
|
|
* a lock before accessing the corresponding data. |
|
|
|
|
* |
|
|
|
|
* Usage example: |
|
|
|
|
* |
|
|
|
@ -25,7 +25,7 @@ use yii\base\Component;
|
|
|
|
|
* } |
|
|
|
|
* ``` |
|
|
|
|
* |
|
|
|
|
* This class is a base one, which should be extended in order to implement actual lock mechanism. |
|
|
|
|
* This is a base class, which should be extended in order to implement the actual lock mechanism. |
|
|
|
|
* |
|
|
|
|
* @author resurtm <resurtm@gmail.com> |
|
|
|
|
* @since 2.0 |
|
|
|
@ -33,20 +33,20 @@ use yii\base\Component;
|
|
|
|
|
abstract class Mutex extends Component |
|
|
|
|
{ |
|
|
|
|
/** |
|
|
|
|
* @var boolean whether all locks acquired in this process (i.e. local locks) must be released automagically |
|
|
|
|
* @var boolean whether all locks acquired in this process (i.e. local locks) must be released automatically |
|
|
|
|
* 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). |
|
|
|
|
* acquired in this process must be released (regardless of errors or exceptions). |
|
|
|
|
*/ |
|
|
|
|
public $autoRelease = true; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @var string[] names of the locks acquired in the current PHP process. |
|
|
|
|
* @var string[] names of the locks acquired by the current PHP process. |
|
|
|
|
*/ |
|
|
|
|
private $_locks = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Initializes the mutex component. |
|
|
|
|
* Initializes the Mutex component. |
|
|
|
|
*/ |
|
|
|
|
public function init() |
|
|
|
|
{ |
|
|
|
@ -61,9 +61,9 @@ abstract class Mutex extends Component
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Acquires lock by given name. |
|
|
|
|
* Acquires a lock by name. |
|
|
|
|
* @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 time 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. |
|
|
|
|
*/ |
|
|
|
@ -79,8 +79,8 @@ abstract class Mutex extends Component
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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. |
|
|
|
|
* Releases acquired lock. This method will return false in case the lock was not found. |
|
|
|
|
* @param string $name of the lock to be released. This lock must already exist. |
|
|
|
|
* @return boolean lock release result: false in case named lock was not found.. |
|
|
|
|
*/ |
|
|
|
|
public function release($name) |
|
|
|
@ -98,15 +98,15 @@ abstract class Mutex extends Component
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* This method should be extended by concrete mutex implementations. Acquires lock by given name. |
|
|
|
|
* This method should be extended by a concrete Mutex implementations. Acquires lock by name. |
|
|
|
|
* @param string $name of the lock to be acquired. |
|
|
|
|
* @param integer $timeout to wait for lock to become released. |
|
|
|
|
* @param integer $timeout time to wait for the lock to be released. |
|
|
|
|
* @return boolean acquiring result. |
|
|
|
|
*/ |
|
|
|
|
abstract protected function acquireLock($name, $timeout = 0); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* This method should be extended by concrete mutex implementations. Releases lock by given name. |
|
|
|
|
* This method should be extended by a concrete Mutex implementations. Releases lock by given name. |
|
|
|
|
* @param string $name of the lock to be released. |
|
|
|
|
* @return boolean release result. |
|
|
|
|
*/ |
|
|
|
|