/** * Inform the coroutine that the executing strand is being terminated. * * @param StrandInterface $strand The strand that is executing the coroutine. */ public function terminate(StrandInterface $strand) { if (!$this->timer) { // Stop termination of the strand and instead propagate a timeout exception. $strand->throwException(new TimeoutException()); } }
/** * Resume execution of a suspended coroutine by passing it an exception. * * @param StrandInterface $strand The strand that is executing the coroutine. * @param Exception $exception The exception to send to the coroutine. */ public function resumeWithException(StrandInterface $strand, Exception $exception) { try { $this->generator->throw($exception); $valid = $this->generator->valid(); } catch (Exception $e) { $strand->throwException($e); return; } if ($valid) { $strand->call($this->generator->current()); } elseif (self::$hasReturnValue) { $strand->returnValue($this->generator->getReturn()); } else { $strand->returnValue(null); } }
/** * Start the coroutine. * * @param StrandInterface $strand The strand that is executing the coroutine. */ public function call(StrandInterface $strand) { $method = [$strand->kernel()->api(), $this->name]; if (!is_callable($method)) { $strand->throwException(new BadMethodCallException('The kernel API does not have an operation named "' . $this->name . '".')); return; } $strand->pop(); $arguments = $this->arguments; array_unshift($arguments, $strand); // If the kernel API implementation returns a non-null value it is // treated as a coroutine to be executed. This allows implementation of // kernel API operations to be implemented as generators. $coroutine = call_user_func_array($method, $arguments); if (null !== $coroutine) { $strand->call($coroutine); } }
/** * Throw an exception to the calling coroutine. * * @param StrandInterface $strand The currently executing strand. * @param Exception $exception The error to send to the calling coroutine. */ public function throw_(StrandInterface $strand, Exception $exception) { $strand->throwException($exception); }
/** * Resume execution of a suspended coroutine by passing it an exception. * * @param StrandInterface $strand The strand that is executing the coroutine. * @param Exception $exception The exception to send to the coroutine. */ public function resumeWithException(StrandInterface $strand, Exception $exception) { $strand->throwException($exception); }