function = $function; $this->args = $args; $this->socket = $socket; } /** * 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. */ foreach (get_declared_classes() as $className) { if (strpos($className, 'ComposerAutoloaderInit') === 0) { // Calling getLoader() will register the class loader for us $className::getLoader(); break; } } Loop\loop($loop = Loop\create(false)); // Disable signals in thread. // At this point, the thread environment has been prepared so begin using the thread. $channel = new ChannelledStream(new DuplexPipe($this->socket, false)); $coroutine = new Coroutine($this->execute($channel)); $coroutine->done(); $timer = $loop->timer(self::KILL_CHECK_FREQUENCY, true, function () use ($loop) { if ($this->killed) { $loop->stop(); } }); $timer->unreference(); $loop->run(); } /** * Sets a local variable to true so the running event loop can check for a kill signal. */ public function kill() { $this->killed = true; return parent::kill(); } /** * @coroutine * * @param \Icicle\Concurrent\Sync\Channel $channel * * @return \Generator * * @resolve int * * @codeCoverageIgnore Only executed in thread. */ private function execute(Channel $channel) { try { if ($this->function instanceof \Closure) { $function = $this->function->bindTo($channel, Channel::class); } if (empty($function)) { $function = $this->function; } $result = new ExitSuccess(yield call_user_func_array($function, $this->args)); } catch (\Exception $exception) { $result = new ExitFailure($exception); } // 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. yield 0; } } }