]> git.mxchange.org Git - friendica.git/blob - src/Core/BaseLock.php
Merge pull request #8222 from annando/ap-gnusocial
[friendica.git] / src / Core / BaseLock.php
1 <?php
2
3 namespace Friendica\Core;
4
5 use Friendica\Core\Lock\ILock;
6
7 /**
8  * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
9  */
10 abstract class BaseLock implements ILock
11 {
12         /**
13          * @var array The local acquired locks
14          */
15         protected $acquiredLocks = [];
16
17         /**
18          * Check if we've locally acquired a lock
19          *
20          * @param string key The Name of the lock
21          *
22          * @return bool      Returns true if the lock is set
23          */
24         protected function hasAcquiredLock($key)
25         {
26                 return isset($this->acquireLock[$key]) && $this->acquiredLocks[$key] === true;
27         }
28
29         /**
30          * Mark a locally acquired lock
31          *
32          * @param string $key The Name of the lock
33          */
34         protected function markAcquire($key)
35         {
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         {
46                 unset($this->acquiredLocks[$key]);
47         }
48
49         /**
50          * {@inheritDoc}
51          */
52         public function releaseAll($override = false)
53         {
54                 $return = true;
55
56                 foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
57                         if (!$this->release($acquiredLock, $override)) {
58                                 $return = false;
59                         }
60                 }
61
62                 return $return;
63         }
64 }