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

@ -28,8 +28,11 @@ use pocketmine\Server;
use function error_reporting;
trait CommonThreadPartsTrait{
/** @var \Threaded|\ClassLoader[]|null */
private ?\Threaded $classLoaders = null;
/**
* @var \ThreadedArray|\ClassLoader[]|null
* @phpstan-var \ThreadedArray<int, \ClassLoader>|null
*/
private ?\ThreadedArray $classLoaders = null;
protected ?string $composerAutoloaderPath = null;
protected bool $isKilled = false;
@ -52,7 +55,7 @@ trait CommonThreadPartsTrait{
}
if($this->classLoaders === null){
$this->classLoaders = new \Threaded();
$this->classLoaders = new \ThreadedArray();
}else{
foreach($this->classLoaders as $k => $autoloader){
unset($this->classLoaders[$k]);

View File

@ -0,0 +1,57 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\thread;
use function get_debug_type;
use function igbinary_serialize;
use function igbinary_unserialize;
/**
* This class automatically serializes values which can't be shared between threads.
* This class does NOT enable sharing the variable between threads. Each call to deserialize() will return a new copy
* of the variable.
*
* @phpstan-template TValue
*/
final class NonThreadSafeValue extends \ThreadedBase{
private string $variable;
/**
* @phpstan-param TValue $variable
*/
public function __construct(
mixed $variable
){
$this->variable = igbinary_serialize($variable) ?? throw new \InvalidArgumentException("Cannot serialize variable of type " . get_debug_type($variable));
}
/**
* Returns a deserialized copy of the original variable.
*
* @phpstan-return TValue
*/
public function deserialize() : mixed{
return igbinary_unserialize($this->variable);
}
}

View File

@ -25,7 +25,7 @@ namespace pocketmine\thread;
use function spl_object_id;
class ThreadManager extends \Volatile{
class ThreadManager extends \ThreadedBase{
private static ?self $instance = null;
@ -40,12 +40,19 @@ class ThreadManager extends \Volatile{
return self::$instance;
}
/** @phpstan-var \ThreadedArray<int, Thread|Worker> */
private \ThreadedArray $threads;
private function __construct(){
$this->threads = new \ThreadedArray();
}
public function add(Worker|Thread $thread) : void{
$this[spl_object_id($thread)] = $thread;
$this->threads[spl_object_id($thread)] = $thread;
}
public function remove(Worker|Thread $thread) : void{
unset($this[spl_object_id($thread)]);
unset($this->threads[spl_object_id($thread)]);
}
/**
@ -56,7 +63,7 @@ class ThreadManager extends \Volatile{
/**
* @var Worker|Thread $thread
*/
foreach($this as $key => $thread){
foreach($this->threads as $key => $thread){
$array[$key] = $thread;
}