Fixed #1501 executing AsyncTasks blocks other threads

This commit is contained in:
Shoghi Cervantes 2014-06-22 12:50:24 +02:00
parent e381313747
commit 7f795bc041
5 changed files with 42 additions and 110 deletions

View File

@ -42,7 +42,6 @@ use pocketmine\level\format\LevelProviderManager;
use pocketmine\level\generator\GenerationRequestManager; use pocketmine\level\generator\GenerationRequestManager;
use pocketmine\level\generator\Generator; use pocketmine\level\generator\Generator;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\metadata\EntityMetadataStore; use pocketmine\metadata\EntityMetadataStore;
use pocketmine\metadata\LevelMetadataStore; use pocketmine\metadata\LevelMetadataStore;
use pocketmine\metadata\PlayerMetadataStore; use pocketmine\metadata\PlayerMetadataStore;
@ -69,9 +68,9 @@ use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginLoadOrder; use pocketmine\plugin\PluginLoadOrder;
use pocketmine\plugin\PluginManager; use pocketmine\plugin\PluginManager;
use pocketmine\scheduler\CallbackTask; use pocketmine\scheduler\CallbackTask;
use pocketmine\scheduler\ExampleTask;
use pocketmine\scheduler\SendUsageTask; use pocketmine\scheduler\SendUsageTask;
use pocketmine\scheduler\ServerScheduler; use pocketmine\scheduler\ServerScheduler;
use pocketmine\scheduler\TickScheduler;
use pocketmine\tile\Tile; use pocketmine\tile\Tile;
use pocketmine\updater\AutoUpdater; use pocketmine\updater\AutoUpdater;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
@ -118,8 +117,16 @@ class Server{
/** @var GenerationRequestManager */ /** @var GenerationRequestManager */
private $generationManager = null; private $generationManager = null;
/** @var TickScheduler */ /**
private $tickScheduler = null; * Counts the ticks since the server start
*
* @var int
*/
private $tickCounter;
private $nextTick = 0;
private $tickMeasure = 20;
private $tickTime = 0;
private $inTick = false;
/** @var \Logger */ /** @var \Logger */
private $logger; private $logger;
@ -151,14 +158,6 @@ class Server{
/** @var LevelMetadataStore */ /** @var LevelMetadataStore */
private $levelMetadata; private $levelMetadata;
/**
* Counts the ticks since the server start
*
* @var int
*/
private $tickCounter;
private $inTick = false;
/** @var SourceInterface[] */ /** @var SourceInterface[] */
private $interfaces = []; private $interfaces = [];
@ -525,7 +524,7 @@ class Server{
* @return float * @return float
*/ */
public function getTicksPerSecond(){ public function getTicksPerSecond(){
return $this->tickScheduler->getTPS(); return round((0.05 / $this->tickMeasure) * 20, 2);
} }
/** /**
@ -1323,7 +1322,6 @@ class Server{
$this->banByIP = new BanList($this->dataPath . "banned-ips.txt"); $this->banByIP = new BanList($this->dataPath . "banned-ips.txt");
$this->banByIP->load(); $this->banByIP->load();
$this->tickScheduler = new TickScheduler(20);
$this->scheduler = new ServerScheduler(); $this->scheduler = new ServerScheduler();
$this->console = new CommandReader(); $this->console = new CommandReader();
@ -1640,7 +1638,6 @@ class Server{
$this->properties->save(); $this->properties->save();
$this->tickScheduler->kill();
$this->console->kill(); $this->console->kill();
foreach($this->interfaces as $interface){ foreach($this->interfaces as $interface){
$interface->shutdown(); $interface->shutdown();
@ -1682,6 +1679,7 @@ class Server{
$this->logger->info("Default game type: " . self::getGamemodeString($this->getGamemode())); //TODO: string name $this->logger->info("Default game type: " . self::getGamemodeString($this->getGamemode())); //TODO: string name
$this->logger->info("Done (" . round(microtime(true) - \pocketmine\START_TIME, 3) . 's)! For help, type "help" or "?"'); $this->logger->info("Done (" . round(microtime(true) - \pocketmine\START_TIME, 3) . 's)! For help, type "help" or "?"');
$this->scheduler->scheduleAsyncTask(new ExampleTask());
if(Utils::getOS() === "win"){ //Workaround less usleep() waste if(Utils::getOS() === "win"){ //Workaround less usleep() waste
$this->tickProcessorWindows(); $this->tickProcessorWindows();
}else{ }else{
@ -1929,7 +1927,12 @@ class Server{
* Tries to execute a server tick * Tries to execute a server tick
*/ */
public function tick(){ public function tick(){
if($this->inTick === false and $this->tickScheduler->hasTick()){ if($this->inTick === false){
$tickTime = microtime(true);
if($tickTime < $this->nextTick){
return false;
}
$this->inTick = true; //Fix race conditions $this->inTick = true; //Fix race conditions
++$this->tickCounter; ++$this->tickCounter;
@ -1944,7 +1947,9 @@ class Server{
} }
} }
$this->tickScheduler->doTick(); $this->tickMeasure = (($time = microtime(true)) - $this->tickTime);
$this->tickTime = $time;
$this->nextTick = 0.05 * (0.05 / max(0.05, $this->tickMeasure)) + $time;
$this->inTick = false; $this->inTick = false;
return true; return true;

View File

@ -33,19 +33,30 @@ abstract class AsyncTask extends \Threaded{
private $result; private $result;
public function run(){ public function run(){
$this->lock(); $this->finished = false;
$this->complete = false;
$this->result = null; $this->result = null;
$this->onRun(); $this->onRun();
$this->finished = true; $this->finished = true;
$this->complete = $this->result === null ? true : false; $this->complete = $this->result === null ? true : false;
$this->unlock();
} }
/** /**
* @return bool * @return bool
*/ */
public function isCompleted(){ public function isCompleted(){
return $this->complete === true; return $this->synchronized(function(){
return $this->complete === true;
});
}
/**
* @return bool
*/
public function isFinished(){
return $this->synchronized(function(){
return $this->finished === true;
});
} }
/** /**
@ -73,13 +84,6 @@ abstract class AsyncTask extends \Threaded{
$this->result = @serialize($result); $this->result = @serialize($result);
} }
/**
* @return bool
*/
public function isFinished(){
return $this->finished === true;
}
/** /**
* Actions to execute when run * Actions to execute when run
* *

View File

@ -24,10 +24,13 @@ namespace pocketmine\scheduler;
class AsyncWorker extends \Worker{ class AsyncWorker extends \Worker{
public $path; public $path;
public function start($options = PTHREADS_INHERIT_CLASSES){ public function __construct(){
$this->path = \pocketmine\PATH; $this->path = \pocketmine\PATH;
return parent::start(PTHREADS_INHERIT_ALL & ~PTHREADS_INHERIT_CLASSES);
}
public function start(){
return parent::start($options & ~PTHREADS_INHERIT_CLASSES);
} }
public function run(){ public function run(){

View File

@ -1,80 +0,0 @@
<?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\scheduler;
class TickScheduler extends \Thread{
protected $sleepTime;
protected $ticksPerSecond;
protected $tickMeasure;
public $hasTick;
public function __construct($ticksPerSecond = 20){
$this->ticksPerSecond = (int) $ticksPerSecond;
$this->sleepTime = (int) (1000000 / $this->ticksPerSecond);
$this->tickMeasure = $this->sleepTime;
$this->start(PTHREADS_INHERIT_ALL & ~PTHREADS_INHERIT_CLASSES);
}
/**
* Returns true if clear to run tick
*
* @return bool
*/
public function hasTick(){
return $this->synchronized(function (){
$hasTick = $this->hasTick;
$this->hasTick = false;
return $hasTick === true;
});
}
public function doTick(){
$this->notify();
}
/**
* @return float
*/
public function getTPS(){
return $this->synchronized(function (){
return round(($this->sleepTime / $this->tickMeasure) * $this->ticksPerSecond, 2);
});
}
public function run(){
$tickTime = microtime(true);
$this->tickMeasure = $this->sleepTime;
while(true){
$this->synchronized(function (){
$this->hasTick = true;
$this->wait();
$this->hasTick = false;
});
$this->tickMeasure = (int) ((($time = microtime(true)) - $tickTime) * 1000000);
$tickTime = $time;
$sleepTime = $this->sleepTime * ($this->sleepTime / max($this->sleepTime, $this->tickMeasure));
usleep((int) $sleepTime);
}
}
}