Added ThreadManager, removed PHP self-kill

This commit is contained in:
Shoghi Cervantes 2014-08-08 11:36:55 +02:00
parent 269a10fadd
commit 51120fdd4f
7 changed files with 160 additions and 14 deletions

View File

@ -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);
}

33
src/pocketmine/Thread.php Normal file
View File

@ -0,0 +1,33 @@
<?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/
*
*
*/
namespace pocketmine;
/**
* This class must be extended by all custom threading classes
*/
abstract class Thread extends \Thread{
public final function start($options = PTHREADS_INHERIT_ALL){
ThreadManager::getInstance()->add($this);
return parent::start($options & ~PTHREADS_INHERIT_CLASSES);
}
}

View File

@ -0,0 +1,68 @@
<?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/
*
*
*/
namespace pocketmine;
class ThreadManager extends \Threaded{
/** @var ThreadManager */
private static $instance = null;
public static function init(){
self::$instance = new ThreadManager();
}
/**
* @return ThreadManager
*/
public static function getInstance(){
return self::$instance;
}
/**
* @param Worker|Thread $thread
*/
public function add($thread){
if($thread instanceof Thread or $thread instanceof Worker){
$this->{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;
}
}

33
src/pocketmine/Worker.php Normal file
View File

@ -0,0 +1,33 @@
<?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/
*
*
*/
namespace pocketmine;
/**
* This class must be extended by all custom threading classes
*/
abstract class Worker extends \Worker{
public final function start($options = PTHREADS_INHERIT_ALL){
ThreadManager::getInstance()->add($this);
return parent::start($options & ~PTHREADS_INHERIT_CLASSES);
}
}

View File

@ -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(){

View File

@ -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){

View File

@ -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();