Passed
Push — master ( f944e1...6aa0ac )
by Alexander
112:37 queued 109:12
created

Mutex::isAcquired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\mutex;
9
10
use yii\base\Component;
11
12
/**
13
 * The Mutex component allows mutual execution of concurrent processes in order to prevent "race conditions".
14
 *
15
 * This is achieved by using a "lock" mechanism. Each possibly concurrent thread cooperates by acquiring
16
 * a lock before accessing the corresponding data.
17
 *
18
 * Usage example:
19
 *
20
 * ```
21
 * if ($mutex->acquire($mutexName)) {
22
 *     // business logic execution
23
 * } else {
24
 *     // execution is blocked!
25
 * }
26
 * ```
27
 *
28
 * This is a base class, which should be extended in order to implement the actual lock mechanism.
29
 *
30
 * @author resurtm <[email protected]>
31
 * @since 2.0
32
 */
33
abstract class Mutex extends Component
34
{
35
    /**
36
     * @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically
37
     * before finishing script execution. Defaults to true. Setting this property to true means that all locks
38
     * acquired in this process must be released (regardless of errors or exceptions).
39
     */
40
    public $autoRelease = true;
41
42
    /**
43
     * @var string[] names of the locks acquired by the current PHP process.
44
     */
45
    private $_locks = [];
46
47
48
    /**
49
     * Initializes the Mutex component.
50
     */
51 43
    public function init()
52
    {
53 43
        if ($this->autoRelease) {
54 43
            $locks = &$this->_locks;
55 43
            register_shutdown_function(function () use (&$locks) {
56
                foreach ($locks as $lock) {
57
                    $this->release($lock);
58
                }
59 43
            });
60
        }
61 43
    }
62
63
    /**
64
     * Acquires a lock by name.
65
     * @param string $name of the lock to be acquired. Must be unique.
66
     * @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return
67
     * false immediately in case lock was already acquired.
68
     * @return bool lock acquiring result.
69
     */
70 43
    public function acquire($name, $timeout = 0)
71
    {
72 43
        if (!in_array($name, $this->_locks, true) && $this->acquireLock($name, $timeout)) {
73 43
            $this->_locks[] = $name;
74
75 43
            return true;
76
        }
77
78 22
        return false;
79
    }
80
81
    /**
82
     * Releases acquired lock. This method will return false in case the lock was not found.
83
     * @param string $name of the lock to be released. This lock must already exist.
84
     * @return bool lock release result: false in case named lock was not found..
85
     */
86 42
    public function release($name)
87
    {
88 42
        if ($this->releaseLock($name)) {
89 42
            $index = array_search($name, $this->_locks);
90 42
            if ($index !== false) {
91 42
                unset($this->_locks[$index]);
92
            }
93
94 42
            return true;
95
        }
96
97 21
        return false;
98
    }
99
100
    /**
101
     * Checks if a lock is currently acquired
102
     *
103
     * @param string $name of the lock to check
104
     * @return bool Returns true if currently acquired
105
     * @since 2.0.36
106
     */
107 9
    public function isAcquired($name) {
108 9
        return in_array($name, $this->_locks, true);
109
    }
110
111
    /**
112
     * This method should be extended by a concrete Mutex implementations. Acquires lock by name.
113
     * @param string $name of the lock to be acquired.
114
     * @param int $timeout time (in seconds) to wait for the lock to be released.
115
     * @return bool acquiring result.
116
     */
117
    abstract protected function acquireLock($name, $timeout = 0);
118
119
    /**
120
     * This method should be extended by a concrete Mutex implementations. Releases lock by given name.
121
     * @param string $name of the lock to be released.
122
     * @return bool release result.
123
     */
124
    abstract protected function releaseLock($name);
125
}
126