pmmpthread support

This commit is contained in:
Dylan K. Taylor
2023-05-20 01:29:26 +01:00
parent 8454076235
commit e0630fbb25
24 changed files with 159 additions and 126 deletions

View File

@ -23,13 +23,14 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pmmp\thread\ThreadSafeArray;
use raklib\server\ipc\InterThreadChannelReader;
final class PthreadsChannelReader implements InterThreadChannelReader{
/**
* @phpstan-param \ThreadedArray<int, string> $buffer
* @phpstan-param ThreadSafeArray<int, string> $buffer
*/
public function __construct(private \ThreadedArray $buffer){}
public function __construct(private ThreadSafeArray $buffer){}
public function read() : ?string{
return $this->buffer->shift();

View File

@ -23,13 +23,14 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pmmp\thread\ThreadSafeArray;
use raklib\server\ipc\InterThreadChannelWriter;
final class PthreadsChannelWriter implements InterThreadChannelWriter{
/**
* @phpstan-param \ThreadedArray<int, string> $buffer
* @phpstan-param ThreadSafeArray<int, string> $buffer
*/
public function __construct(private \ThreadedArray $buffer){}
public function __construct(private ThreadSafeArray $buffer){}
public function write(string $str) : void{
$this->buffer[] = $str;

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pmmp\thread\ThreadSafeArray;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\network\AdvancedNetworkInterface;
use pocketmine\network\mcpe\compression\ZlibCompressor;
@ -105,10 +106,10 @@ class RakLibInterface implements ServerEventListener, AdvancedNetworkInterface{
$this->sleeper = new SleeperNotifier();
/** @phpstan-var \ThreadedArray<int, string> $mainToThreadBuffer */
$mainToThreadBuffer = new \ThreadedArray();
/** @phpstan-var \ThreadedArray<int, string> $threadToMainBuffer */
$threadToMainBuffer = new \ThreadedArray();
/** @phpstan-var ThreadSafeArray<int, string> $mainToThreadBuffer */
$mainToThreadBuffer = new ThreadSafeArray();
/** @phpstan-var ThreadSafeArray<int, string> $threadToMainBuffer */
$threadToMainBuffer = new ThreadSafeArray();
$this->rakLib = new RakLibServer(
$this->server->getLogger(),

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pmmp\thread\Thread as NativeThread;
use pmmp\thread\ThreadSafeArray;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\thread\NonThreadSafeValue;
use pocketmine\thread\Thread;
@ -38,7 +40,6 @@ use function error_get_last;
use function gc_enable;
use function ini_set;
use function register_shutdown_function;
use const PTHREADS_INHERIT_NONE;
class RakLibServer extends Thread{
protected bool $cleanShutdown = false;
@ -50,13 +51,13 @@ class RakLibServer extends Thread{
protected NonThreadSafeValue $address;
/**
* @phpstan-param \ThreadedArray<int, string> $mainToThreadBuffer
* @phpstan-param \ThreadedArray<int, string> $threadToMainBuffer
* @phpstan-param ThreadSafeArray<int, string> $mainToThreadBuffer
* @phpstan-param ThreadSafeArray<int, string> $threadToMainBuffer
*/
public function __construct(
protected \ThreadedLogger $logger,
protected \ThreadedArray $mainToThreadBuffer,
protected \ThreadedArray $threadToMainBuffer,
protected \ThreadSafeLogger $logger,
protected ThreadSafeArray $mainToThreadBuffer,
protected ThreadSafeArray $threadToMainBuffer,
InternetAddress $address,
protected int $serverId,
protected int $maxMtuSize,
@ -88,13 +89,13 @@ class RakLibServer extends Thread{
}
private function setCrashInfo(RakLibThreadCrashInfo $info) : void{
$this->synchronized(function(RakLibThreadCrashInfo $info) : void{
$this->synchronized(function() use ($info) : void{
$this->crashInfo = new NonThreadSafeValue($info);
$this->notify();
}, $info);
});
}
public function startAndWait(int $options = PTHREADS_INHERIT_NONE) : void{
public function startAndWait(int $options = NativeThread::INHERIT_NONE) : void{
$this->start($options);
$this->synchronized(function() : void{
while(!$this->ready && $this->crashInfo === null){

View File

@ -23,15 +23,16 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pmmp\thread\ThreadSafeArray;
use pocketmine\snooze\SleeperNotifier;
use raklib\server\ipc\InterThreadChannelWriter;
final class SnoozeAwarePthreadsChannelWriter implements InterThreadChannelWriter{
/**
* @phpstan-param \ThreadedArray<int, string> $buffer
* @phpstan-param ThreadSafeArray<int, string> $buffer
*/
public function __construct(
private \ThreadedArray $buffer,
private ThreadSafeArray $buffer,
private SleeperNotifier $notifier
){}