Fixed #1882 Race-condition on multiple world generation, causing lock of the geneartion thread

This commit is contained in:
Shoghi Cervantes 2014-08-16 11:18:26 +02:00
parent a2e231101e
commit de6f5309dc

View File

@ -104,7 +104,8 @@ class GenerationManager{
/** @var \SplQueue */ /** @var \SplQueue */
protected $requestQueue; protected $requestQueue;
protected $needsChunk = null; /** @var array */
protected $needsChunk = [];
protected $shutdown = false; protected $shutdown = false;
@ -174,9 +175,9 @@ class GenerationManager{
} }
protected function receiveChunk($levelID, FullChunk $chunk){ protected function receiveChunk($levelID, FullChunk $chunk){
if($this->needsChunk !== null and $this->needsChunk[0] === $levelID){ if($this->needsChunk[$levelID] !== null){
if($this->needsChunk[1] === $chunk->getX() and $this->needsChunk[2] === $chunk->getZ()){ if($this->needsChunk[$levelID][0] === $chunk->getX() and $this->needsChunk[$levelID][1] === $chunk->getZ()){
$this->needsChunk = $chunk; $this->needsChunk[$levelID] = $chunk;
} }
} }
//TODO: set new received chunks //TODO: set new received chunks
@ -190,15 +191,16 @@ class GenerationManager{
* @return FullChunk * @return FullChunk
*/ */
public function requestChunk($levelID, $chunkX, $chunkZ){ public function requestChunk($levelID, $chunkX, $chunkZ){
$this->needsChunk = [$levelID, $chunkX, $chunkZ]; $this->needsChunk[$levelID] = [$chunkX, $chunkZ];
$binary = chr(self::PACKET_REQUEST_CHUNK) . Binary::writeInt($levelID) . Binary::writeInt($chunkX) . Binary::writeInt($chunkZ); $binary = chr(self::PACKET_REQUEST_CHUNK) . Binary::writeInt($levelID) . Binary::writeInt($chunkX) . Binary::writeInt($chunkZ);
@socket_write($this->socket, Binary::writeInt(strlen($binary)) . $binary); @socket_write($this->socket, Binary::writeInt(strlen($binary)) . $binary);
do{ do{
$this->readPacket(); $this->readPacket();
}while($this->shutdown !== true and !($this->needsChunk instanceof FullChunk)); }while($this->shutdown !== true and !($this->needsChunk[$levelID] instanceof FullChunk));
$chunk = $this->needsChunk; $chunk = $this->needsChunk[$levelID];
$this->needsChunk = null; $this->needsChunk[$levelID] = null;
if($chunk instanceof FullChunk){ if($chunk instanceof FullChunk){
return $chunk; return $chunk;
}else{ }else{
@ -229,6 +231,7 @@ class GenerationManager{
} }
$packet = $this->socketRead($len); $packet = $this->socketRead($len);
$pid = ord($packet{0}); $pid = ord($packet{0});
$offset = 1; $offset = 1;
if($pid === self::PACKET_REQUEST_CHUNK){ if($pid === self::PACKET_REQUEST_CHUNK){