]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock/SemaphoreLockDriver.php
Merge pull request #6955 from tobiasd/20190331-vier
[friendica.git] / src / Core / Lock / SemaphoreLockDriver.php
index 93911a3d10207e80a58113e76865566d04221245..781e110b1708a83633d1497296448ba639632595 100644 (file)
@@ -2,25 +2,27 @@
 
 namespace Friendica\Core\Lock;
 
-class SemaphoreLockDriver implements ILockDriver
+use Friendica\Core\Cache;
+
+class SemaphoreLockDriver extends AbstractLockDriver
 {
-       /**
-        * @var array stored semaphores
-        */
        private static $semaphore = [];
 
+       public function __construct()
+       {
+               if (!function_exists('sem_get')) {
+                       throw new \Exception('Semaphore lock not supported');
+               }
+       }
+
        /**
-        * @brief Creates a semaphore key
-        *
-        * @param string $key Name of the lock
-        *
-        * @return integer the semaphore key
+        * (@inheritdoc)
         */
        private static function semaphoreKey($key)
        {
                $temp = get_temppath();
 
-               $file = $temp.'/'.$key.'.sem';
+               $file = $temp . '/' . $key . '.sem';
 
                if (!file_exists($file)) {
                        file_put_contents($file, $key);
@@ -30,53 +32,41 @@ class SemaphoreLockDriver implements ILockDriver
        }
 
        /**
-        *
-        * @brief Sets a lock for a given name
-        *
-        * @param string $key The Name of the lock
-        * @param integer $timeout Seconds until we give up
-        *
-        * @return boolean Was the lock successful?
+        * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
-               // The second parameter for "sem_acquire" doesn't exist before 5.6.1
-               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                       self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
-                       if (self::$semaphore[$key]) {
-                               return sem_acquire(self::$semaphore[$key], ($timeout == 0));
+               self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
+               if (self::$semaphore[$key]) {
+                       if (sem_acquire(self::$semaphore[$key], ($timeout == 0))) {
+                               $this->markAcquire($key);
+                               return true;
                        }
                }
+
+               return false;
        }
 
        /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
+        * (@inheritdoc)
         */
-       public function releaseLock($key)
+       public function releaseLock($key, $override = false)
        {
-               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                       if (empty(self::$semaphore[$key])) {
-                               return false;
-                       } else {
-                               $success = @sem_release(self::$semaphore[$key]);
-                               unset(self::$semaphore[$key]);
-                               return $success;
-                       }
+               if (empty(self::$semaphore[$key])) {
+                       return false;
+               } else {
+                       $success = @sem_release(self::$semaphore[$key]);
+                       unset(self::$semaphore[$key]);
+                       $this->markRelease($key);
+                       return $success;
                }
        }
 
        /**
-        * @brief Removes all lock that were set by us
-        *
-        * @return void
+        * (@inheritdoc)
         */
-       public function releaseAll()
+       public function isLocked($key)
        {
-               // not needed/supported
-               return;
+               return isset(self::$semaphore[$key]);
        }
-}
\ No newline at end of file
+}