Require pthreads ^5.1

This version of pthreads has a substantially improved API, improved
performance, improved memory usage, and much less magical and broken
behaviour.
This commit is contained in:
Dylan K. Taylor
2023-01-23 20:02:33 +00:00
parent 14b250c63f
commit 222415859a
22 changed files with 245 additions and 118 deletions

View File

@ -26,7 +26,10 @@ namespace pocketmine\network\mcpe\raklib;
use raklib\server\ipc\InterThreadChannelReader;
final class PthreadsChannelReader implements InterThreadChannelReader{
public function __construct(private \Threaded $buffer){}
/**
* @phpstan-param \ThreadedArray<int, string> $buffer
*/
public function __construct(private \ThreadedArray $buffer){}
public function read() : ?string{
return $this->buffer->shift();

View File

@ -26,7 +26,10 @@ namespace pocketmine\network\mcpe\raklib;
use raklib\server\ipc\InterThreadChannelWriter;
final class PthreadsChannelWriter implements InterThreadChannelWriter{
public function __construct(private \Threaded $buffer){}
/**
* @phpstan-param \ThreadedArray<int, string> $buffer
*/
public function __construct(private \ThreadedArray $buffer){}
public function write(string $str) : void{
$this->buffer[] = $str;

View File

@ -85,8 +85,10 @@ class RakLibInterface implements ServerEventListener, AdvancedNetworkInterface{
$this->sleeper = new SleeperNotifier();
$mainToThreadBuffer = new \Threaded();
$threadToMainBuffer = new \Threaded();
/** @phpstan-var \ThreadedArray<int, string> $mainToThreadBuffer */
$mainToThreadBuffer = new \ThreadedArray();
/** @phpstan-var \ThreadedArray<int, string> $threadToMainBuffer */
$threadToMainBuffer = new \ThreadedArray();
$this->rakLib = new RakLibServer(
$this->server->getLogger(),

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\thread\NonThreadSafeValue;
use pocketmine\thread\Thread;
use raklib\generic\Socket;
use raklib\generic\SocketException;
@ -43,19 +44,27 @@ class RakLibServer extends Thread{
protected bool $cleanShutdown = false;
protected bool $ready = false;
protected string $mainPath;
public ?RakLibThreadCrashInfo $crashInfo = null;
/** @phpstan-var NonThreadSafeValue<RakLibThreadCrashInfo>|null */
public ?NonThreadSafeValue $crashInfo = null;
/** @phpstan-var NonThreadSafeValue<InternetAddress> */
protected NonThreadSafeValue $address;
/**
* @phpstan-param \ThreadedArray<int, string> $mainToThreadBuffer
* @phpstan-param \ThreadedArray<int, string> $threadToMainBuffer
*/
public function __construct(
protected \ThreadedLogger $logger,
protected \Threaded $mainToThreadBuffer,
protected \Threaded $threadToMainBuffer,
protected InternetAddress $address,
protected \ThreadedArray $mainToThreadBuffer,
protected \ThreadedArray $threadToMainBuffer,
InternetAddress $address,
protected int $serverId,
protected int $maxMtuSize,
protected int $protocolVersion,
protected SleeperNotifier $mainThreadNotifier
){
$this->mainPath = \pocketmine\PATH;
$this->address = new NonThreadSafeValue($address);
}
/**
@ -75,12 +84,12 @@ class RakLibServer extends Thread{
}
public function getCrashInfo() : ?RakLibThreadCrashInfo{
return $this->crashInfo;
return $this->crashInfo?->deserialize();
}
private function setCrashInfo(RakLibThreadCrashInfo $info) : void{
$this->synchronized(function(RakLibThreadCrashInfo $info) : void{
$this->crashInfo = $info;
$this->crashInfo = new NonThreadSafeValue($info);
$this->notify();
}, $info);
}
@ -91,7 +100,7 @@ class RakLibServer extends Thread{
while(!$this->ready && $this->crashInfo === null){
$this->wait();
}
$crashInfo = $this->crashInfo;
$crashInfo = $this->crashInfo?->deserialize();
if($crashInfo !== null){
if($crashInfo->getClass() === SocketException::class){
throw new SocketException($crashInfo->getMessage());
@ -110,7 +119,7 @@ class RakLibServer extends Thread{
register_shutdown_function([$this, "shutdownHandler"]);
try{
$socket = new Socket($this->address);
$socket = new Socket($this->address->deserialize());
}catch(SocketException $e){
$this->setCrashInfo(RakLibThreadCrashInfo::fromThrowable($e));
return;

View File

@ -27,8 +27,11 @@ use pocketmine\snooze\SleeperNotifier;
use raklib\server\ipc\InterThreadChannelWriter;
final class SnoozeAwarePthreadsChannelWriter implements InterThreadChannelWriter{
/**
* @phpstan-param \ThreadedArray<int, string> $buffer
*/
public function __construct(
private \Threaded $buffer,
private \ThreadedArray $buffer,
private SleeperNotifier $notifier
){}