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,16 +23,17 @@ declare(strict_types=1);
namespace pocketmine\thread;
use pmmp\thread\ThreadSafeArray;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\Server;
use function error_reporting;
trait CommonThreadPartsTrait{
/**
* @var \ThreadedArray|\ClassLoader[]|null
* @phpstan-var \ThreadedArray<int, \ClassLoader>|null
* @var ThreadSafeArray|\ClassLoader[]|null
* @phpstan-var ThreadSafeArray<int, \ClassLoader>|null
*/
private ?\ThreadedArray $classLoaders = null;
private ?ThreadSafeArray $classLoaders = null;
protected ?string $composerAutoloaderPath = null;
protected bool $isKilled = false;
@ -55,14 +56,15 @@ trait CommonThreadPartsTrait{
}
if($this->classLoaders === null){
$this->classLoaders = new \ThreadedArray();
$loaders = $this->classLoaders = new ThreadSafeArray();
}else{
$loaders = $this->classLoaders;
foreach($this->classLoaders as $k => $autoloader){
unset($this->classLoaders[$k]);
}
}
foreach($autoloaders as $autoloader){
$this->classLoaders[] = $autoloader;
$loaders[] = $autoloader;
}
}

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\thread;
use pmmp\thread\ThreadSafe;
use function get_debug_type;
use function igbinary_serialize;
use function igbinary_unserialize;
@ -34,7 +35,7 @@ use function igbinary_unserialize;
*
* @phpstan-template TValue
*/
final class NonThreadSafeValue extends \ThreadedBase{
final class NonThreadSafeValue extends ThreadSafe{
private string $variable;
/**

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\thread;
use pmmp\thread\Thread as NativeThread;
use pocketmine\scheduler\AsyncTask;
use const PTHREADS_INHERIT_NONE;
/**
* Specialized Thread class aimed at PocketMine-MP-related usages. It handles setting up autoloading and error handling.
@ -35,10 +35,10 @@ use const PTHREADS_INHERIT_NONE;
* CPU.
* @see AsyncTask
*/
abstract class Thread extends \Thread{
abstract class Thread extends NativeThread{
use CommonThreadPartsTrait;
public function start(int $options = PTHREADS_INHERIT_NONE) : bool{
public function start(int $options = NativeThread::INHERIT_NONE) : bool{
//this is intentionally not traitified
ThreadManager::getInstance()->add($this);

View File

@ -23,9 +23,11 @@ declare(strict_types=1);
namespace pocketmine\thread;
use pmmp\thread\ThreadSafe;
use pmmp\thread\ThreadSafeArray;
use function spl_object_id;
class ThreadManager extends \ThreadedBase{
class ThreadManager extends ThreadSafe{
private static ?self $instance = null;
@ -40,11 +42,11 @@ class ThreadManager extends \ThreadedBase{
return self::$instance;
}
/** @phpstan-var \ThreadedArray<int, Thread|Worker> */
private \ThreadedArray $threads;
/** @phpstan-var ThreadSafeArray<int, Thread|Worker> */
private ThreadSafeArray $threads;
private function __construct(){
$this->threads = new \ThreadedArray();
$this->threads = new ThreadSafeArray();
}
public function add(Worker|Thread $thread) : void{

View File

@ -23,8 +23,9 @@ declare(strict_types=1);
namespace pocketmine\thread;
use pmmp\thread\Thread as NativeThread;
use pmmp\thread\Worker as NativeWorker;
use pocketmine\scheduler\AsyncTask;
use const PTHREADS_INHERIT_NONE;
/**
* Specialized Worker class for PocketMine-MP-related use cases. It handles setting up autoloading and error handling.
@ -36,10 +37,10 @@ use const PTHREADS_INHERIT_NONE;
* If you want to run tasks on other CPU cores, check out AsyncTask first.
* @see AsyncTask
*/
abstract class Worker extends \Worker{
abstract class Worker extends NativeWorker{
use CommonThreadPartsTrait;
public function start(int $options = PTHREADS_INHERIT_NONE) : bool{
public function start(int $options = NativeThread::INHERIT_NONE) : bool{
//this is intentionally not traitified
ThreadManager::getInstance()->add($this);