1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/lib/Worker/WorkerThread.php

54 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2016-12-30 02:16:04 +01:00
<?php
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Worker;
2016-08-18 18:04:48 +02:00
use Amp\Parallel\Context\Thread;
use Amp\Parallel\Sync\Channel;
2017-05-18 09:51:31 +02:00
use Amp\Promise;
/**
* A worker thread that executes task objects.
*
* @deprecated ext-pthreads development has been halted, see https://github.com/krakjoe/pthreads/issues/929
*/
2018-10-27 01:29:45 +02:00
final class WorkerThread extends TaskWorker
2018-10-07 16:50:45 +02:00
{
/**
* @param string $envClassName Name of class implementing \Amp\Parallel\Worker\Environment to instigate.
* Defaults to \Amp\Parallel\Worker\BasicEnvironment.
* @param string|null $bootstrapPath Path to custom autoloader.
*/
public function __construct(string $envClassName = BasicEnvironment::class, string $bootstrapPath = null)
2018-10-07 16:50:45 +02:00
{
parent::__construct(new Thread(static function (Channel $channel, string $className, string $bootstrapPath = null): Promise {
if ($bootstrapPath !== null) {
if (!\is_file($bootstrapPath)) {
throw new \Error(\sprintf("No file found at bootstrap file path given '%s'", $bootstrapPath));
}
// Include file within unbound closure to protect scope.
(static function () use ($bootstrapPath): void {
require $bootstrapPath;
})->bindTo(null, null)();
}
if (!\class_exists($className)) {
throw new \Error(\sprintf("Invalid environment class name '%s'", $className));
}
if (!\is_subclass_of($className, Environment::class)) {
throw new \Error(\sprintf("The class '%s' does not implement '%s'", $className, Environment::class));
}
$environment = new $className;
if (!\defined("AMP_WORKER")) {
\define("AMP_WORKER", \AMP_CONTEXT);
}
$runner = new TaskRunner($channel, $environment);
2016-08-18 18:04:48 +02:00
return $runner->run();
}, $envClassName, $bootstrapPath));
}
}