function = $function; $this->args = $args; $this->socket = $socket; if (self::$autoloadPath === null) { // Determine path to composer autoload.php $files = [ dirname(__DIR__, 3) . \DIRECTORY_SEPARATOR . "vendor" . \DIRECTORY_SEPARATOR . "autoload.php", dirname(__DIR__, 5) . \DIRECTORY_SEPARATOR . "autoload.php", ]; foreach ($files as $file) { if (in_array($file, \get_included_files())) { self::$autoloadPath = $file; return; } } throw new \Error("Could not locate autoload.php"); } } /** * Runs the thread code and the initialized function. * * @codeCoverageIgnore Only executed in thread. */ 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. */ // Protect scope by using an unbound closure (protects static access as well). $autoloadPath = self::$autoloadPath; (static function () use ($autoloadPath) { require $autoloadPath; })->bindTo(null, null)(); // At this point, the thread environment has been prepared so begin using the thread. if ($this->killed) { return; // Thread killed while requiring autoloader, simply exit. } if (!\is_resource($this->socket) || \feof($this->socket)) { return; // Parent context exited, no need to continue. } Loop::run(function () { $watcher = Loop::repeat(self::KILL_CHECK_FREQUENCY, function () { if ($this->killed) { Loop::stop(); } }); Loop::unreference($watcher); return $this->execute(new ChannelledSocket($this->socket, $this->socket)); }); } /** * Sets a local variable to true so the running event loop can check for a kill signal. */ public function kill() { return $this->killed = true; } /** * @coroutine * * @param \Amp\Parallel\Sync\Channel $channel * * @return \Generator * * @codeCoverageIgnore Only executed in thread. */ private function execute(Channel $channel): \Generator { try { if ($this->function instanceof \Closure) { $result = call($this->function->bindTo($channel, null), ...$this->args); } else { $result = call($this->function, ...$this->args); } $result = new ExitSuccess(yield $result); } catch (\Throwable $exception) { $result = new ExitFailure($exception); } if ($this->killed) { return; // Parent is not listening for a result. } // Attempt to return the result. try { try { yield $channel->send($result); } catch (SerializationException $exception) { // Serializing the result failed. Send the reason why. yield $channel->send(new ExitFailure($exception)); } } catch (ChannelException $exception) { // The result was not sendable! The parent context must have died or killed the context. } } }