5
5
use SplObjectStorage ;
6
6
use function Amp \async ;
7
7
use function Amp \Future \awaitAll ;
8
- use function register_shutdown_function ;
9
8
10
9
/**
11
10
* A handle on an acquired lock from a synchronization object.
15
14
*/
16
15
final class Lock
17
16
{
18
- private static \Fiber $ testFiber ;
17
+ private static ? \Fiber $ testFiber = null ;
19
18
20
19
private static ?\SplObjectStorage $ pendingOperations = null ;
21
20
@@ -34,17 +33,15 @@ public function __construct(\Closure $release)
34
33
35
34
private static function setupPendingOperations (): SplObjectStorage
36
35
{
37
- if (self ::$ pendingOperations === null ) {
38
- self ::$ pendingOperations = new SplObjectStorage ();
39
-
40
- register_shutdown_function (static function () {
41
- while (self ::$ pendingOperations ->count () > 0 ) {
42
- awaitAll (self ::$ pendingOperations );
43
- }
44
- });
45
- }
36
+ $ pending = new SplObjectStorage ();
37
+
38
+ \register_shutdown_function (static function () use ($ pending ): void {
39
+ while ($ pending ->count () > 0 ) {
40
+ awaitAll ($ pending );
41
+ }
42
+ });
46
43
47
- return self :: $ pendingOperations ;
44
+ return $ pending ;
48
45
}
49
46
50
47
/**
@@ -73,9 +70,9 @@ public function release(): void
73
70
if ($ this ->isForceClosed ()) {
74
71
$ future = async ($ release );
75
72
76
- self ::$ pendingOperations = self ::setupPendingOperations ();
77
- self :: $ pendingOperations ->attach ($ future );
78
- $ future ->finally (fn () => self :: $ pendingOperations ->detach ($ future ));
73
+ $ pending = self ::$ pendingOperations ?? = self ::setupPendingOperations ();
74
+ $ pending ->attach ($ future );
75
+ $ future ->finally (fn () => $ pending ->detach ($ future ));
79
76
} else {
80
77
$ release ();
81
78
}
@@ -94,17 +91,17 @@ public function __destruct()
94
91
95
92
private function isForceClosed (): bool
96
93
{
97
- self ::$ testFiber ??= new \Fiber (function () {
94
+ $ fiber = self ::$ testFiber ??= new \Fiber (function () {
98
95
while (true ) {
99
96
\Fiber::suspend ();
100
97
}
101
98
});
102
99
103
100
try {
104
- if (self :: $ testFiber ->isStarted ()) {
105
- self :: $ testFiber ->resume ();
101
+ if ($ fiber ->isStarted ()) {
102
+ $ fiber ->resume ();
106
103
} else {
107
- self :: $ testFiber ->start ();
104
+ $ fiber ->start ();
108
105
}
109
106
110
107
return false ;
0 commit comments