Completed
Push — 2.1 ( 8ecc36...9546bd )
by Alexander
13:31
created

Mutex::releaseLock()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
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 examples:
19
 *
20
 * ```
21
 * if ($mutex->acquire($mutexName)) {
22
 *     // business logic execution
23
 * } else {
24
 *     // execution is blocked!
25
 * }
26
 * ```
27
 *
28
 * ```
29
 * $mutex->sync($mutexName, 10, function () {
30
 *     // business logic execution with synchronization
31
 * }));
32
 * ```
33
 *
34
 * This is a base class, which should be extended in order to implement the actual lock mechanism.
35
 *
36
 * @author resurtm <[email protected]>
37
 * @since 2.0
38
 */
39
abstract class Mutex extends Component
40
{
41
    /**
42
     * @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically
43
     * before finishing script execution. Defaults to true. Setting this property to true means that all locks
44
     * acquired in this process must be released (regardless of errors or exceptions).
45
     */
46
    public $autoRelease = true;
47
48
    /**
49
     * @var string[] names of the locks acquired by the current PHP process.
50
     */
51
    private $_locks = [];
52
53
54
    /**
55
     * Initializes the Mutex component.
56
     */
57 16
    public function init()
58
    {
59 16
        if ($this->autoRelease) {
60 16
            $locks = &$this->_locks;
61 16
            register_shutdown_function(function () use (&$locks) {
62
                foreach ($locks as $lock) {
63
                    $this->release($lock);
64
                }
65 16
            });
66
        }
67 16
    }
68
69
    /**
70
     * Acquires a lock by name.
71
     * @param string $name of the lock to be acquired. Must be unique.
72
     * @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return
73
     * false immediately in case lock was already acquired.
74
     * @return bool lock acquiring result.
75
     */
76 16
    public function acquire($name, $timeout = 0)
77
    {
78 16
        if ($this->acquireLock($name, $timeout)) {
79 16
            $this->_locks[] = $name;
80
81 16
            return true;
82
        }
83
84 9
        return false;
85
    }
86
87
    /**
88
     * Releases acquired lock. This method will return false in case the lock was not found.
89
     * @param string $name of the lock to be released. This lock must already exist.
90
     * @return bool lock release result: false in case named lock was not found..
91
     */
92 13
    public function release($name)
93
    {
94 13
        if ($this->releaseLock($name)) {
95 13
            $index = array_search($name, $this->_locks);
96 13
            if ($index !== false) {
97 13
                unset($this->_locks[$index]);
98
            }
99
100 13
            return true;
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * Executes callback with mutex synchronization.
108
     *
109
     * @param string $name of the lock to be acquired. Must be unique.
110
     * @param int $timeout time (in seconds) to wait for lock to be released.
111
     * @param callable $callback a valid PHP callback that performs the job. Accepts mutex instance as parameter.
112
     * @param bool $throw whether to throw an exception when the lock is not acquired.
113
     * @return mixed result of callback function, or null when the lock is not acquired.
114
     * @throws SyncException when the lock is not acquired.
115
     * @since 2.1
116
     */
117 9
    public function sync($name, $timeout, callable $callback, $throw = true)
118
    {
119 9
        if ($this->acquire($name, $timeout)) {
120
            try {
121 3
                $result = call_user_func($callback, $this);
122 3
            } finally {
123 3
                $this->release($name);
124
            }
125 3
            return $result;
126
        }
127 6
        if ($throw) {
128 3
            throw new SyncException('Cannot acquire the lock.');
129
        }
130 3
        return null;
131
    }
132
133
    /**
134
     * This method should be extended by a concrete Mutex implementations. Acquires lock by name.
135
     * @param string $name of the lock to be acquired.
136
     * @param int $timeout time (in seconds) to wait for the lock to be released.
137
     * @return bool acquiring result.
138
     */
139
    abstract protected function acquireLock($name, $timeout = 0);
140
141
    /**
142
     * This method should be extended by a concrete Mutex implementations. Releases lock by given name.
143
     * @param string $name of the lock to be released.
144
     * @return bool release result.
145
     */
146
    abstract protected function releaseLock($name);
147
}
148