Skip to content

Commit

Permalink
Update some methods and add new Mutex!
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Aug 28, 2024
1 parent 793228d commit b134d64
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/vennv/vapm/AwaitGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ public function getCount(): int;
/**
* @return void
*
* This function is used to wait for the count to be zero
* This function is used to reset the count
*/
public function reset(): void;

/**
* @return void
*
* This function is used to wait for the count to be zero
*/
public function wait(): void;

}

final class AwaitGroup implements AwaitGroupInterface
Expand Down Expand Up @@ -100,7 +107,7 @@ public function reset(): void
public function wait(): void
{
while ($this->count > 0) {
// Wait for the count to be zero
CoroutineGen::run();
}
}

Expand Down
27 changes: 25 additions & 2 deletions src/vennv/vapm/ChildCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,34 @@
interface ChildCoroutineInterface
{

/**
* @param Exception $exception
* @return void
*
* This function sets the exception.
*/
public function setException(Exception $exception): void;

public function run(): void;
/**
* @return ChildCoroutine
*
* This function runs the coroutine.
*/

public function run(): ChildCoroutine;

/**
* @return bool
*
* This function checks if the coroutine is finished.
*/
public function isFinished(): bool;

/**
* @return mixed
*
* This function returns the return value of the coroutine.
*/
public function getReturn(): mixed;

}
Expand All @@ -56,9 +78,10 @@ public function setException(Exception $exception): void
$this->exception = $exception;
}

public function run(): void
public function run(): ChildCoroutine
{
$this->coroutine->send($this->coroutine->current());
return $this;
}

public function isFinished(): bool
Expand Down
10 changes: 2 additions & 8 deletions src/vennv/vapm/CoroutineGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,15 @@ public static function getTaskQueue(): ?SplQueue
public static function runNonBlocking(mixed ...$coroutines): void
{
System::init();

self::$taskQueue ??= new SplQueue();

foreach ($coroutines as $coroutine) {
if (is_callable($coroutine)) $coroutine = call_user_func($coroutine);

if ($coroutine instanceof Generator) {
self::schedule(new ChildCoroutine($coroutine));
} else {
call_user_func(fn() => $coroutine);
}
}

self::run();
}

/**
Expand Down Expand Up @@ -168,9 +163,8 @@ public static function run(): void
{
if (self::$taskQueue?->isEmpty() === false) {
$coroutine = self::$taskQueue->dequeue();
if ($coroutine instanceof ChildCoroutine) {
$coroutine->run();
if (!$coroutine->isFinished()) self::schedule($coroutine);
if ($coroutine instanceof ChildCoroutine && !$coroutine->isFinished()) {
self::schedule($coroutine->run());
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions src/vennv/vapm/Mutex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* Vapm - A library support for PHP about Async, Promise, Coroutine, Thread, GreenThread
* and other non-blocking methods. The library also includes some Javascript packages
* such as Express. The method is based on Fibers & Generator & Processes, requires
* you to have php version from >= 8.1
*
* Copyright (C) 2023 VennDev
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

declare(strict_types=1);

namespace vennv\vapm;

use Generator;

interface MutexInterface
{

/**
* @return bool
*
* This function returns the lock status.
*/
public function isLocked(): bool;

/**
* @return Generator
*
* This function locks the mutex.
*/
public function lock(): Generator;

/**
* @return Generator
*
* This function unlocks the mutex.
*/
public function unlock(): Generator;

}

final class Mutex implements MutexInterface
{

private bool $locked = false;

/**
* @return bool
*
* This function returns the lock status.
*/
public function isLocked(): bool
{
return $this->locked;
}

/**
* @return Generator
*
* This function locks the mutex.
*/
public function lock(): Generator
{
while ($this->locked) {
yield;
}
$this->locked = true;
}

/**
* @return Generator
*
* This function unlocks the mutex.
*/
public function unlock(): Generator
{
yield $this->locked = false;
}

}

0 comments on commit b134d64

Please sign in to comment.