Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Cache] Revert "feature #41989 make LockRegistry use semaphores when possible" #44667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion 1 src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 11 additions & 36 deletions 47 src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -77,41 +77,33 @@ 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);
}

while (true) {
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);

Expand All @@ -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:{}}");
Expand All @@ -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;
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.