1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00

Null thread reference at shutdown

This commit is contained in:
Aaron Piotrowski 2015-09-08 12:55:29 -05:00
parent 79f4adb26a
commit 88c6a79f7e

View File

@ -48,6 +48,11 @@ class Thread implements ContextInterface
*/
private $args;
/**
* @var bool
*/
private $started = false;
/**
* Spawns a new thread and runs it.
*
@ -116,10 +121,12 @@ class Thread implements ContextInterface
*/
public function start()
{
if (null !== $this->thread) {
if ($this->started) {
throw new StatusError('The thread has already been started.');
}
$this->started = true;
list($channel, $this->socket) = Socket\pair();
$this->thread = new Internal\Thread($this->socket, $this->function, $this->args);
@ -139,10 +146,12 @@ class Thread implements ContextInterface
public function kill()
{
if (null !== $this->thread) {
$this->close();
if ($this->thread->isRunning() && !$this->thread->kill()) {
throw new ThreadException('Could not kill thread.');
try {
if ($this->thread->isRunning() && !$this->thread->kill()) {
throw new ThreadException('Could not kill thread.');
}
} finally {
$this->close();
}
}
}
@ -169,6 +178,8 @@ class Thread implements ContextInterface
if (is_resource($this->socket)) {
fclose($this->socket);
}
$this->thread = null;
}
/**
@ -186,8 +197,8 @@ class Thread implements ContextInterface
*/
public function join()
{
if (null === $this->thread) {
throw new StatusError('The thread has not been started.');
if (null === $this->channel || null === $this->thread) {
throw new StatusError('The thread has not been started or has already finished.');
}
try {
@ -213,8 +224,8 @@ class Thread implements ContextInterface
*/
public function receive()
{
if (null === $this->thread) {
throw new StatusError('The thread has not been started.');
if (null === $this->channel) {
throw new StatusError('The thread has not been started or has already finished.');
}
$data = (yield $this->channel->receive());
@ -236,8 +247,8 @@ class Thread implements ContextInterface
*/
public function send($data)
{
if (null === $this->thread) {
throw new StatusError('The thread has not been started.');
if (null === $this->channel) {
throw new StatusError('The thread has not been started or has already finished.');
}
if ($data instanceof ExitStatusInterface) {