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