Lets say I have these Kotlin suspend functions doing various operations:
suspend fun operation1(){ //some code }
suspend fun operation2(){ //some code }
suspend fun operation3(){ //some code }
suspend fun operation4(){ //some code }
suspend fun operation5(){ //some code }
I have a function which gets called by an external library (which we can't modify), where I make calls to these functions (of course its logic is more complex in practice). Let's assume the library can make any number of calls in a short time.
fun calledByLibrary(someParam: String){
GlobalScope.launch{
when(someParam){
"someValue1" -> operation1()
"someValue2" -> operation2()
"someValue3" -> operation3()
"someValue4" -> operation4()
"someValue5" -> operation5()
}
}
}
Now, we had a bug, and figured that operation3 and operation4 can not be suspended to run in 'parallel'. They even need to be executed in the same order as called by the external library. For all the other combinations it's ok to do that, so we would like to keep them as suspend to support this.
What's the best way to solve this?
One simple, and easy way would be to use a Mutex in operation3 and operation4 to ensure they don't run in the same time, like this:
val mutex = Mutex()
suspend fun operation3(){ mutex.withLock(){/*some code*/} }
suspend fun operation4(){ mutex.withLock(){/*some code*/} }
But this does not guarantee that they will be executed in the order they have been requested by the library.
Also tried searching for running coroutines in a queue, but surprisingly did not find anything trivial. (Current solution is using a single threaded dispatcher and runBlocking, which is far from ideal)