1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 14:01:14 +01:00

Actually fix kill threads

This commit is contained in:
Aaron Piotrowski 2015-09-06 14:59:24 -05:00
parent b163207cea
commit 79f4adb26a
4 changed files with 22 additions and 18 deletions

View File

@ -68,6 +68,11 @@ class Fork implements ContextInterface
$this->channel = null;
}
public function __destruct()
{
$this->kill(); // Will only terminate if the process is still running.
}
/**
* Checks if the context is running.
*

View File

@ -83,7 +83,7 @@ class Thread extends \Thread
$coroutine = new Coroutine($this->execute($channel));
$coroutine->done();
$timer = $loop->timer(self::KILL_CHECK_FREQUENCY, true, function () use ($loop, $coroutine, $channel) {
$timer = $loop->timer(self::KILL_CHECK_FREQUENCY, true, function () use ($loop) {
if ($this->killed) {
$loop->stop();
}
@ -99,7 +99,7 @@ class Thread extends \Thread
public function kill()
{
$this->killed = true;
parent::kill();
return parent::kill();
}
/**

View File

@ -138,13 +138,23 @@ class Thread implements ContextInterface
*/
public function kill()
{
if (null === $this->thread) {
throw new StatusError('The thread has not been started.');
}
if (null !== $this->thread) {
$this->close();
$this->thread->kill();
if ($this->thread->isRunning() && !$this->thread->kill()) {
throw new ThreadException('Could not kill thread.');
}
}
}
/**
* Kills the thread if it is still running.
*
* @throws \Icicle\Concurrent\Exception\ThreadException
*/
public function __destruct()
{
$this->kill();
}
/**

View File

@ -4,7 +4,6 @@ namespace Icicle\Concurrent\Worker;
use Icicle\Concurrent\ContextInterface;
use Icicle\Concurrent\Exception\StatusError;
use Icicle\Concurrent\Worker\Internal\TaskFailure;
use Icicle\Coroutine\Coroutine;
class Worker implements WorkerInterface
{
@ -95,14 +94,4 @@ class Worker implements WorkerInterface
{
$this->context->kill();
}
/**
* Kills the worker when it is destroyed.
*/
public function __destruct()
{
if ($this->context->isRunning()) {
$this->context->kill();
}
}
}