Skip to content

Commit

Permalink
Style: Format Code
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Feb 9, 2025
1 parent acd87ba commit 2898e0d
Show file tree
Hide file tree
Showing 32 changed files with 431 additions and 424 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/assets/ export-ignore
/docs/ export-ignore
/examples/ export-ignore
/tests/ export-ignore
/.github/ export-ignore
.gitattributes export-ignore
.php-cs-fixer.php export-ignore
phpunit.sh export-ignore
phpunit.xml export-ignore
4 changes: 0 additions & 4 deletions .tarignore

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 9 additions & 3 deletions src/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ protected function openStream(): void
$this->zx7e = new Zx7e();
}

/*** @return void */
/**
* @return void
*/
public function close(): void
{
if ($this->closed) {
Expand Down Expand Up @@ -223,13 +225,17 @@ public function receive(bool $blocking = true): mixed
}
}

/*** @return string */
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/*** @return string */
/**
* @return string
*/
public function getPath(): string
{
return $this->path;
Expand Down
26 changes: 13 additions & 13 deletions src/Coroutine/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public static function define(array|string $key, mixed $value = null): void
Context::$context[$hash][$key] = $value;
}

/**
* @param \Revolt\EventLoop\Suspension|null $suspension
*
* @return string
*/
public static function getHash(Suspension|null $suspension = null): string
{
if (!$suspension) {
$suspension = getSuspension();
}
return spl_object_hash($suspension);
}

/**
* @param string|null $key
*
Expand Down Expand Up @@ -95,17 +108,4 @@ public static function clear(): void
{
unset(Context::$context[Context::getHash()]);
}

/**
* @param \Revolt\EventLoop\Suspension|null $suspension
*
* @return string
*/
public static function getHash(Suspension|null $suspension = null): string
{
if (!$suspension) {
$suspension = getSuspension();
}
return spl_object_hash($suspension);
}
}
114 changes: 57 additions & 57 deletions src/Coroutine/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,63 +143,6 @@ public function await(Promise $promise): mixed
return $result;
}

/**
* @param Closure $closure
*
* @return Promise
*/
public function async(Closure $closure): Promise
{
return promise(function (Closure $resolve, Closure $reject, Promise $promise) use ($closure) {
$currentSuspension = getSuspension();
$suspension = new Suspension(function () use ($closure, $resolve, $reject, $currentSuspension) {
Context::extend($currentSuspension);
try {
$resolve($closure());
} catch (EscapeException $exception) {
throw $exception;
} catch (Throwable $exception) {
$reject($exception);
return;
} finally {
Context::clear();
}
}, $resolve, $reject, $promise);

$this->fiber2suspension[$suspension->fiber] = WeakReference::create($suspension);

try {
$suspension->start();
} catch (EscapeException $exception) {
$this->handleEscapeException($exception);
}
});
}

/**
* @param int|float $second
*
* @return int|float
* @throws Throwable
*/
public function sleep(int|float $second): int|float
{
$suspension = getSuspension();
delay(static fn () => Coroutine::resume($suspension, $second), $second);
return Coroutine::suspend($suspension);
}

/**
* @param EscapeException $exception
*
* @return void
*/
#[NoReturn]
public function handleEscapeException(EscapeException $exception): void
{
Process::getInstance()->processedInMain($exception->lastWords);
}

/**
*
* The coroutine that cannot be restored can only throw an exception.
Expand Down Expand Up @@ -227,6 +170,17 @@ public static function resume(EventLoop\Suspension $suspension, mixed $result =
return null;
}

/**
* @param EscapeException $exception
*
* @return void
*/
#[NoReturn]
public function handleEscapeException(EscapeException $exception): void
{
Process::getInstance()->processedInMain($exception->lastWords);
}

/**
* @param \Revolt\EventLoop\Suspension $suspension
* @param Throwable $exception
Expand Down Expand Up @@ -259,4 +213,50 @@ public static function suspend(EventLoop\Suspension|null $suspension = null): mi
Coroutine::getInstance()->handleEscapeException($exception);
}
}

/**
* @param Closure $closure
*
* @return Promise
*/
public function async(Closure $closure): Promise
{
return promise(function (Closure $resolve, Closure $reject, Promise $promise) use ($closure) {
$currentSuspension = getSuspension();
$suspension = new Suspension(function () use ($closure, $resolve, $reject, $currentSuspension) {
Context::extend($currentSuspension);
try {
$resolve($closure());
} catch (EscapeException $exception) {
throw $exception;
} catch (Throwable $exception) {
$reject($exception);
return;
} finally {
Context::clear();
}
}, $resolve, $reject, $promise);

$this->fiber2suspension[$suspension->fiber] = WeakReference::create($suspension);

try {
$suspension->start();
} catch (EscapeException $exception) {
$this->handleEscapeException($exception);
}
});
}

/**
* @param int|float $second
*
* @return int|float
* @throws Throwable
*/
public function sleep(int|float $second): int|float
{
$suspension = getSuspension();
delay(static fn () => Coroutine::resume($suspension, $second), $second);
return Coroutine::suspend($suspension);
}
}
3 changes: 3 additions & 0 deletions src/File/Exception/FileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

use Exception;

/**
*
*/
class FileException extends Exception
{
}
16 changes: 8 additions & 8 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,6 @@ public function supportParallel(): bool
return $this->parallel;
}

/**
* @return bool
*/
public function supportProcessControl(): bool
{
return $this->processControl;
}

/**
* @Author cclilshy
* @Date 2024/9/24 14:27
Expand Down Expand Up @@ -339,4 +331,12 @@ public function getProcessId(): int
{
return $this->supportProcessControl() ? posix_getpid() : getmygid();
}

/**
* @return bool
*/
public function supportProcessControl(): bool
{
return $this->processControl;
}
}
52 changes: 26 additions & 26 deletions src/Parallel/Future.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Future
public const STATUS_FULFILLED = 1;
public const STATUS_REJECTED = 2;

/*** @var \Ripple\Coroutine\WaitGroup */
/*** @var WaitGroup */
private WaitGroup $waitGroup;

/**
Expand Down Expand Up @@ -62,6 +62,15 @@ public function resolve(mixed $result): void
$this->waitGroup->done();
}

/**
* @return bool
*/
public function done(): bool
{
$this->waitGroup->wait();
return $this->status === Future::STATUS_FULFILLED;
}

/**
* @param Throwable $exception
*
Expand All @@ -82,31 +91,6 @@ public function getStatus(): int
return $this->status;
}

/**
* @return \parallel\Future
*/
public function getParallelFuture(): \parallel\Future
{
return $this->parallelFuture;
}

/**
* @return \parallel\Runtime
*/
public function getRuntime(): Runtime
{
return $this->runtime;
}

/**
* @return bool
*/
public function done(): bool
{
$this->waitGroup->wait();
return $this->status === Future::STATUS_FULFILLED;
}

/**
* @return mixed
* @throws Throwable
Expand All @@ -129,6 +113,14 @@ public function cancel(): bool
return $bool;
}

/**
* @return \parallel\Future
*/
public function getParallelFuture(): \parallel\Future
{
return $this->parallelFuture;
}

/**
* @return bool
*/
Expand All @@ -145,4 +137,12 @@ public function kill(): void
$this->getRuntime()->kill();
Parallel::getInstance()->poll();
}

/**
* @return \parallel\Runtime
*/
public function getRuntime(): Runtime
{
return $this->runtime;
}
}
Loading

0 comments on commit 2898e0d

Please sign in to comment.