Skip to content

Commit 75d1359

Browse files
Crash less and allow more cyclomatic refs (#339)
1 parent 58376ca commit 75d1359

File tree

8 files changed

+132
-8
lines changed

8 files changed

+132
-8
lines changed

src/cache.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,12 @@ zend_function* php_parallel_cache_closure(const zend_function *source, zend_func
370370
memcpy(closure, cache, sizeof(zend_op_array));
371371
}
372372

373-
if (closure->op_array.static_variables) {
373+
if (source->op_array.static_variables) {
374374
HashTable *statics =
375375
ZEND_MAP_PTR_GET(
376376
source->op_array.static_variables_ptr);
377377

378+
if (statics) {
378379
closure->op_array.static_variables =
379380
php_parallel_copy_hash_ctor(statics, 1);
380381

@@ -387,8 +388,22 @@ zend_function* php_parallel_cache_closure(const zend_function *source, zend_func
387388
closure->op_array.static_variables_ptr,
388389
&closure->op_array.static_variables);
389390
#endif
391+
}
390392
}
391393

394+
#if PHP_VERSION_ID >= 80100
395+
if (source->op_array.num_dynamic_func_defs) {
396+
uint32_t it = 0;
397+
closure->op_array.dynamic_func_defs = php_parallel_cache_copy_mem(
398+
source->op_array.dynamic_func_defs,
399+
sizeof(zend_op_array*) * source->op_array.num_dynamic_func_defs);
400+
while (it < source->op_array.num_dynamic_func_defs) {
401+
closure->op_array.dynamic_func_defs[it] = (zend_op_array*) php_parallel_cache_closure((zend_function*) source->op_array.dynamic_func_defs[it], NULL);
402+
it++;
403+
}
404+
}
405+
#endif
406+
392407
return closure;
393408
} /* }}} */
394409

src/scheduler.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ static zend_always_inline int php_parallel_scheduler_list_delete(void *lhs, void
3333
static void php_parallel_schedule_free_function(zend_function *function) {
3434
if (function->op_array.static_variables) {
3535
php_parallel_copy_hash_dtor(function->op_array.static_variables, 1);
36+
ZEND_MAP_PTR_SET(function->op_array.static_variables_ptr, NULL);
37+
function->op_array.static_variables = NULL;
3638
}
3739

3840
#if PHP_VERSION_ID >= 80100
@@ -245,6 +247,8 @@ static void php_parallel_scheduler_clean(zend_function *function) {
245247

246248
if (!(GC_FLAGS(statics) & IS_ARRAY_IMMUTABLE)) {
247249
zend_array_destroy(statics);
250+
ZEND_MAP_PTR_SET(function->op_array.static_variables_ptr, NULL);
251+
function->op_array.static_variables = NULL;
248252
}
249253
}
250254

@@ -254,8 +258,9 @@ static void php_parallel_scheduler_clean(zend_function *function) {
254258

255259
while (it < function->op_array.num_dynamic_func_defs) {
256260
php_parallel_scheduler_clean(
257-
(zend_function*) function->op_array.dynamic_func_defs[it]);
258-
it++;
261+
(zend_function*) function->op_array.dynamic_func_defs[it]);
262+
pefree(function->op_array.dynamic_func_defs[it],1);
263+
it++;
259264
}
260265
}
261266
#endif

tests/base/045.phpt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,3 @@ bool(true)
2828
bool(true)
2929
--XLEAK--
3030
The interrupt we use for cancellation is not treated in a thread safe way in core
31-
32-
33-

tests/base/068.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ array(1) {
2020
["self"]=>
2121
*RECURSION*
2222
}
23-
--XFAIL--
23+
--XLEAK--
2424
REASON: no cyclic reference collector implemented yet

tests/base/069.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ object(Foo)#2 (1) {
2424
["foo"]=>
2525
*RECURSION*
2626
}
27-
--XFAIL--
27+
--XLEAK--
2828
REASON: no cyclic reference collector implemented yet

tests/base/071.phpt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
parallel recursion in arrays in the closure
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('parallel')) {
6+
die("skip parallel not loaded");
7+
}
8+
if (!version_compare(PHP_VERSION, "8.1", ">=")) {
9+
die("skip php 8.1 required");
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
function transformChunk($n)
15+
{
16+
$fibonacci = function ($n) use (&$fibonacci) {
17+
if ($n == 0) {
18+
return 0;
19+
}
20+
if ($n == 1) {
21+
return 1;
22+
}
23+
return $fibonacci($n - 1) + $fibonacci($n - 2);
24+
};
25+
return $fibonacci($n);
26+
}
27+
28+
$runtime = new \parallel\Runtime();
29+
$future = $runtime->run(
30+
transformChunk(...),
31+
[
32+
10
33+
]
34+
);
35+
var_dump($future->value());
36+
37+
$runtime = new \parallel\Runtime();
38+
$future = $runtime->run(
39+
transformChunk(...),
40+
[
41+
10
42+
]
43+
);
44+
var_dump($future->value());
45+
46+
?>
47+
--EXPECT--
48+
int(55)
49+
int(55)
50+
--XFAIL--
51+
REASON: no cyclic reference collector implemented yet

tests/base/072-bootstrap.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
function transformChunk($n)
3+
{
4+
$fibonacci = function ($n) use (&$fibonacci) {
5+
if ($n == 0) {
6+
return 0;
7+
}
8+
if ($n == 1) {
9+
return 1;
10+
}
11+
return $fibonacci($n - 1) + $fibonacci($n - 2);
12+
};
13+
return $fibonacci($n);
14+
}

tests/base/072.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
parallel recursion in arrays in the closure
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('parallel')) {
6+
die("skip parallel not loaded");
7+
}
8+
if (!version_compare(PHP_VERSION, "8.1", ">=")) {
9+
die("skip php 8.1 required");
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
15+
$app = function ($n) {
16+
return transformChunk($n);
17+
};
18+
19+
$runtime = new \parallel\Runtime(__DIR__.'/072-bootstrap.php');
20+
$future = $runtime->run(
21+
$app,
22+
[
23+
10
24+
]
25+
);
26+
var_dump($future->value());
27+
28+
$runtime = new \parallel\Runtime(__DIR__.'/072-bootstrap.php');
29+
$future = $runtime->run(
30+
$app,
31+
[
32+
10
33+
]
34+
);
35+
var_dump($future->value());
36+
37+
?>
38+
--EXPECT--
39+
int(55)
40+
int(55)
41+
--XFAIL--
42+
REASON: no cyclic reference collector implemented yet

0 commit comments

Comments
 (0)