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

Commit eb60956

Browse filesBrowse files
bug #44667 [Cache] Revert "feature #41989 make LockRegistry use semaphores when possible" (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [Cache] Revert "feature #41989 make `LockRegistry` use semaphores when possible" | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #44536 | License | MIT | Doc PR | - This reverts commit 479919d, reversing changes made to 356c953. I'd like to revert this PR because using semaphores is creating problems that don't have any easy solution. Commits ------- 6bcc3cb Revert "feature #41989 [Cache] make `LockRegistry` use semaphores when possible (nicolas-grekas)"
2 parents b6d4480 + 6bcc3cb commit eb60956
Copy full SHA for eb60956

File tree

Expand file treeCollapse file tree

2 files changed

+11
-37
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+11
-37
lines changed

‎src/Symfony/Component/Cache/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/CHANGELOG.md
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ CHANGELOG
44
5.4
55
---
66

7-
* Make `LockRegistry` use semaphores when possible
87
* Deprecate `DoctrineProvider` and `DoctrineAdapter` because these classes have been added to the `doctrine/cache` package
98
* Add `DoctrineDbalAdapter` identical to `PdoAdapter` for `Doctrine\DBAL\Connection` or DBAL URL
109
* Deprecate usage of `PdoAdapter` with `Doctrine\DBAL\Connection` or DBAL URL

‎src/Symfony/Component/Cache/LockRegistry.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/LockRegistry.php
+11-36Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
final class LockRegistry
2828
{
2929
private static $openedFiles = [];
30-
private static $lockedKeys;
30+
private static $lockedFiles;
3131

3232
/**
3333
* The number of items in this list controls the max number of concurrent processes.
@@ -77,41 +77,33 @@ public static function setFiles(array $files): array
7777
fclose($file);
7878
}
7979
}
80-
self::$openedFiles = self::$lockedKeys = [];
80+
self::$openedFiles = self::$lockedFiles = [];
8181

8282
return $previousFiles;
8383
}
8484

8585
public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null, LoggerInterface $logger = null)
8686
{
87-
if ('\\' === \DIRECTORY_SEPARATOR && null === self::$lockedKeys) {
87+
if ('\\' === \DIRECTORY_SEPARATOR && null === self::$lockedFiles) {
8888
// disable locking on Windows by default
89-
self::$files = self::$lockedKeys = [];
89+
self::$files = self::$lockedFiles = [];
9090
}
9191

92-
$key = unpack('i', md5($item->getKey(), true))[1];
92+
$key = self::$files ? abs(crc32($item->getKey())) % \count(self::$files) : -1;
9393

94-
if (!\function_exists('sem_get')) {
95-
$key = self::$files ? abs($key) % \count(self::$files) : null;
96-
}
97-
98-
if (null === $key || (self::$lockedKeys[$key] ?? false) || !$lock = self::open($key)) {
94+
if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) {
9995
return $callback($item, $save);
10096
}
10197

10298
while (true) {
10399
try {
104100
$locked = false;
105101
// race to get the lock in non-blocking mode
106-
if ($wouldBlock = \function_exists('sem_get')) {
107-
$locked = @sem_acquire($lock, true);
108-
} else {
109-
$locked = flock($lock, \LOCK_EX | \LOCK_NB, $wouldBlock);
110-
}
102+
$locked = flock($lock, \LOCK_EX | \LOCK_NB, $wouldBlock);
111103

112104
if ($locked || !$wouldBlock) {
113105
$logger && $logger->info(sprintf('Lock %s, now computing item "{key}"', $locked ? 'acquired' : 'not supported'), ['key' => $item->getKey()]);
114-
self::$lockedKeys[$key] = true;
106+
self::$lockedFiles[$key] = true;
115107

116108
$value = $callback($item, $save);
117109

@@ -126,25 +118,12 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
126118

127119
return $value;
128120
}
129-
130121
// if we failed the race, retry locking in blocking mode to wait for the winner
131122
$logger && $logger->info('Item "{key}" is locked, waiting for it to be released', ['key' => $item->getKey()]);
132-
133-
if (\function_exists('sem_get')) {
134-
$lock = sem_get($key);
135-
@sem_acquire($lock);
136-
} else {
137-
flock($lock, \LOCK_SH);
138-
}
123+
flock($lock, \LOCK_SH);
139124
} finally {
140-
if ($locked) {
141-
if (\function_exists('sem_get')) {
142-
sem_remove($lock);
143-
} else {
144-
flock($lock, \LOCK_UN);
145-
}
146-
}
147-
unset(self::$lockedKeys[$key]);
125+
flock($lock, \LOCK_UN);
126+
unset(self::$lockedFiles[$key]);
148127
}
149128
static $signalingException, $signalingCallback;
150129
$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
169148

170149
private static function open(int $key)
171150
{
172-
if (\function_exists('sem_get')) {
173-
return sem_get($key);
174-
}
175-
176151
if (null !== $h = self::$openedFiles[$key] ?? null) {
177152
return $h;
178153
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.