]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/SemaphoreLock.php
Merge pull request #9160 from MrPetovan/bug/9138-escape-field-input
[friendica.git] / src / Core / Lock / SemaphoreLock.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Lock;
23
24 use Friendica\Core\BaseLock;
25 use Friendica\Core\Cache\Duration;
26
27 class SemaphoreLock extends BaseLock
28 {
29         private static $semaphore = [];
30
31         public function __construct()
32         {
33                 if (!function_exists('sem_get')) {
34                         throw new \Exception('Semaphore lock not supported');
35                 }
36         }
37
38         /**
39          * (@inheritdoc)
40          */
41         private static function semaphoreKey($key)
42         {
43                 $success = true;
44
45                 $temp = get_temppath();
46
47                 $file = $temp . '/' . $key . '.sem';
48
49                 if (!file_exists($file)) {
50                         $success = !empty(file_put_contents($file, $key));
51                 }
52
53                 return $success ? ftok($file, 'f') : false;
54         }
55
56         /**
57          * (@inheritdoc)
58          */
59         public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
60         {
61                 self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
62                 if (!empty(self::$semaphore[$key])) {
63                         if ((bool)sem_acquire(self::$semaphore[$key], ($timeout === 0))) {
64                                 $this->markAcquire($key);
65                                 return true;
66                         }
67                 }
68
69                 return false;
70         }
71
72         /**
73          * (@inheritdoc)
74          *
75          * @param bool $override not necessary parameter for semaphore locks since the lock lives as long as the execution
76          *                       of the using function
77          */
78         public function release($key, $override = false)
79         {
80                 $success = false;
81
82                 if (!empty(self::$semaphore[$key])) {
83                         try {
84                                 $success = @sem_release(self::$semaphore[$key]);
85                                 unset(self::$semaphore[$key]);
86                                 $this->markRelease($key);
87                         } catch (\Exception $exception) {
88                                 $success = false;
89                         }
90                 }
91
92                 return $success;
93         }
94
95         /**
96          * (@inheritdoc)
97          */
98         public function isLocked($key)
99         {
100                 return isset(self::$semaphore[$key]);
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         public function getName()
107         {
108                 return Type::SEMAPHORE;
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         public function getLocks(string $prefix = '')
115         {
116                 // We can just return our own semaphore keys, since we don't know
117                 // the state of other semaphores, even if the .sem files exists
118                 $keys = array_keys(self::$semaphore);
119
120                 if (empty($prefix)) {
121                         return $keys;
122                 } else {
123                         $result = [];
124
125                         foreach ($keys as $key) {
126                                 if (strpos($key, $prefix) === 0) {
127                                         array_push($result, $key);
128                                 }
129                         }
130
131                         return $result;
132                 }
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         public function releaseAll($override = false)
139         {
140                 // Semaphores are just alive during a run, so there is no need to release
141                 // You can just release your own locks
142                 return parent::releaseAll($override);
143         }
144 }