X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FLock%2FSemaphoreLock.php;h=d5153d432e46b0ccf68a27f6e18e02c1429fcb48;hb=c803dcb6c5f808ac8f962e9bd5e631a91655cbc7;hp=75c7284a5fd8085c64aba0c485def8bdad92d9da;hpb=41e2031e6b7b572c00aea53136baaef38cd79c86;p=friendica.git diff --git a/src/Core/Lock/SemaphoreLock.php b/src/Core/Lock/SemaphoreLock.php index 75c7284a5f..d5153d432e 100644 --- a/src/Core/Lock/SemaphoreLock.php +++ b/src/Core/Lock/SemaphoreLock.php @@ -20,27 +20,17 @@ 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; } /** @@ -61,6 +51,9 @@ 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) { @@ -68,17 +61,12 @@ class SemaphoreLock extends Lock if (!empty(self::$semaphore[$key])) { try { - $success = @sem_release(self::$semaphore[$key]) && - unlink(self::keyToFile($key)); + $success = @sem_release(self::$semaphore[$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; @@ -105,16 +93,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; + } } /** @@ -122,16 +117,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); } }