World: fixed population timer sometimes not being stopped

This commit is contained in:
Dylan K. Taylor 2023-02-21 18:31:33 +00:00
parent b574d49d36
commit efdd7a186d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -3118,59 +3118,63 @@ class World implements ChunkManager{
Timings::$population->startTiming(); Timings::$population->startTiming();
for($xx = -1; $xx <= 1; ++$xx){ try{
for($zz = -1; $zz <= 1; ++$zz){ for($xx = -1; $xx <= 1; ++$xx){
if($this->isChunkLocked($chunkX + $xx, $chunkZ + $zz)){ for($zz = -1; $zz <= 1; ++$zz){
//chunk is already in use by another generation request; queue the request for later if($this->isChunkLocked($chunkX + $xx, $chunkZ + $zz)){
return $resolver?->getPromise() ?? $this->enqueuePopulationRequest($chunkX, $chunkZ, $associatedChunkLoader); //chunk is already in use by another generation request; queue the request for later
return $resolver?->getPromise() ?? $this->enqueuePopulationRequest($chunkX, $chunkZ, $associatedChunkLoader);
}
} }
} }
}
$this->activeChunkPopulationTasks[$chunkHash] = true; $this->activeChunkPopulationTasks[$chunkHash] = true;
if($resolver === null){ if($resolver === null){
$resolver = new PromiseResolver(); $resolver = new PromiseResolver();
$this->chunkPopulationRequestMap[$chunkHash] = $resolver; $this->chunkPopulationRequestMap[$chunkHash] = $resolver;
}
$chunkPopulationLockId = new ChunkLockId();
$temporaryChunkLoader = new class implements ChunkLoader{};
for($xx = -1; $xx <= 1; ++$xx){
for($zz = -1; $zz <= 1; ++$zz){
$this->lockChunk($chunkX + $xx, $chunkZ + $zz, $chunkPopulationLockId);
$this->registerChunkLoader($temporaryChunkLoader, $chunkX + $xx, $chunkZ + $zz);
} }
}
$centerChunk = $this->loadChunk($chunkX, $chunkZ); $chunkPopulationLockId = new ChunkLockId();
$adjacentChunks = $this->getAdjacentChunks($chunkX, $chunkZ);
$task = new PopulationTask( $temporaryChunkLoader = new class implements ChunkLoader{
$this->worldId, };
$chunkX, for($xx = -1; $xx <= 1; ++$xx){
$chunkZ, for($zz = -1; $zz <= 1; ++$zz){
$centerChunk, $this->lockChunk($chunkX + $xx, $chunkZ + $zz, $chunkPopulationLockId);
$adjacentChunks, $this->registerChunkLoader($temporaryChunkLoader, $chunkX + $xx, $chunkZ + $zz);
function(Chunk $centerChunk, array $adjacentChunks) use ($chunkPopulationLockId, $chunkX, $chunkZ, $temporaryChunkLoader) : void{
if(!$this->isLoaded()){
return;
} }
$this->generateChunkCallback($chunkPopulationLockId, $chunkX, $chunkZ, $centerChunk, $adjacentChunks, $temporaryChunkLoader);
} }
);
$workerId = $this->workerPool->selectWorker();
if(!isset($this->workerPool->getRunningWorkers()[$workerId]) && isset($this->generatorRegisteredWorkers[$workerId])){
$this->logger->debug("Selected worker $workerId previously had generator registered, but is now offline");
unset($this->generatorRegisteredWorkers[$workerId]);
}
if(!isset($this->generatorRegisteredWorkers[$workerId])){
$this->registerGeneratorToWorker($workerId);
}
$this->workerPool->submitTaskToWorker($task, $workerId);
Timings::$population->stopTiming(); $centerChunk = $this->loadChunk($chunkX, $chunkZ);
return $resolver->getPromise(); $adjacentChunks = $this->getAdjacentChunks($chunkX, $chunkZ);
$task = new PopulationTask(
$this->worldId,
$chunkX,
$chunkZ,
$centerChunk,
$adjacentChunks,
function(Chunk $centerChunk, array $adjacentChunks) use ($chunkPopulationLockId, $chunkX, $chunkZ, $temporaryChunkLoader) : void{
if(!$this->isLoaded()){
return;
}
$this->generateChunkCallback($chunkPopulationLockId, $chunkX, $chunkZ, $centerChunk, $adjacentChunks, $temporaryChunkLoader);
}
);
$workerId = $this->workerPool->selectWorker();
if(!isset($this->workerPool->getRunningWorkers()[$workerId]) && isset($this->generatorRegisteredWorkers[$workerId])){
$this->logger->debug("Selected worker $workerId previously had generator registered, but is now offline");
unset($this->generatorRegisteredWorkers[$workerId]);
}
if(!isset($this->generatorRegisteredWorkers[$workerId])){
$this->registerGeneratorToWorker($workerId);
}
$this->workerPool->submitTaskToWorker($task, $workerId);
return $resolver->getPromise();
}finally{
Timings::$population->stopTiming();
}
} }
/** /**