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