2015-07-09 01:35:34 -05:00
|
|
|
<?php
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
|
|
|
|
|
|
use Icicle\Concurrent\Forking\ForkContext;
|
2015-07-12 15:57:04 -05:00
|
|
|
use Icicle\Coroutine\Coroutine;
|
|
|
|
use Icicle\Loop;
|
2015-07-09 01:35:34 -05:00
|
|
|
|
2015-07-09 13:13:20 -05:00
|
|
|
class Test extends ForkContext
|
|
|
|
{
|
2015-07-12 15:57:04 -05:00
|
|
|
/**
|
|
|
|
* @synchronized
|
|
|
|
*/
|
|
|
|
public $data;
|
|
|
|
|
2015-07-09 13:13:20 -05:00
|
|
|
public function run()
|
|
|
|
{
|
2015-07-13 12:30:00 -05:00
|
|
|
print "Child sleeping for 4 seconds...\n";
|
|
|
|
yield $this->lock();
|
2015-07-12 15:57:04 -05:00
|
|
|
sleep(4);
|
2015-07-13 12:30:00 -05:00
|
|
|
yield $this->unlock();
|
2015-07-10 02:23:08 -05:00
|
|
|
|
2015-07-13 12:30:00 -05:00
|
|
|
yield $this->synchronized(function () {
|
2015-07-10 02:23:08 -05:00
|
|
|
$this->data = 'progress';
|
|
|
|
});
|
|
|
|
|
2015-07-13 12:30:00 -05:00
|
|
|
print "Child sleeping for 2 seconds...\n";
|
2015-07-10 02:23:08 -05:00
|
|
|
sleep(2);
|
2015-07-09 13:13:20 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-09 01:35:34 -05:00
|
|
|
|
2015-07-12 15:57:04 -05:00
|
|
|
$generator = function () {
|
|
|
|
$context = new Test();
|
|
|
|
$context->data = 'blank';
|
|
|
|
$context->start();
|
2015-07-09 01:35:34 -05:00
|
|
|
|
2015-07-12 15:57:04 -05:00
|
|
|
Loop\timer(1, function () use ($context) {
|
2015-07-13 12:30:00 -05:00
|
|
|
$context->synchronized(function ($context) {
|
|
|
|
print "Finally got lock from child!\n";
|
2015-07-12 15:57:04 -05:00
|
|
|
});
|
|
|
|
});
|
2015-07-10 02:23:08 -05:00
|
|
|
|
2015-07-12 15:57:04 -05:00
|
|
|
$timer = Loop\periodic(1, function () use ($context) {
|
|
|
|
static $i;
|
|
|
|
$i = $i + 1 ?: 1;
|
|
|
|
print "Demonstrating how alive the parent is for the {$i}th time.\n";
|
|
|
|
|
|
|
|
if ($context->isRunning()) {
|
2015-07-13 12:30:00 -05:00
|
|
|
printf("Context data: '%s'\n", $context->data);
|
2015-07-12 15:57:04 -05:00
|
|
|
}
|
2015-07-10 02:23:08 -05:00
|
|
|
});
|
2015-07-09 01:35:34 -05:00
|
|
|
|
2015-07-12 15:57:04 -05:00
|
|
|
try {
|
|
|
|
yield $context->join();
|
|
|
|
print "Context done!\n";
|
|
|
|
} catch (Exception $e) {
|
|
|
|
print "Error from child!\n";
|
|
|
|
print $e."\n";
|
|
|
|
} finally {
|
|
|
|
$timer->stop();
|
|
|
|
}
|
|
|
|
};
|
2015-07-10 16:17:43 -05:00
|
|
|
|
2015-07-12 15:57:04 -05:00
|
|
|
new Coroutine($generator());
|
2015-07-10 02:23:08 -05:00
|
|
|
Loop\run();
|