diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index 50e31768b..b2d5714e7 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -345,12 +345,24 @@ namespace pocketmine { $logger->warning("Non-packaged PocketMine-MP installation detected, do not use on production."); } + ThreadManager::init(); $server = new Server($autoloader, $logger, \pocketmine\PATH, \pocketmine\DATA, \pocketmine\PLUGIN_PATH); $server->start(); + + foreach(ThreadManager::getInstance()->getAll() as $id => $thread){ + if($thread->isRunning()){ + $logger->debug("Stopping ".(new \ReflectionClass($thread))->getShortName()." thread"); + if($thread instanceof Thread){ + $thread->kill(); + $thread->detach(); + }elseif($thread instanceof Worker){ + $thread->shutdown(); + $thread->join(); + } + } + } + $logger->shutdown(); $logger->join(); - kill(getmypid()); - exit(0); - } diff --git a/src/pocketmine/Thread.php b/src/pocketmine/Thread.php new file mode 100644 index 000000000..c23b15bb5 --- /dev/null +++ b/src/pocketmine/Thread.php @@ -0,0 +1,33 @@ +add($this); + return parent::start($options & ~PTHREADS_INHERIT_CLASSES); + } +} \ No newline at end of file diff --git a/src/pocketmine/ThreadManager.php b/src/pocketmine/ThreadManager.php new file mode 100644 index 000000000..f507cd70b --- /dev/null +++ b/src/pocketmine/ThreadManager.php @@ -0,0 +1,68 @@ +{spl_object_hash($thread)} = $thread; + } + } + + /** + * @param Worker|Thread $thread + */ + public function remove($thread){ + if($thread instanceof Thread or $thread instanceof Worker){ + unset($this->{spl_object_hash($thread)}); + } + } + + /** + * @return Worker[]|Thread[] + */ + public function getAll(){ + $array = []; + foreach($this as $key => $thread){ + $array[$key] = $thread; + } + return $array; + } +} \ No newline at end of file diff --git a/src/pocketmine/Worker.php b/src/pocketmine/Worker.php new file mode 100644 index 000000000..e84bf7d52 --- /dev/null +++ b/src/pocketmine/Worker.php @@ -0,0 +1,33 @@ +add($this); + return parent::start($options & ~PTHREADS_INHERIT_CLASSES); + } +} \ No newline at end of file diff --git a/src/pocketmine/command/CommandReader.php b/src/pocketmine/command/CommandReader.php index e479c4c60..ac8615f05 100644 --- a/src/pocketmine/command/CommandReader.php +++ b/src/pocketmine/command/CommandReader.php @@ -21,7 +21,9 @@ namespace pocketmine\command; -class CommandReader extends \Thread{ +use pocketmine\Thread; + +class CommandReader extends Thread{ private $stream; /** @var resource */ @@ -36,7 +38,7 @@ class CommandReader extends \Thread{ */ public function __construct($stream = "php://stdin"){ $this->stream = $stream; - $this->start(PTHREADS_INHERIT_ALL & ~PTHREADS_INHERIT_CLASSES); + $this->start(); } private function readLine(){ diff --git a/src/pocketmine/level/generator/GenerationThread.php b/src/pocketmine/level/generator/GenerationThread.php index 1c4dca32f..81c2a6d72 100644 --- a/src/pocketmine/level/generator/GenerationThread.php +++ b/src/pocketmine/level/generator/GenerationThread.php @@ -22,7 +22,9 @@ namespace pocketmine\level\generator; -class GenerationThread extends \Thread{ +use pocketmine\Thread; + +class GenerationThread extends Thread{ protected $loadPaths; /** @var \SplAutoloader */ @@ -69,7 +71,7 @@ class GenerationThread extends \Thread{ @socket_set_option($this->externalSocket, SOL_SOCKET, SO_SNDBUF, 1024 * 1024 * 2); @socket_set_option($this->externalSocket, SOL_SOCKET, SO_RCVBUF, 1024 * 1024 * 2); - $this->start(PTHREADS_INHERIT_ALL & ~PTHREADS_INHERIT_CLASSES); + $this->start(); } protected function addDependency(array &$loadPaths, \ReflectionClass $dep){ diff --git a/src/pocketmine/scheduler/AsyncWorker.php b/src/pocketmine/scheduler/AsyncWorker.php index ccef45b2d..f70b1c40b 100644 --- a/src/pocketmine/scheduler/AsyncWorker.php +++ b/src/pocketmine/scheduler/AsyncWorker.php @@ -21,15 +21,11 @@ namespace pocketmine\scheduler; -class AsyncWorker extends \Worker{ +use pocketmine\Worker; + +class AsyncWorker extends Worker{ public $path; - public function start($options = PTHREADS_INHERIT_ALL){ - $this->path = \pocketmine\PATH; - - return parent::start($options & ~PTHREADS_INHERIT_CLASSES); - } - public function run(){ require($this->path . "src/spl/SplClassLoader.php"); $autoloader = new \SplClassLoader();