Skip to content

Commit

Permalink
fix: Avoid slowing down mutex when close
Browse files Browse the repository at this point in the history
  • Loading branch information
Distractic committed Dec 18, 2023
1 parent 9bad411 commit 7828409
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
15 changes: 4 additions & 11 deletions src/main/kotlin/com/github/rushyverse/api/gui/LocaleGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.job
import kotlinx.coroutines.plus
import kotlinx.coroutines.sync.withLock
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.plugin.Plugin

Expand Down Expand Up @@ -39,15 +38,9 @@ public abstract class LocaleGUI(
}

override suspend fun close(client: Client, closeInventory: Boolean): Boolean {
if (!closeInventory) {
return false
}

return mutex.withLock {
if (unsafeContains(client)) {
client.player?.closeInventory(InventoryCloseEvent.Reason.PLUGIN)
true
} else false
}
return if (closeInventory && contains(client)) {
client.player?.closeInventory(InventoryCloseEvent.Reason.PLUGIN)
true
} else false
}
}
19 changes: 11 additions & 8 deletions src/main/kotlin/com/github/rushyverse/api/gui/PlayerGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ public abstract class PlayerGUI(
}

override suspend fun close(client: Client, closeInventory: Boolean): Boolean {
return mutex.withLock { inventories.remove(client) }?.run {
job.cancel(GUIClosedForClientException(client))
job.join()
if (closeInventory) {
inventory.close()
}
true
} == true
val (inventory, job) = mutex.withLock { inventories.remove(client) } ?: return false

job.cancel(GUIClosedForClientException(client))
job.join()

if (closeInventory) {
// Call out of the lock to avoid slowing down the mutex.
inventory.close()
}

return true
}
}
15 changes: 4 additions & 11 deletions src/main/kotlin/com/github/rushyverse/api/gui/SingleGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.job
import kotlinx.coroutines.plus
import kotlinx.coroutines.sync.withLock
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.plugin.Plugin

Expand Down Expand Up @@ -40,16 +39,10 @@ public abstract class SingleGUI(
}

override suspend fun close(client: Client, closeInventory: Boolean): Boolean {
if (!closeInventory) {
return false
}

return mutex.withLock {
if (unsafeContains(client)) {
client.player?.closeInventory(InventoryCloseEvent.Reason.PLUGIN)
true
} else false
}
return if (closeInventory && contains(client)) {
client.player?.closeInventory(InventoryCloseEvent.Reason.PLUGIN)
true
} else false
}

override suspend fun getKey(client: Client): Any {
Expand Down

0 comments on commit 7828409

Please sign in to comment.