]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock/SemaphoreLock.php
Merge pull request #8269 from MrPetovan/bug/frio-more-actions
[friendica.git] / src / Core / Lock / SemaphoreLock.php
index 0f41f9f309556cf7355016044ce7ba85294d3f93..25bc9aa1d4a6201b24afa0e5138c8fdb6adf68bd 100644 (file)
@@ -1,10 +1,30 @@
 <?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Core\Lock;
 
-use Friendica\Core\Cache;
+use Friendica\Core\BaseLock;
+use Friendica\Core\Cache\Duration;
 
-class SemaphoreLock extends Lock
+class SemaphoreLock extends BaseLock
 {
        private static $semaphore = [];
 
@@ -20,37 +40,27 @@ class SemaphoreLock extends Lock
         */
        private static function semaphoreKey($key)
        {
-               $file = self::keyToFile($key);
+               $success = true;
 
-               if (!file_exists($file)) {
-                       file_put_contents($file, $key);
-               }
+               $temp = get_temppath();
 
-               return ftok($file, 'f');
-       }
+               $file = $temp . '/' . $key . '.sem';
 
-       /**
-        * Returns the full path to the semaphore file
-        *
-        * @param string $key The key of the semaphore
-        *
-        * @return string The full path
-        */
-       private static function keyToFile($key)
-       {
-               $temp = get_temppath();
+               if (!file_exists($file)) {
+                       $success = !empty(file_put_contents($file, $key));
+               }
 
-               return $temp . '/' . $key . '.sem';
+               return $success ? ftok($file, 'f') : false;
        }
 
        /**
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
+       public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
        {
                self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
-               if (self::$semaphore[$key]) {
-                       if (sem_acquire(self::$semaphore[$key], ($timeout == 0))) {
+               if (!empty(self::$semaphore[$key])) {
+                       if ((bool)sem_acquire(self::$semaphore[$key], ($timeout === 0))) {
                                $this->markAcquire($key);
                                return true;
                        }
@@ -61,26 +71,22 @@ class SemaphoreLock extends Lock
 
        /**
         * (@inheritdoc)
+        *
+        * @param bool $override not necessary parameter for semaphore locks since the lock lives as long as the execution
+        *                       of the using function
         */
-       public function releaseLock($key, $override = false)
+       public function release($key, $override = false)
        {
                $success = false;
 
                if (!empty(self::$semaphore[$key])) {
                        try {
                                $success = @sem_release(self::$semaphore[$key]);
-                               if (file_exists(self::keyToFile($key)) && $success) {
-                                       $success = unlink(self::keyToFile($key));
-                               }
                                unset(self::$semaphore[$key]);
                                $this->markRelease($key);
                        } catch (\Exception $exception) {
                                $success = false;
                        }
-               } else if ($override) {
-                       if ($this->acquireLock($key)) {
-                               $success = $this->releaseLock($key, true);
-                       }
                }
 
                return $success;
@@ -99,7 +105,7 @@ class SemaphoreLock extends Lock
         */
        public function getName()
        {
-               return self::TYPE_SEMAPHORE;
+               return Type::SEMAPHORE;
        }
 
        /**
@@ -107,16 +113,23 @@ class SemaphoreLock extends Lock
         */
        public function getLocks(string $prefix = '')
        {
-               $temp = get_temppath();
-               $locks = [];
-               foreach (glob(sprintf('%s/%s*.sem', $temp, $prefix)) as $lock) {
-                       $lock = pathinfo($lock, PATHINFO_FILENAME);
-                       if(sem_get(self::semaphoreKey($lock))) {
-                               $locks[] = $lock;
+               // We can just return our own semaphore keys, since we don't know
+               // the state of other semaphores, even if the .sem files exists
+               $keys = array_keys(self::$semaphore);
+
+               if (empty($prefix)) {
+                       return $keys;
+               } else {
+                       $result = [];
+
+                       foreach ($keys as $key) {
+                               if (strpos($key, $prefix) === 0) {
+                                       array_push($result, $key);
+                               }
                        }
-               }
 
-               return $locks;
+                       return $result;
+               }
        }
 
        /**
@@ -124,16 +137,8 @@ class SemaphoreLock extends Lock
         */
        public function releaseAll($override = false)
        {
-               $success = parent::releaseAll($override);
-
-               $temp = get_temppath();
-               foreach (glob(sprintf('%s/*.sem', $temp)) as $lock) {
-                       $lock = pathinfo($lock, PATHINFO_FILENAME);
-                       if (!$this->releaseLock($lock, true)) {
-                               $success = false;
-                       }
-               }
-
-               return $success;
+               // Semaphores are just alive during a run, so there is no need to release
+               // You can just release your own locks
+               return parent::releaseAll($override);
        }
 }