]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/AbstractLockDriver.php
Avoid memory issue in exception
[friendica.git] / src / Core / Lock / AbstractLockDriver.php
1 <?php
2
3 namespace Friendica\Core\Lock;
4 use Friendica\BaseObject;
5
6 /**
7  * Class AbstractLockDriver
8  *
9  * @package Friendica\Core\Lock
10  *
11  * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
12  */
13 abstract class AbstractLockDriver extends BaseObject implements ILockDriver
14 {
15         /**
16          * @var array The local acquired locks
17          */
18         protected $acquiredLocks = [];
19
20         /**
21          * Check if we've locally acquired a lock
22          *
23          * @param string key The Name of the lock
24          * @return bool      Returns true if the lock is set
25          */
26         protected function hasAcquiredLock($key) {
27                 return isset($this->acquireLock[$key]) && $this->acquiredLocks[$key] === true;
28         }
29
30         /**
31          * Mark a locally acquired lock
32          *
33          * @param string $key The Name of the lock
34          */
35         protected function markAcquire($key) {
36                 $this->acquiredLocks[$key] = true;
37         }
38
39         /**
40          * Mark a release of a locally acquired lock
41          *
42          * @param string $key The Name of the lock
43          */
44         protected function markRelease($key) {
45                 unset($this->acquiredLocks[$key]);
46         }
47
48         /**
49          * Releases all lock that were set by us
50          *
51          * @return void
52          */
53         public function releaseAll() {
54                 foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
55                         $this->releaseLock($acquiredLock);
56                 }
57         }
58 }