Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
fix fault when shutdown called before start on Thread or Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
krakjoe committed Nov 20, 2015
1 parent 7e210cf commit a8c1011
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
11 changes: 4 additions & 7 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@
<date>2015-11-20</date>
<time>11:00:00</time>
<version>
<release>3.1.0</release>
<api>3.1.0</api>
<release>3.1.1</release>
<api>3.1.1</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
Initial development of v3 is complete, this release is the first supported
release of pthreads v3 for PHP7.
Breaking changes for Collectables in this release.
SemVer will be (roughly) followed from this release onward.
Everyone on PHP7 must upgrade!
Fix fault when ::shutdown is called on Thread preceeding ::start
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -128,6 +124,7 @@
<file name="tests/workers-default-collector.phpt" role="test" />
<file name="tests/workers-no-collect.phpt" role="test" />
<file name="tests/workers-no-join.phpt" role="test" />
<file name="tests/workers-no-shutdown-before-start.phpt" role="test" />
<file name="tests/workers-no-stack.phpt" role="test" />
<file name="tests/workers-no-start.phpt" role="test" />
<file name="tests/workers-no-unstack.phpt" role="test" />
Expand Down
7 changes: 7 additions & 0 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ zend_bool pthreads_join(pthreads_object_t* thread) {
return 0;
}

if (!pthreads_monitor_check(thread->monitor, PTHREADS_MONITOR_STARTED)) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
"%s has not been started",
thread->std.ce->name->val);
return 0;
}

pthreads_monitor_add(thread->monitor, PTHREADS_MONITOR_JOINED);

return (pthread_join(thread->thread, NULL) == SUCCESS);
Expand Down
22 changes: 22 additions & 0 deletions tests/workers-no-shutdown-before-start.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test pthreads Worker::shutdown
--DESCRIPTION--
This test verifies shutdown of a Worker (or Thread) not yet started doesn't fault
--FILE--
<?php
$worker = new Worker();

$worker->stack(new class extends Threaded {
public function run() {
var_dump($this);
}
});

$worker->shutdown();
?>
--EXPECTF--
Fatal error: Uncaught RuntimeException: Worker has not been started in %s:10
Stack trace:
#0 %s(10): Worker->shutdown()
#1 {main}
thrown in %s on line 10
3 changes: 0 additions & 3 deletions tests/workers-ok-unstack.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Test pthreads Worker::unstack
This test verifies that unstack functions as intended
--FILE--
<?php

$worker = new Worker();

$worker->stack(new class extends Threaded {
Expand All @@ -14,8 +13,6 @@ $worker->stack(new class extends Threaded {
});

var_dump($worker->unstack());

$worker->shutdown();
?>
--EXPECTF--
object(class@anonymous)#%d (%d) {
Expand Down

0 comments on commit a8c1011

Please sign in to comment.