1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 18:17:52 +01:00
parallel/lib/Threading/Internal/Thread.php

161 lines
4.6 KiB
PHP
Raw Normal View History

2016-08-22 06:40:48 +02:00
<?php declare(strict_types = 1);
2016-08-18 18:04:48 +02:00
namespace Amp\Concurrent\Threading\Internal;
use Amp\Concurrent\{ChannelException, SerializationException};
use Amp\Concurrent\Sync\{Channel, ChannelledStream, Internal\ExitFailure, Internal\ExitSuccess};
use Amp\Coroutine;
use Amp\Socket\Socket;
use Interop\Async\Awaitable;
/**
* An internal thread that executes a given function concurrently.
*
* @internal
*/
2016-08-18 18:04:48 +02:00
class Thread extends \Thread {
const KILL_CHECK_FREQUENCY = 250;
/**
* @var callable The function to execute in the thread.
*/
private $function;
/**
* @var mixed[] Arguments to pass to the function.
*/
private $args;
/**
* @var resource
*/
private $socket;
/**
* @var bool
*/
private $killed = false;
/**
* 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.
*/
2016-08-18 18:04:48 +02:00
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.
*
* @codeCoverageIgnore Only executed in thread.
*/
2016-08-18 18:04:48 +02:00
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.
*/
2016-08-18 18:04:48 +02:00
$paths = [
\dirname(__DIR__, 5) . \DIRECTORY_SEPARATOR . 'autoload.php',
\dirname(__DIR__, 3) . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php',
];
$autoloadPath = null;
foreach ($paths as $path) {
if (\file_exists($path)) {
$autoloadPath = $path;
break;
}
}
2016-08-18 18:04:48 +02:00
if (null === $autoloadPath) {
2016-08-19 00:36:58 +02:00
throw new \Error('Could not locate autoload.php');
2016-08-18 18:04:48 +02:00
}
require $autoloadPath;
// At this point, the thread environment has been prepared so begin using the thread.
try {
2016-08-18 18:04:48 +02:00
\Amp\execute(function () {
2016-08-19 00:36:58 +02:00
$channel = new ChannelledStream(new Socket($this->socket, false));
2016-08-18 18:04:48 +02:00
$watcher = \Amp\repeat(self::KILL_CHECK_FREQUENCY, function () {
if ($this->killed) {
\Amp\stop();
}
});
\Amp\unreference($watcher);
return new Coroutine($this->execute($channel));
});
2016-01-27 08:03:37 +01:00
} catch (\Throwable $exception) {
2016-08-18 18:04:48 +02:00
return 1;
}
2016-08-18 18:04:48 +02:00
return 0;
}
/**
* Sets a local variable to true so the running event loop can check for a kill signal.
*/
2016-08-18 18:04:48 +02:00
public function kill() {
return $this->killed = true;
}
/**
* @coroutine
*
2016-08-18 18:04:48 +02:00
* @param \Amp\Concurrent\Sync\Channel $channel
*
* @return \Generator
*
* @codeCoverageIgnore Only executed in thread.
*/
2016-08-18 18:04:48 +02:00
private function execute(Channel $channel): \Generator {
try {
2015-08-27 21:49:41 +02:00
if ($this->function instanceof \Closure) {
$function = $this->function->bindTo($channel, Channel::class);
2015-08-27 21:49:41 +02:00
}
if (empty($function)) {
$function = $this->function;
}
2016-08-18 18:04:48 +02:00
$result = $function(...$this->args);
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Awaitable) {
$result = yield $result;
}
2016-08-18 18:04:48 +02:00
$result = new ExitSuccess($result);
2016-01-23 07:00:56 +01:00
} catch (\Throwable $exception) {
$result = new ExitFailure($exception);
}
2015-12-12 07:34:41 +01:00
// Attempt to return the result.
try {
2016-01-23 18:20:58 +01:00
try {
2016-08-18 18:04:48 +02:00
return yield $channel->send($result);
2016-01-23 18:20:58 +01:00
} catch (SerializationException $exception) {
// Serializing the result failed. Send the reason why.
2016-08-18 18:04:48 +02:00
return yield $channel->send(new ExitFailure($exception));
2016-01-23 18:20:58 +01:00
}
} catch (ChannelException $exception) {
// The result was not sendable! The parent context must have died or killed the context.
2016-01-23 18:29:22 +01:00
return 0;
2015-12-12 07:34:41 +01:00
}
}
}