Skip to content

Commit

Permalink
added short name
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jan 27, 2021
1 parent d11b08d commit 82f63be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
12 changes: 12 additions & 0 deletions examples/coroutine/short_name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

run(function () {
sleep(1);
go(function () {
usleep(100000);
echo "co2\n";
});
echo "co1\n";
});
22 changes: 6 additions & 16 deletions src/alias_ns.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,16 @@

declare(strict_types=1);

namespace Swoole\Coroutine {
use Swoole\Coroutine;
namespace Co;

if (SWOOLE_USE_SHORTNAME) {
function run(callable $fn, ...$args)
{
$s = new Scheduler();
$options = Coroutine::getOptions();
if (!isset($options['hook_flags'])) {
$s->set(['hook_flags' => SWOOLE_HOOK_ALL]);
}
$s->add($fn, ...$args);
return $s->start();
return \Swoole\Coroutine\run($fn, ...$args);
}
}

namespace Co {
if (SWOOLE_USE_SHORTNAME) {
function run(callable $fn, ...$args)
{
return \Swoole\Coroutine\run($fn, ...$args);
}
function go(callable $fn, ...$args)
{
return \Swoole\Coroutine::create($fn, ...$args);
}
}
16 changes: 16 additions & 0 deletions src/core/Coroutine/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@

use Swoole\Coroutine;

function run(callable $fn, ...$args)
{
$s = new Scheduler();
$options = Coroutine::getOptions();
if (!isset($options['hook_flags'])) {
$s->set(['hook_flags' => SWOOLE_HOOK_ALL]);
}
$s->add($fn, ...$args);
return $s->start();
}

function go(callable $fn, ...$args)
{
Coroutine::create($fn, ...$args);
}

function batch(array $tasks, float $timeout = -1): array
{
$wg = new WaitGroup(count($tasks));
Expand Down

0 comments on commit 82f63be

Please sign in to comment.