2015-08-24 17:40:36 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Concurrent\Threading;
|
|
|
|
|
|
|
|
use Icicle\Concurrent\Sync\Channel;
|
|
|
|
use Icicle\Concurrent\Sync\ChannelInterface;
|
|
|
|
use Icicle\Concurrent\Sync\ExitFailure;
|
|
|
|
use Icicle\Concurrent\Sync\ExitSuccess;
|
|
|
|
use Icicle\Coroutine\Coroutine;
|
|
|
|
use Icicle\Loop;
|
2015-08-25 16:37:22 +02:00
|
|
|
use Icicle\Socket\Stream\DuplexStream;
|
2015-08-24 17:40:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An internal thread that executes a given function concurrently.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class InternalThread extends \Thread
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var callable The function to execute in the thread.
|
|
|
|
*/
|
|
|
|
private $function;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private $args;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
private $socket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $lock = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new thread object.
|
|
|
|
*
|
|
|
|
* @param resource $socket IPC communication socket.
|
|
|
|
* @param callable $function The function to execute in the thread.
|
|
|
|
* @param mixed[] $args Arguments to pass to the function.
|
|
|
|
*/
|
|
|
|
public function __construct($socket, callable $function, array $args = [])
|
|
|
|
{
|
|
|
|
$this->function = $function;
|
|
|
|
$this->args = $args;
|
|
|
|
$this->socket = $socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the thread code and the initialized function.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the thread environment has been prepared so begin using the thread.
|
2015-08-25 16:37:22 +02:00
|
|
|
$channel = new Channel(new DuplexStream($this->socket));
|
2015-08-24 17:40:36 +02:00
|
|
|
|
|
|
|
$coroutine = new Coroutine($this->execute($channel));
|
|
|
|
$coroutine->done();
|
|
|
|
|
2015-08-27 04:57:21 +02:00
|
|
|
Loop\run();
|
2015-08-24 17:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to obtain the lock. Returns true if the lock was obtained.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function tsl()
|
|
|
|
{
|
|
|
|
if (!$this->lock) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 21:34:50 +02:00
|
|
|
return $this->synchronized(function () {
|
2015-08-24 17:40:36 +02:00
|
|
|
if ($this->lock) {
|
|
|
|
$this->lock = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-08-27 21:34:50 +02:00
|
|
|
});
|
2015-08-24 17:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the lock.
|
|
|
|
*/
|
|
|
|
public function release()
|
|
|
|
{
|
|
|
|
$this->lock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @coroutine
|
|
|
|
*
|
|
|
|
* @param \Icicle\Concurrent\Sync\ChannelInterface $channel
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @resolve int
|
|
|
|
*/
|
|
|
|
private function execute(ChannelInterface $channel)
|
|
|
|
{
|
|
|
|
$executor = new ThreadExecutor($this, $channel);
|
|
|
|
|
|
|
|
try {
|
2015-08-27 21:49:41 +02:00
|
|
|
if ($this->function instanceof \Closure) {
|
|
|
|
$function = $this->function->bindTo($executor, ThreadExecutor::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($function)) {
|
|
|
|
$function = $this->function;
|
2015-08-24 17:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = new ExitSuccess(yield call_user_func_array($function, $this->args));
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$result = new ExitFailure($exception);
|
|
|
|
}
|
|
|
|
|
2015-08-25 02:35:42 +02:00
|
|
|
yield $channel->send($result);
|
2015-08-24 17:40:36 +02:00
|
|
|
}
|
|
|
|
}
|