@@ -8,36 +8,53 @@ register_shutdown_function(function () {
8
8
});
9
9
10
10
class Cycle {
11
- public static $ counter = 0 ;
12
11
public $ self ;
13
- public function __construct () {
12
+ public function __construct (public int $ id ) {
14
13
$ this ->self = $ this ;
15
14
}
16
15
public function __destruct () {
17
- $ id = self ::$ counter ++;
18
- printf ("%d: Start destruct \n" , $ id );
16
+ printf ("%d: Start destruct \n" , $ this ->id );
19
17
try {
20
- if ($ id === 0 ) {
18
+ if ($ this ->id === 0 ) {
19
+ /* Fiber will be collected by GC because it's not referenced */
20
+ Fiber::suspend (new stdClass );
21
+ } else if ($ this ->id === 1 ) {
22
+ /* Fiber will be dtor during shutdown */
23
+ global $ f2 ;
24
+ $ f2 = Fiber::getCurrent ();
21
25
Fiber::suspend (new stdClass );
22
26
}
23
27
} finally {
24
- printf ("%d: End destruct \n" , $ id );
28
+ printf ("%d: End destruct \n" , $ this -> id );
25
29
}
26
30
}
27
31
}
28
32
29
- $ f = new Fiber (function () {
30
- new Cycle ();
31
- new Cycle ();
33
+ $ refs = [];
34
+ $ f = new Fiber (function () use (&$ refs ) {
35
+ $ refs [] = WeakReference::create (new Cycle (0 ));
36
+ $ refs [] = WeakReference::create (new Cycle (1 ));
37
+ $ refs [] = WeakReference::create (new Cycle (2 ));
32
38
gc_collect_cycles ();
33
39
});
34
40
35
41
$ f ->start ();
36
42
43
+ gc_collect_cycles ();
44
+
45
+ foreach ($ refs as $ id => $ ref ) {
46
+ printf ("%d: %s \n" , $ id , $ ref ->get () ? 'Live ' : 'Collected ' );
47
+ }
48
+
37
49
?>
38
50
--EXPECT--
51
+ 2: Start destruct
52
+ 2: End destruct
39
53
0: Start destruct
54
+ 0: End destruct
40
55
1: Start destruct
41
- 1: End destruct
56
+ 0: Collected
57
+ 1: Live
58
+ 2: Collected
42
59
Shutdown
43
- 0 : End destruct
60
+ 1 : End destruct
0 commit comments