Skip to content

Commit

Permalink
refactor(mps-sync-plugin): use Mutex instead of Semaphore(1)
Browse files Browse the repository at this point in the history
  • Loading branch information
benedekh committed Apr 22, 2024
1 parent 1fdcf25 commit fefec27
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.intellij.ui.content.ContentFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.Mutex
import mu.KotlinLogging
import org.modelix.kotlin.utils.UnstableModelixFeature
import org.modelix.model.api.BuiltinLanguages
Expand Down Expand Up @@ -88,7 +88,7 @@ class ModelSyncGuiFactory : ToolWindowFactory, Disposable {

private val logger = KotlinLogging.logger {}
private val coroutineScope = CoroutineScope(Dispatchers.Default)
private val mutex = Semaphore(1)
private val mutex = Mutex()

val contentPanel = JPanel()
val bindingsRefresher: BindingsComboBoxRefresher
Expand Down Expand Up @@ -346,15 +346,15 @@ class ModelSyncGuiFactory : ToolWindowFactory, Disposable {

private fun callOnlyIfNotFetching(action: suspend () -> Unit) {
coroutineScope.launch {
if (mutex.tryAcquire()) {
if (mutex.tryLock()) {
try {
setUiControlsEnabled(false)
action()
} catch (ex: Exception) {
logger.error(ex) { "Failed to fetch data from server." }
} finally {
setUiControlsEnabled(true)
mutex.release()
mutex.unlock()
}
}
}
Expand Down

0 comments on commit fefec27

Please sign in to comment.