2015-07-14 00:30:59 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Concurrent\Threading;
|
|
|
|
|
2015-08-27 16:10:08 +02:00
|
|
|
use Icicle\Concurrent\ContextInterface;
|
2015-08-22 23:27:44 +02:00
|
|
|
use Icicle\Concurrent\Exception\InvalidArgumentError;
|
2015-08-27 20:06:39 +02:00
|
|
|
use Icicle\Concurrent\Exception\StatusError;
|
2015-08-22 23:27:44 +02:00
|
|
|
use Icicle\Concurrent\Exception\SynchronizationError;
|
2015-08-28 23:18:02 +02:00
|
|
|
use Icicle\Concurrent\Exception\ThreadException;
|
2015-08-05 09:48:43 +02:00
|
|
|
use Icicle\Concurrent\Sync\Channel;
|
2015-08-22 23:27:44 +02:00
|
|
|
use Icicle\Concurrent\Sync\ExitStatusInterface;
|
2015-08-27 20:06:39 +02:00
|
|
|
use Icicle\Concurrent\SynchronizableInterface;
|
2015-08-22 23:27:44 +02:00
|
|
|
use Icicle\Coroutine;
|
2015-08-25 16:37:22 +02:00
|
|
|
use Icicle\Socket\Stream\DuplexStream;
|
2015-07-27 00:53:00 +02:00
|
|
|
|
2015-07-15 19:36:32 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* Implements an execution context using native multi-threading.
|
2015-08-11 00:38:58 +02:00
|
|
|
*
|
2015-08-22 23:27:44 +02:00
|
|
|
* The thread context is not itself threaded. A local instance of the context is
|
|
|
|
* maintained both in the context that creates the thread and in the thread
|
|
|
|
* itself.
|
2015-07-15 19:36:32 +02:00
|
|
|
*/
|
2015-08-27 20:06:39 +02:00
|
|
|
class Thread implements ContextInterface, SynchronizableInterface
|
2015-07-14 00:30:59 +02:00
|
|
|
{
|
2015-08-25 16:39:58 +02:00
|
|
|
const LATENCY_TIMEOUT = 0.01; // 10 ms
|
|
|
|
|
2015-07-27 00:53:00 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* @var \Icicle\Concurrent\Threading\InternalThread An internal thread instance.
|
2015-07-27 00:53:00 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
private $thread;
|
2015-07-27 00:53:00 +02:00
|
|
|
|
2015-08-07 06:25:04 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* @var \Icicle\Concurrent\Sync\Channel A channel for communicating with the thread.
|
2015-08-07 06:25:04 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
private $channel;
|
2015-08-07 06:25:04 +02:00
|
|
|
|
2015-08-25 02:35:42 +02:00
|
|
|
/**
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
private $socket;
|
|
|
|
|
2015-08-27 20:06:39 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $started = false;
|
|
|
|
|
2015-08-07 01:59:25 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* Spawns a new thread and runs it.
|
|
|
|
*
|
|
|
|
* @param callable $function A callable to invoke in the thread.
|
|
|
|
*
|
2015-08-25 02:35:42 +02:00
|
|
|
* @return \Icicle\Concurrent\Threading\Thread The thread object that was spawned.
|
2015-08-07 01:59:25 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
public static function spawn(callable $function /* , ...$args */)
|
|
|
|
{
|
2015-08-24 17:47:36 +02:00
|
|
|
$class = new \ReflectionClass(__CLASS__);
|
|
|
|
$thread = $class->newInstanceArgs(func_get_args());
|
2015-08-22 23:27:44 +02:00
|
|
|
$thread->start();
|
|
|
|
return $thread;
|
|
|
|
}
|
2015-08-05 09:48:43 +02:00
|
|
|
|
2015-08-18 17:12:06 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* Creates a new thread context from a thread.
|
|
|
|
*
|
|
|
|
* @param callable $function
|
2015-08-18 17:12:06 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
public function __construct(callable $function /* , ...$args */)
|
|
|
|
{
|
|
|
|
$args = array_slice(func_get_args(), 1);
|
|
|
|
|
2015-08-27 22:32:57 +02:00
|
|
|
// Make sure closures don't `use` other variables or have statics.
|
|
|
|
if ($function instanceof \Closure) {
|
|
|
|
$reflector = new \ReflectionFunction($function);
|
|
|
|
if (!empty($reflector->getStaticVariables())) {
|
|
|
|
throw new InvalidArgumentError('Closures with static variables cannot be passed to thread.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 02:35:42 +02:00
|
|
|
list($channel, $this->socket) = Channel::createSocketPair();
|
2015-08-22 23:27:44 +02:00
|
|
|
|
2015-08-25 02:35:42 +02:00
|
|
|
$this->thread = new InternalThread($this->socket, $function, $args);
|
2015-08-25 16:37:22 +02:00
|
|
|
$this->channel = new Channel(new DuplexStream($channel));
|
2015-08-22 23:27:44 +02:00
|
|
|
}
|
2015-08-18 17:12:06 +02:00
|
|
|
|
2015-07-27 00:53:00 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* Checks if the context is running.
|
2015-07-27 00:53:00 +02:00
|
|
|
*
|
2015-08-22 23:27:44 +02:00
|
|
|
* @return bool True if the context is running, otherwise false.
|
2015-07-27 00:53:00 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
public function isRunning()
|
2015-07-15 00:15:10 +02:00
|
|
|
{
|
2015-08-28 23:18:02 +02:00
|
|
|
return $this->thread->isRunning() && !$this->thread->isTerminated();
|
2015-08-07 01:59:25 +02:00
|
|
|
}
|
|
|
|
|
2015-08-05 09:48:43 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* Starts the context execution.
|
2015-08-05 09:48:43 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
public function start()
|
2015-07-14 00:30:59 +02:00
|
|
|
{
|
2015-08-27 20:06:39 +02:00
|
|
|
if ($this->started) {
|
|
|
|
throw new StatusError('The thread has already been started.');
|
2015-08-05 09:48:43 +02:00
|
|
|
}
|
|
|
|
|
2015-08-27 04:34:41 +02:00
|
|
|
$this->thread->start(PTHREADS_INHERIT_INI | PTHREADS_INHERIT_FUNCTIONS | PTHREADS_INHERIT_CLASSES);
|
2015-08-27 20:06:39 +02:00
|
|
|
|
|
|
|
$this->started = true;
|
2015-08-22 23:27:44 +02:00
|
|
|
}
|
2015-08-11 00:38:58 +02:00
|
|
|
|
2015-08-22 23:27:44 +02:00
|
|
|
/**
|
|
|
|
* Immediately kills the context.
|
|
|
|
*/
|
|
|
|
public function kill()
|
|
|
|
{
|
2015-08-28 23:18:02 +02:00
|
|
|
if (!$this->thread->kill()) {
|
|
|
|
throw new ThreadException('Failed to kill the thread.');
|
|
|
|
}
|
|
|
|
|
2015-08-25 16:53:17 +02:00
|
|
|
$this->channel->close();
|
2015-08-25 16:37:22 +02:00
|
|
|
fclose($this->socket);
|
2015-08-05 09:48:43 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:12:06 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* @coroutine
|
|
|
|
*
|
|
|
|
* Gets a promise that resolves when the context ends and joins with the
|
|
|
|
* parent context.
|
2015-08-18 17:12:06 +02:00
|
|
|
*
|
2015-08-22 23:27:44 +02:00
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @resolve mixed Resolved with the return or resolution value of the context once it has completed execution.
|
2015-08-25 02:35:42 +02:00
|
|
|
*
|
2015-08-27 22:32:57 +02:00
|
|
|
* @throws \Icicle\Concurrent\Exception\StatusError Thrown if the context has not been started.
|
2015-08-25 02:35:42 +02:00
|
|
|
* @throws \Icicle\Concurrent\Exception\SynchronizationError Thrown if an exit status object is not received.
|
2015-08-18 17:12:06 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
public function join()
|
2015-08-18 17:12:06 +02:00
|
|
|
{
|
2015-08-27 20:06:39 +02:00
|
|
|
if (!$this->started) {
|
|
|
|
throw new StatusError('The context has not been started.');
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:12:06 +02:00
|
|
|
try {
|
2015-08-22 23:27:44 +02:00
|
|
|
$response = (yield $this->channel->receive());
|
|
|
|
|
|
|
|
if (!$response instanceof ExitStatusInterface) {
|
|
|
|
throw new SynchronizationError('Did not receive an exit status from thread.');
|
2015-08-18 17:12:06 +02:00
|
|
|
}
|
2015-08-22 23:27:44 +02:00
|
|
|
|
|
|
|
yield $response->getResult();
|
2015-08-18 17:12:06 +02:00
|
|
|
} finally {
|
2015-08-22 23:27:44 +02:00
|
|
|
$this->thread->join();
|
2015-08-25 02:35:42 +02:00
|
|
|
$this->channel->close();
|
|
|
|
fclose($this->socket);
|
2015-08-18 17:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* {@inheritdoc}
|
2015-08-18 17:12:06 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
public function receive()
|
2015-08-18 17:12:06 +02:00
|
|
|
{
|
2015-08-27 20:06:39 +02:00
|
|
|
if (!$this->started) {
|
|
|
|
throw new StatusError('The context has not been started.');
|
|
|
|
}
|
|
|
|
|
2015-08-22 23:27:44 +02:00
|
|
|
$data = (yield $this->channel->receive());
|
|
|
|
|
|
|
|
if ($data instanceof ExitStatusInterface) {
|
|
|
|
$data = $data->getResult();
|
|
|
|
throw new SynchronizationError(sprintf(
|
|
|
|
'Thread unexpectedly exited with result of type: %s',
|
|
|
|
is_object($data) ? get_class($data) : gettype($data)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
yield $data;
|
2015-08-18 17:12:06 +02:00
|
|
|
}
|
|
|
|
|
2015-08-07 01:59:25 +02:00
|
|
|
/**
|
2015-08-22 23:27:44 +02:00
|
|
|
* {@inheritdoc}
|
2015-08-07 01:59:25 +02:00
|
|
|
*/
|
2015-08-22 23:27:44 +02:00
|
|
|
public function send($data)
|
2015-08-05 09:48:43 +02:00
|
|
|
{
|
2015-08-27 20:06:39 +02:00
|
|
|
if (!$this->started) {
|
|
|
|
throw new StatusError('The context has not been started.');
|
|
|
|
}
|
|
|
|
|
2015-08-22 23:27:44 +02:00
|
|
|
if ($data instanceof ExitStatusInterface) {
|
|
|
|
throw new InvalidArgumentError('Cannot send exit status objects.');
|
2015-07-27 00:53:00 +02:00
|
|
|
}
|
2015-07-14 00:30:59 +02:00
|
|
|
|
2015-08-27 20:06:39 +02:00
|
|
|
yield $this->channel->send($data);
|
2015-08-22 23:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-27 20:06:39 +02:00
|
|
|
* {@inheritdoc}
|
2015-08-22 23:27:44 +02:00
|
|
|
*/
|
2015-08-25 16:53:17 +02:00
|
|
|
public function synchronized(callable $callback)
|
2015-08-22 23:27:44 +02:00
|
|
|
{
|
2015-08-27 20:06:39 +02:00
|
|
|
if (!$this->started) {
|
|
|
|
throw new StatusError('The context has not been started.');
|
|
|
|
}
|
|
|
|
|
2015-08-22 23:27:44 +02:00
|
|
|
while (!$this->thread->tsl()) {
|
2015-08-25 16:39:58 +02:00
|
|
|
yield Coroutine\sleep(self::LATENCY_TIMEOUT);
|
2015-08-19 02:12:58 +02:00
|
|
|
}
|
2015-08-22 23:27:44 +02:00
|
|
|
|
2015-08-25 16:53:17 +02:00
|
|
|
try {
|
|
|
|
yield $callback($this);
|
|
|
|
} finally {
|
2015-08-22 23:27:44 +02:00
|
|
|
$this->thread->release();
|
2015-08-25 16:53:17 +02:00
|
|
|
}
|
2015-08-05 09:48:43 +02:00
|
|
|
}
|
2015-07-14 00:30:59 +02:00
|
|
|
}
|