Skip to content

Commit

Permalink
remove dependence on global keyword, use static class
Browse files Browse the repository at this point in the history
- this is for mimicking the `ext-parallel` extension execution behavior of passing user defined globals around in threads.
  • Loading branch information
TheTechsTech committed Dec 15, 2021
1 parent 1180bfa commit f709ef0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
15 changes: 4 additions & 11 deletions Spawn/Channeled.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public function __construct(
?string $name = null,
bool $anonymous = true
) {
global $___channeled___;
if ($this->capacity !== null && $___channeled___ === 'parallel')
if ($this->capacity !== null && Globals::isChannelling())
$capacity = $this->capacity;

if (($capacity < -1) || ($capacity == 0))
Expand Down Expand Up @@ -150,12 +149,10 @@ public static function make(string $name, int $capacity = -1): ChanneledInterfac

public static function open(string $name): ChanneledInterface
{
global $___channeled___;

if (static::isChannel($name))
return static::$channels[$name];

if ($___channeled___ === 'parallel')
if (Globals::isChannelling())
return new static(-1, $name, false);

static::throwExistence(\sprintf('channel named %s not found', $name));
Expand Down Expand Up @@ -260,13 +257,11 @@ public static function isMessenger($message): bool

public function send($value): void
{
global $___channeled___;

if ($this->isClosed())
static::throwClosed(\sprintf('channel(%s) closed', $this->name));

if (
!isset($___channeled___) && null !== $value && $this->process === null && !\is_resource($value)
!Globals::isChannelling() && null !== $value && $this->process === null && !\is_resource($value)
&& ($this->capacity > $this->buffered->count() || $this->capacity == -1) && $this->type === 'buffered'
) {
try {
Expand Down Expand Up @@ -329,10 +324,8 @@ function () use ($future, $checkState) {

public function recv()
{
global $___channeled___;

if (
!isset($___channeled___) && $this->type === 'buffered'
!Globals::isChannelling() && $this->type === 'buffered'
&& $this->process === null && !$this->buffered->isEmpty()
) {
try {
Expand Down
25 changes: 7 additions & 18 deletions Spawn/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Async\Spawn\ChanneledInterface;
use Async\Spawn\Spawn;
use Async\Spawn\FutureInterface;
use Async\Spawn\Globals;
use Async\Spawn\ParallelInterface;

use function Opis\Closure\{serialize as serializing, unserialize as deserializing};
Expand Down Expand Up @@ -346,7 +347,8 @@ function spawning(
*/
function parallel($task, ...$argv): FutureInterface
{
global $___paralleling;
$___paralleling = Globals::get();

$channel = null;
foreach ($argv as $isChannel) {
if ($isChannel instanceof ChanneledInterface) {
Expand Down Expand Up @@ -387,7 +389,7 @@ function parallel($task, ...$argv): FutureInterface
*/
function paralleling(?\Closure $task = null, ?string $include = null, ...$args): FutureInterface
{
global $___paralleling;
$___paralleling = Globals::get();

$channel = null;
foreach ($args as $isChannel) {
Expand All @@ -414,29 +416,16 @@ function paralleling(?\Closure $task = null, ?string $include = null, ...$args):
}

/**
* Returns an array of **Future** `user defined` *global* variables.
*
* @return array|null
*/
function paralleling_globals(): ?array
{
global $___paralleling;

return $___paralleling;
}

/**
* Setup `user defined` global `key => value` pair to be transferred to `Future` **subprocess**.
* Setup `user defined` global `key => value` pair to be transferred to `Future` **child-process**.
* - Can `include/require` an additional **file** to execute.
* - Also an indicator for a `Channel` that it has been started by `subprocess` Future.
* - Also an indicator for a `Channel` that it has been started by `child-process` Future.
*
* @param string $include additional file to execute
* @param array|null $keyValue
* @return void
*/
function paralleling_setup(?string $include = null, ?array $keyValue = null): void
{
global $___channeled___;
if (!empty($include) && \is_string($include)) {
require $include;
}
Expand All @@ -445,7 +434,7 @@ function paralleling_setup(?string $include = null, ?array $keyValue = null): vo
foreach ($keyValue as $key => $value)
$GLOBALS[$key] = $value;

$___channeled___ = 'parallel';
Globals::channelling();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Spawn/Future.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ public function getLast()

public function getResult()
{
global $___paralleling;
$user = Globals::get();

if (\is_null($this->finalResult)) {
$this->finalResult = $this->decode($this->lastResult);
Expand All @@ -692,7 +692,9 @@ public function getResult()
if (isset($paralleling) && \is_array($paralleling)) {
unset($paralleling['GLOBALS']);
unset($paralleling['results']);
$___paralleling = \is_array($___paralleling) ? \array_merge($___paralleling, $paralleling) : $paralleling;
$defined = \is_array($user) ? \array_merge($user, $paralleling) : $paralleling;
foreach ($defined as $key => $value)
Globals::set($key, $value);
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions Spawn/Globals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Async\Spawn;

class Globals
{
/**
* User `defined` **global** variables
*
* @var array[]
*/
protected static $defined;

protected static $isChannel = false;

/**
* Returns an array of **Future** `user defined` *global* variables.
*
* @return array[]|null
*/
public static function get(): ?array
{
return self::$defined;
}

public static function set(?string $key, $value): void
{
if (isset($key))
self::$defined[$key] = $value;
}

public static function channelling(): void
{
self::$isChannel = true;
}

public static function isChannelling(): bool
{
return self::$isChannel;
}
}
3 changes: 2 additions & 1 deletion tests/SpawnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Async\Tests;

use Async\Spawn\Globals;
use Async\Spawn\Spawn;
use Async\Spawn\SpawnError;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -346,7 +347,7 @@ public function testCanUseClassParentProcessAndReturnGlobal()
$this->assertInstanceOf(MyClass::class, $result);
$this->assertTrue($result->property);

$global = paralleling_globals();
$global = Globals::get();
$this->assertEquals($global['__testing'], 'helping');
}

Expand Down

0 comments on commit f709ef0

Please sign in to comment.