Skip to content

Commit

Permalink
update: mutual constraints between processes and threads
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Aug 14, 2024
1 parent 92d742d commit 837d80b
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 232 deletions.
149 changes: 149 additions & 0 deletions example/parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,153 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

use parallel\Channel;
use parallel\Events;
use parallel\Future;
use parallel\Runtime;
use parallel\Sync;
use Psc\Utils\Output;
use Revolt\EventLoop;

use function P\cancel;
use function P\defer;
use function P\onSignal;
use function P\tick;

include_once __DIR__ . '/../vendor/autoload.php';
Output::info(\strval(\posix_getpid()));
class Test
{
public Runtime $counterRuntime;
public Channel $counterChannel;
public Future $counterFuture;
public Sync $counterSync;
public Events $events;
private string $signalHandlerId;

/**
* @return void
*/
public function startCounter(): void
{
if(isset($this->events)) {
return;
}

$this->events = new Events();
$this->events->setBlocking(true);
$this->counterChannel = parallel\Channel::make('counter');
$this->counterSync = new Sync(true);
$this->counterRuntime = new Runtime();
$this->counterFuture = $this->counterRuntime->run(static function (Channel $channel, Sync $sync) {
$sync(fn () => $sync->wait());
$processId = \posix_getpid();
$count = 0;

while($number = $channel->recv()) {
$sync->set($count += $number);
if($number > 0) {
\posix_kill($processId, \SIGUSR2);
}

if($count < 0) {
break;
}
}
}, [
$this->counterChannel,
$this->counterSync
]);

try {
$this->signalHandlerId = onSignal(\SIGUSR2, fn () => $this->poll());
} catch (EventLoop\UnsupportedFeatureException) {
// ignore
}

defer(function () {
$this->counterSync->notify();
});
}

/**
* @return void
* @throws Throwable
*/
public function stopCounter(): void
{
$this->counterChannel->send(-1);
$this->counterFuture->value();
$this->counterRuntime->close();
$this->counterChannel->close();
cancel($this->signalHandlerId);

unset($this->counterRuntime);
unset($this->counterChannel);
unset($this->counterFuture);
unset($this->counterSync);
unset($this->events);
unset($this->signalHandlerId);
}

/**
* @param Closure $closure
* @param array $params
* @param string $name
* @return void
*/
public function run(Closure $closure, array $params, string $name): void
{
$this->startCounter();
$runtime = new Runtime();
$future = $runtime->run(static function (Closure $closure, array $argv, Channel $channel) {
try {
return $closure(...$argv);
} finally {
$channel->send(1);
}
}, [
$closure,
$params,
$this->counterChannel
]);
$this->events->addFuture($name, $future);
}

/**
* @return void
* @throws Throwable
*/
private function poll(): void
{
while($count = $this->counterSync->get()) {
for ($i = 0; $i < $count; $i++) {
$event = $this->events->poll();
$this->counterChannel->send(-1);
}
}
$this->stopCounter();
}

public function __destruct()
{
Output::warning('__destruct');
}
}

$test = new Test();
$test->run(static function (string $name) {
\var_dump('1');
return $name;
}, ['name'], '1');
tick();

$test = new Test();

$test->run(static function (string $name) {
\var_dump('2');
return $name;
}, ['name'], '2');
tick();

\var_dump('ticked');
13 changes: 11 additions & 2 deletions example/thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@

include __DIR__ . '/../vendor/autoload.php';


for ($i = 0; $i < 100; $i++) {
for ($i = 0; $i < 1; $i++) {
$thread = thread(static function ($context) {
return \file_get_contents(__FILE__);
});
Expand All @@ -48,5 +47,15 @@
echo \strlen($value), \PHP_EOL;
});
}
tick();

for ($i = 0; $i < 5; $i++) {
$thread = thread(static function ($context) {
return \file_get_contents(__FILE__);
});

$thread->run()->onValue(static function ($value) {
echo \strlen($value), \PHP_EOL;
});
}
tick();
40 changes: 0 additions & 40 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2023-2024.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
~
~ 特此免费授予任何获得本软件及相关文档文件(“软件”)副本的人,不受限制地处理
~ 本软件,包括但不限于使用、复制、修改、合并、出版、发行、再许可和/或销售
~ 软件副本的权利,并允许向其提供本软件的人做出上述行为,但须符合以下条件:
~
~ 上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。
~
~ 本软件按“原样”提供,不提供任何形式的保证,无论是明示或暗示的,
~ 包括但不限于适销性、特定目的的适用性和非侵权性的保证。在任何情况下,
~ 无论是合同诉讼、侵权行为还是其他方面,作者或版权持有人均不对
~ 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
-->

<phpunit bootstrap="vendor/autoload.php"
colors="true"
stopOnFailure="false"
Expand All @@ -43,10 +9,4 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="XDEBUG_MODE" value="coverage"/>
</php>
</phpunit>
12 changes: 9 additions & 3 deletions src/Core/LibraryAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
*/
abstract class LibraryAbstract
{
/**
* @var LibraryAbstract
*/
/*** @var LibraryAbstract */
protected static LibraryAbstract $instance;

/**
Expand All @@ -54,4 +52,12 @@ public static function getInstance(): static
}
return static::$instance;
}

/**
* @return bool
*/
public static function hasInstance(): bool
{
return isset(static::$instance);
}
}
40 changes: 23 additions & 17 deletions src/Core/Parallel/Future.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Closure;
use parallel\Events;
use parallel\Events\Event;
use Psc\Utils\Output;
use Throwable;

class Future
Expand Down Expand Up @@ -102,24 +103,29 @@ public function onError(Closure $onError): Future
*/
public function onEvent(Events\Event $event): void
{
switch ($event->type) {
case Events\Event\Type::Error:
if (isset($this->onError)) {
($this->onError)($event->value);
}
break;

case Events\Event\Type::Cancel:
if (isset($this->onCancelled)) {
($this->onCancelled)($event->value);
}
break;
case Events\Event\Type::Kill:
if (isset($this->onKilled)) {
($this->onKilled)($event->value);
}
break;
try {
switch ($event->type) {
case Events\Event\Type::Error:
if (isset($this->onError)) {
($this->onError)($event->value);
}
break;

case Events\Event\Type::Cancel:
if (isset($this->onCancelled)) {
($this->onCancelled)($event->value);
}
break;
case Events\Event\Type::Kill:
if (isset($this->onKilled)) {
($this->onKilled)($event->value);
}
break;
}
} catch (Throwable $exception) {
Output::error($exception->getMessage());
}

}

/**
Expand Down
Loading

0 comments on commit 837d80b

Please sign in to comment.