Skip to content

Commit

Permalink
fix(Minimap): lock on ArrayBlockingQueue take (#4933)
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 authored Dec 16, 2024
1 parent f64c1d4 commit e5a0a17
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ class MinimapTextureAtlasManager {
private fun allocate(chunkPos: ChunkPos): AtlasPosition {
val atlasPosition = availableAtlasPositions.take() ?: error("No more space in the texture atlas!")

chunkPosAtlasPosMap[chunkPos] = atlasPosition
lock.write {
chunkPosAtlasPosMap[chunkPos] = atlasPosition
}

return atlasPosition
}
Expand Down Expand Up @@ -118,8 +120,10 @@ class MinimapTextureAtlasManager {
chunkPos: ChunkPos,
editor: (NativeImageBackedTexture, AtlasPosition) -> Unit,
) {
val atlasPosition = lock.write {
getOrAllocate(chunkPos).apply(dirtyAtlasPositions::add)
val atlasPosition = getOrAllocate(chunkPos)

lock.write {
dirtyAtlasPositions.add(atlasPosition)
}

editor(texture, atlasPosition)
Expand All @@ -131,7 +135,7 @@ class MinimapTextureAtlasManager {
* @return the GLid of the texture
*/
fun prepareRendering(): Int {
lock.write {
lock.read {
if (this.dirtyAtlasPositions.isEmpty()) {
return this.texture.glId
}
Expand All @@ -144,11 +148,13 @@ class MinimapTextureAtlasManager {
!this.allocated || dirtyChunks >= FULL_UPLOAD_THRESHOLD -> uploadFullTexture()
else -> uploadOnlyDirtyPositions()
}
}

lock.write {
this.dirtyAtlasPositions.clear()

return this.texture.glId
}

return this.texture.glId
}

private fun uploadFullTexture() {
Expand Down

0 comments on commit e5a0a17

Please sign in to comment.