(to the others, who read my walls of texts recently: nothing new here)
i dont use the c api, but i know nothing that would really change the game there. so i know about 2 approaches to break a loop at will. the 1st one is like an event loop, where u can continuously check a dedicated loop condition just for this purpose, so u can safely exit via setting that from within the loop, or maybe with an interrupt handler. the 2nd is to put your loop into pcall() or xpcall() and call error() (or trigger any kinda error) and then u will continue right after the pcall().
at 1st, to not shoot my leg during development (live-coding), i wrote things like:
while whateverCondition and 0<hp do
so simply thats the basic idea, and then, in a function thats executed inside xpcall(), i could figure out a way to leave behind that hp boilerplate with 3 components, and now i write simply brk() right after the beginning of the loop, that gets the current line via the debug library, and uses that as an identifier (or whatever i give it), and then it makes the countdown on its own, and if its over, then it will calls error(), otherwise if the function reaches its end, then i will delete all the counters after the xpcall(). the rest is upon ur imagination, and u can use a counter, a signal, a timer, a variable, a file, an environment variable or whatever to trigger it...
btw, as i mentioned it, i wrote about this a few days ago, and u can search lua-l for "brk", nobody else wrote it after me if im right :D
ahh, sorry, i just realized that u only wanna suspend it... then i think its a matter of a callback that u change at will, but if its based on a signal, then i think others gonna bring up better ideas than whatever i could... :D
On 26.01.20 21:28, Levente Kovacs wrote:
> Is there any way in my C application to interrupt the script, do something,
> and continue with the script?
you might want to have a look at my "mtint" module, see
https://github.com/osch/lua-mtint#mtint. This might not fit 100% your use case,
since it is meant to be a solution used from pure Lua and not from C
applications. But perhaps it might be useful somehow.
Your email inspired me to extend the "mtint" module to also allow installation
of an interrupt handler function that makes it possible to continue the
With "mtint" it is possible to interrupt another thread or coroutine from a
concurrently running thread. This can be useful for applications with
interactive user interface when a user wants to abort a long running background
task or a user supplied script that is stuck in an infinite loop.
See Example 3 (https://github.com/osch/lua-mtint#example-3): here a
"computation" background thread is started. The background thread registers an
interrupt handler function and gives its interruptible id back to the main
thread. Then the computation is started and runs in an infinite loop. The main
thread then interrupts the computation thread several times by using the
interuptible id and communicates with the interrupt handler function to give
commands to continue or to quit the computation.
Just throwing it out there. I had written a module LuaStepper (
https://github.com/aryajur/LuaStepper ) with which you can load Lua Scripts, execute them concurrently, pause them and resume them. It works by setting hooks in the scripts so doesn't depend on any operating system threading. As mentioned before in this thread that the hook approach won't work if the script calls a blocking C function. To avoid that there are some mechanisms in the module to wrap any external code required by the script and then provide it to the executing script to control blocking behavior.
On Mon, Jan 27, 2020 at 11:58 AM Levente Kovacs <[hidden email]> wrote:
Thanks for the answers. I think the method is not straight forward, so I
won't implement it.