-
Notifications
You must be signed in to change notification settings - Fork 74
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
13 changed files
with
373 additions
and
67 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
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
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
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,64 @@ | ||
<?php | ||
/** | ||
* This file is part of Swoole. | ||
* | ||
* @link https://www.swoole.com | ||
* @contact [email protected] | ||
* @license https://github.com/swoole/library/blob/master/LICENSE | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Swoole\Coroutine; | ||
|
||
use Swoole\Coroutine; | ||
use Swoole\Exception; | ||
use Swoole\Timer; | ||
|
||
class Barrier | ||
{ | ||
private $cid = -1; | ||
|
||
private $timer = -1; | ||
|
||
private static $cancel_list = []; | ||
|
||
public function __destruct() | ||
{ | ||
if ($this->timer != -1) { | ||
Timer::clear($this->timer); | ||
if (static::$cancel_list[$this->cid]) { | ||
unset(static::$cancel_list[$this->cid]); | ||
return; | ||
} | ||
} | ||
if ($this->cid != -1) { | ||
Coroutine::resume($this->cid); | ||
} | ||
} | ||
|
||
public static function make() | ||
{ | ||
return new static(); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public static function wait(Barrier &$barrier, float $timeout = -1) | ||
{ | ||
if ($barrier->cid != -1) { | ||
throw new Exception('The barrier is waiting, cannot wait again.'); | ||
} | ||
$cid = Coroutine::getCid(); | ||
$barrier->cid = $cid; | ||
if ($timeout > 0 && ($timeout_ms = intval($timeout * 1000)) > 0) { | ||
$barrier->timer = Timer::after($timeout_ms, function () use ($cid) { | ||
self::$cancel_list[$cid] = true; | ||
Coroutine::resume($cid); | ||
}); | ||
} | ||
$barrier = null; | ||
Coroutine::yield(); | ||
} | ||
} |
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
Oops, something went wrong.