myanmarking
11/22/2023, 11:26 AMmyanmarking
11/22/2023, 11:26 AMDaniel Pitts
11/22/2023, 4:45 PMxoangon
11/22/2023, 8:58 PMCoroutineScope
as the suspend code. That will allow you to use all the power of APIs lile Mutex::withLock
myanmarking
11/22/2023, 9:07 PMToby
11/23/2023, 4:40 AMToby
11/23/2023, 4:41 AMxoangon
11/23/2023, 8:48 AMI need to halt some method from returning until an internal condition is metIt doesn't seem that you need synchronisation or
Mutex
. What I understand from your saying here is that you need sequential code (you are talking about one method waiting, not concurrent processes trying to read the same thing)
If sequential code is what you need, then just convert the regular non-suspend function into a suspend
function. Coroutines execute the code sequentially by defaultmyanmarking
11/23/2023, 9:36 AMfun onSurfaceDestroyed(){
}
suspend fun loop(){
while(running){
...
...
canvas.lockCanvas
..
canvas.unlockCanvas
yield()
}
}
The onSurfaceDestroyed callback is called by the system, and dictates that, after that method returns, you can no longer use the canvas. So, since the game loop runs in a background coroutine(for obvious reasons), you need to make sure that onSurfaceDestroyed cannot return between lock and unlock canvas.myanmarking
11/23/2023, 9:38 AMmyanmarking
11/23/2023, 9:40 AMxoangon
11/23/2023, 10:50 AMonSurfaceDestroyed
) but not being that the case, then I'd use a runBlocking
I still not see the necessity of Mutex
or synchronized
from the code above. There's only an async process, but there's not concurrencymyanmarking
11/23/2023, 10:52 AMxoangon
11/23/2023, 10:56 AMCoroutineScope
using the CoroutineScope()
factory and then using Mutex
?Daniel Pitts
11/23/2023, 5:09 PMfun onSurfaceDestroyed() = runBlocking {
mutex.withLock {
running = false
}
}
suspend fun loop() {
while (true) {
mutex.withLock {
if (!running) return
canvas.lockCanvas
...
canvas.unlockCanvas
}
}
}
Daniel Pitts
11/23/2023, 5:13 PMmyanmarking
11/24/2023, 4:25 PMDaniel Pitts
11/24/2023, 4:26 PMmyanmarking
11/24/2023, 4:27 PMmyanmarking
11/24/2023, 4:40 PMDaniel Pitts
11/24/2023, 5:22 PM