1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 10:07:49 +01:00
parallel/src/Threading/Internal/Thread.php

107 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Concurrent\Threading\Internal;
use Icicle\Concurrent\Sync\Channel;
use Icicle\Concurrent\Sync\ChannelInterface;
use Icicle\Concurrent\Sync\Internal\ExitFailure;
use Icicle\Concurrent\Sync\Internal\ExitSuccess;
use Icicle\Concurrent\Threading\Executor;
use Icicle\Coroutine\Coroutine;
use Icicle\Loop;
2015-08-25 16:37:22 +02:00
use Icicle\Socket\Stream\DuplexStream;
/**
* An internal thread that executes a given function concurrently.
*
* @internal
*/
class Thread extends \Thread
{
/**
* @var callable The function to execute in the thread.
*/
private $function;
/**
* @var
*/
private $args;
/**
* @var resource
*/
private $socket;
/**
* Creates a new thread object.
*
* @param resource $socket IPC communication socket.
* @param callable $function The function to execute in the thread.
* @param mixed[] $args Arguments to pass to the function.
*/
public function __construct($socket, callable $function, array $args = [])
{
$this->function = $function;
$this->args = $args;
$this->socket = $socket;
}
/**
* Runs the thread code and the initialized function.
*/
public function run()
{
/* First thing we need to do is re-initialize the class autoloader. If
* we don't do this first, any object of a class that was loaded after
* the thread started will just be garbage data and unserializable
* values (like resources) will be lost. This happens even with
* thread-safe objects.
*/
foreach (get_declared_classes() as $className) {
if (strpos($className, 'ComposerAutoloaderInit') === 0) {
// Calling getLoader() will register the class loader for us
$className::getLoader();
break;
}
}
// At this point, the thread environment has been prepared so begin using the thread.
2015-08-25 16:37:22 +02:00
$channel = new Channel(new DuplexStream($this->socket));
$coroutine = new Coroutine($this->execute($channel));
$coroutine->done();
Loop\run();
}
/**
* @coroutine
*
* @param \Icicle\Concurrent\Sync\ChannelInterface $channel
*
* @return \Generator
*
* @resolve int
*/
private function execute(ChannelInterface $channel)
{
$executor = new Executor($this, $channel);
try {
2015-08-27 21:49:41 +02:00
if ($this->function instanceof \Closure) {
$function = $this->function->bindTo($executor, Executor::class);
2015-08-27 21:49:41 +02:00
}
if (empty($function)) {
$function = $this->function;
}
$result = new ExitSuccess(yield call_user_func_array($function, $this->args));
} catch (\Exception $exception) {
$result = new ExitFailure($exception);
}
yield $channel->send($result);
}
}