ServerKiller: fixed start/stop race condition

in some cases the main thread was trying to signal the server killer to stop before it was even started due to limited resources available for scheduling.
This commit is contained in:
Dylan K. Taylor 2018-05-09 20:59:56 +01:00
parent 753ed3801d
commit 126a97b405

View File

@ -29,6 +29,9 @@ class ServerKiller extends Thread{
public $time;
/** @var bool */
private $stopped = false;
public function __construct($time = 15){
$this->time = $time;
}
@ -37,7 +40,9 @@ class ServerKiller extends Thread{
$this->registerClassLoader();
$start = time();
$this->synchronized(function(){
$this->wait($this->time * 1000000);
if(!$this->stopped){
$this->wait($this->time * 1000000);
}
});
if(time() - $start >= $this->time){
echo "\nTook too long to stop, server was killed forcefully!\n";
@ -45,6 +50,14 @@ class ServerKiller extends Thread{
}
}
public function quit() : void{
$this->synchronized(function() : void{
$this->stopped = true;
$this->notify();
});
parent::quit();
}
public function getThreadName() : string{
return "Server Killer";
}