27
27
final class LockRegistry
28
28
{
29
29
private static $ openedFiles = [];
30
- private static $ lockedKeys ;
30
+ private static $ lockedFiles ;
31
31
32
32
/**
33
33
* 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
77
77
fclose ($ file );
78
78
}
79
79
}
80
- self ::$ openedFiles = self ::$ lockedKeys = [];
80
+ self ::$ openedFiles = self ::$ lockedFiles = [];
81
81
82
82
return $ previousFiles ;
83
83
}
84
84
85
85
public static function compute (callable $ callback , ItemInterface $ item , bool &$ save , CacheInterface $ pool , \Closure $ setMetadata = null , LoggerInterface $ logger = null )
86
86
{
87
- if ('\\' === \DIRECTORY_SEPARATOR && null === self ::$ lockedKeys ) {
87
+ if ('\\' === \DIRECTORY_SEPARATOR && null === self ::$ lockedFiles ) {
88
88
// disable locking on Windows by default
89
- self ::$ files = self ::$ lockedKeys = [];
89
+ self ::$ files = self ::$ lockedFiles = [];
90
90
}
91
91
92
- $ key = unpack ( ' i ' , md5 ( $ item ->getKey (), true ))[ 1 ] ;
92
+ $ key = self :: $ files ? abs ( crc32 ( $ item ->getKey ())) % \count ( self :: $ files ) : - 1 ;
93
93
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 )) {
99
95
return $ callback ($ item , $ save );
100
96
}
101
97
102
98
while (true ) {
103
99
try {
104
100
$ locked = false ;
105
101
// 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 );
111
103
112
104
if ($ locked || !$ wouldBlock ) {
113
105
$ 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 ;
115
107
116
108
$ value = $ callback ($ item , $ save );
117
109
@@ -126,25 +118,12 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
126
118
127
119
return $ value ;
128
120
}
129
-
130
121
// if we failed the race, retry locking in blocking mode to wait for the winner
131
122
$ 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 );
139
124
} 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 ]);
148
127
}
149
128
static $ signalingException , $ signalingCallback ;
150
129
$ 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
169
148
170
149
private static function open (int $ key )
171
150
{
172
- if (\function_exists ('sem_get ' )) {
173
- return sem_get ($ key );
174
- }
175
-
176
151
if (null !== $ h = self ::$ openedFiles [$ key ] ?? null ) {
177
152
return $ h ;
178
153
}
0 commit comments