[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Is there a way to pause lua_pcall() and resume later?
- From: Andreas Falkenhahn <andreas@...>
- Date: Mon, 12 Aug 2019 15:16:50 +0200
I'm wondering if there is a way to pause script execution during lua_pcall() and resume it later at the paused position?
Here's the background of my problem: I have created a wrapper for some Android UI functions so that they can be called from a Lua script, e.g.
Now when the user taps the button, the Java code makes a JNI call to C code which then calls lua_pcall() to run a Lua function handling the respective event. This all works fine so far except when it comes to functions that use a modal loop to block until the user has responded, e.g.
showMessageBox("Important message for you, please acknowledge before we go on")
Since Android doesn't have the concept of modal loops, lua_pcall() needs to return control to the UI thread immediately even though the user hasn't closed the message box created by showMessageBox() yet. So when entering my showMessageBox() implementation I'd somehow need to tell lua_pcall() to pause its execution and resume it later when the user has closed the message box. Is that possible somehow?
I have also thought about circumventing the problem by having Lua run on its own thread but that won't work because it will call into functions which need to be run on the UI thread and delegating them to the UI thread isn't an option because that will lead to a deadlock in the following situation:
- UI thread receives a "button clicked" event and tells the Lua thread to run lua_pcall()
- the code that is run by lua_pcall() on the Lua thread now could then run functions like setLabel() to change the label of a button widget but this will deadlock now because setLabel() can only be called on the UI thread so the Lua thread will be waiting for the UI thread while the UI thread will be waiting for the Lua thread... deadlock!
So the only option I see here is to find a way to pause lua_pcall() and resume it later to solve the modal loop problem. That feels like a somewhat ugly solution but I don't see any other way. So is that possible somehow or would it have to be hacked into the VM?
Thanks for ideas!
Andreas Falkenhahn mailto:email@example.com