-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* @author workbunny/Chaz6chez | ||
* @email [email protected] | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers; | ||
|
||
class DefaultWaitGroup implements WaitGroupInterface | ||
{ | ||
/** @var int */ | ||
protected int $_count; | ||
|
||
/** @inheritdoc */ | ||
public function __construct() | ||
{ | ||
$this->_count = 0; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function __destruct() | ||
{ | ||
$this->_count = 0; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function add(int $delta = 1): bool | ||
{ | ||
$this->_count ++; | ||
return true; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function done(): bool | ||
{ | ||
$this->_count --; | ||
return true; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function wait(int $timeout = -1): void | ||
{ | ||
$time = time(); | ||
while (1) { | ||
if ($timeout > 0 and time() - $time >= $timeout) { | ||
return; | ||
} | ||
if ($this->_count <= 0) { | ||
|
||
return; | ||
} | ||
// todo sleep | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @author workbunny/Chaz6chez | ||
* @email [email protected] | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers; | ||
|
||
interface WaitGroupInterface | ||
{ | ||
/** | ||
* 初始化 | ||
*/ | ||
public function __construct(); | ||
|
||
/** | ||
* 销毁 | ||
*/ | ||
public function __destruct(); | ||
|
||
/** | ||
* 增加一个计数 | ||
* | ||
* @param int $delta | ||
* @return bool | ||
*/ | ||
public function add(int $delta = 1): bool; | ||
|
||
/** | ||
* 完成一个计数 | ||
* | ||
* @return bool | ||
*/ | ||
public function done(): bool; | ||
|
||
/** | ||
* 阻塞等待 | ||
* | ||
* @param int $timeout | ||
* @return void | ||
*/ | ||
public function wait(int $timeout = -1): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* @author workbunny/Chaz6chez | ||
* @email [email protected] | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Workbunny\WebmanCoroutine\Utils\WaitGroup; | ||
|
||
use Workbunny\WebmanCoroutine\Factory; | ||
use Workbunny\WebmanCoroutine\Utils\RegisterMethods; | ||
use Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers\DefaultWaitGroup; | ||
use Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers\WaitGroupInterface; | ||
|
||
class WaitGroup | ||
{ | ||
use RegisterMethods; | ||
|
||
/** | ||
* @var WaitGroupInterface | ||
*/ | ||
protected WaitGroupInterface $_interface; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected static array $_handlers = [ | ||
// Factory::WORKERMAN_SWOW => SwowChannel::class, | ||
// Factory::WORKBUNNY_SWOW => SwowChannel::class, | ||
// Factory::WORKERMAN_SWOOLE => SwooleChannel::class, | ||
// Factory::WORKBUNNY_SWOOLE => SwooleChannel::class, | ||
Factory::RIPPLE_FIBER => DefaultWaitGroup::class, | ||
]; | ||
|
||
/** | ||
* 构造方法 | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->_interface = new (self::$_handlers[Factory::getCurrentEventLoop()] ?? DefaultWaitGroup::class)(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public static function registerVerify(mixed $value): false|string | ||
{ | ||
return is_a($value, WaitGroupInterface::class) ? WaitGroupInterface::class : false; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public static function unregisterExecute(string $key): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* 代理调用WaitGroupInterface方法 | ||
* | ||
* @param string $name | ||
* @param array $arguments | ||
* @return mixed | ||
*/ | ||
public function __call(string $name, array $arguments): mixed | ||
{ | ||
if (!method_exists($this->_interface, $name)) { | ||
throw new \BadMethodCallException("Method $name not exists. "); | ||
} | ||
return $this->_interface->$name(...$arguments); | ||
} | ||
} |