From 6bcc3cb2de5480a817556f433427ebb54dc721c3 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 16 Dec 2021 17:13:28 +0100 Subject: [PATCH] Revert "feature #41989 [Cache] make `LockRegistry` use semaphores when possible (nicolas-grekas)" This reverts commit 479919d4d5cbc17710d16a04ff9728d1519afb8e, reversing changes made to 356c9533cc520b5aab5d92d0b06a408beb22a157. --- src/Symfony/Component/Cache/CHANGELOG.md | 1 - src/Symfony/Component/Cache/LockRegistry.php | 47 +++++--------------- 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/Cache/CHANGELOG.md b/src/Symfony/Component/Cache/CHANGELOG.md index 0654b0389ad6a..60a862740d1e7 100644 --- a/src/Symfony/Component/Cache/CHANGELOG.md +++ b/src/Symfony/Component/Cache/CHANGELOG.md @@ -4,7 +4,6 @@ CHANGELOG 5.4 --- - * Make `LockRegistry` use semaphores when possible * Deprecate `DoctrineProvider` and `DoctrineAdapter` because these classes have been added to the `doctrine/cache` package * Add `DoctrineDbalAdapter` identical to `PdoAdapter` for `Doctrine\DBAL\Connection` or DBAL URL * Deprecate usage of `PdoAdapter` with `Doctrine\DBAL\Connection` or DBAL URL diff --git a/src/Symfony/Component/Cache/LockRegistry.php b/src/Symfony/Component/Cache/LockRegistry.php index 910c11fae29c4..2e506a1032433 100644 --- a/src/Symfony/Component/Cache/LockRegistry.php +++ b/src/Symfony/Component/Cache/LockRegistry.php @@ -27,7 +27,7 @@ final class LockRegistry { private static $openedFiles = []; - private static $lockedKeys; + private static $lockedFiles; /** * The number of items in this list controls the max number of concurrent processes. @@ -77,25 +77,21 @@ public static function setFiles(array $files): array fclose($file); } } - self::$openedFiles = self::$lockedKeys = []; + self::$openedFiles = self::$lockedFiles = []; return $previousFiles; } public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null, LoggerInterface $logger = null) { - if ('\\' === \DIRECTORY_SEPARATOR && null === self::$lockedKeys) { + if ('\\' === \DIRECTORY_SEPARATOR && null === self::$lockedFiles) { // disable locking on Windows by default - self::$files = self::$lockedKeys = []; + self::$files = self::$lockedFiles = []; } - $key = unpack('i', md5($item->getKey(), true))[1]; + $key = self::$files ? abs(crc32($item->getKey())) % \count(self::$files) : -1; - if (!\function_exists('sem_get')) { - $key = self::$files ? abs($key) % \count(self::$files) : null; - } - - if (null === $key || (self::$lockedKeys[$key] ?? false) || !$lock = self::open($key)) { + if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) { return $callback($item, $save); } @@ -103,15 +99,11 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s try { $locked = false; // race to get the lock in non-blocking mode - if ($wouldBlock = \function_exists('sem_get')) { - $locked = @sem_acquire($lock, true); - } else { - $locked = flock($lock, \LOCK_EX | \LOCK_NB, $wouldBlock); - } + $locked = flock($lock, \LOCK_EX | \LOCK_NB, $wouldBlock); if ($locked || !$wouldBlock) { $logger && $logger->info(sprintf('Lock %s, now computing item "{key}"', $locked ? 'acquired' : 'not supported'), ['key' => $item->getKey()]); - self::$lockedKeys[$key] = true; + self::$lockedFiles[$key] = true; $value = $callback($item, $save); @@ -126,25 +118,12 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s return $value; } - // if we failed the race, retry locking in blocking mode to wait for the winner $logger && $logger->info('Item "{key}" is locked, waiting for it to be released', ['key' => $item->getKey()]); - - if (\function_exists('sem_get')) { - $lock = sem_get($key); - @sem_acquire($lock); - } else { - flock($lock, \LOCK_SH); - } + flock($lock, \LOCK_SH); } finally { - if ($locked) { - if (\function_exists('sem_get')) { - sem_remove($lock); - } else { - flock($lock, \LOCK_UN); - } - } - unset(self::$lockedKeys[$key]); + flock($lock, \LOCK_UN); + unset(self::$lockedFiles[$key]); } static $signalingException, $signalingCallback; $signalingException = $signalingException ?? unserialize("O:9:\"Exception\":1:{s:16:\"\0Exception\0trace\";a:0:{}}"); @@ -169,10 +148,6 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s private static function open(int $key) { - if (\function_exists('sem_get')) { - return sem_get($key); - } - if (null !== $h = self::$openedFiles[$key] ?? null) { return $h; }