Replies: 1 comment 4 replies
-
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm currently developing a python module in C and I need to implement a custom
goto
function. Once goto is called, the respective function shall be called and it's expected that the control flow never comes back to the calling location. If there's nogoto
in a function, the program shall terminate. To not fill up the stack, I came up with a "dispatcher" concept.In the example code,
run
implements the dispatcher and executes themy_func_1
. When agoto
is encountered, the function to go to is stored in a variable and a custom exception is raised. The exception is handled inrun
by calling the desired function.The output of the program above would be
1 a X
.my_func_3
doesn't set a goto and therefore therun
function finishes and the program ends.There's a thread in the old forum talking about this and the suggestion is also to use exceptions. However it's not addressing the problem described in the next chapter.
The Problem: IRQs/Callbacks
Now comes the problem: I want to have callbacks to respond to events. Callbacks being functions executed from another thread than the thread which runs the python "main loop". And these callbacks shall also be able to use
goto
and alter the control flow of the program.Here's the example from above expanded by an event registration and callback function:
The expected output of this program would be
1 a X
. Note that - as before - at no point we're returning tomy_func_1
orbutton_func
. The control flow shall be completely altered.Idea 1: Exception in a custom ISR
One idea to solve this was to implement a custom IRQ handler (similar to
mp_irq_handler
) to store the function to go to and throw an exception. The problem is that exceptions from ISRs are not handed to the main loop.I assume there's no way to be able to raise exceptions in ISRs and handle them in the main loop (due to register manipulation I assume?!)?
Idea 2: Similar to AsyncIO
Similar to Idea 1, the ISR stores the function to go to but it doesn't do more. The other functions of my module all check if a
goto
is needed and then throw the exception. Meaning: Exceptions are only thrown in the main loop and therefore can be handled byrun
. This is similar to asyncIO I think (e.g. it also only aborts tasks on async functions and not builtins etc.)I already implemented this approach and it works. However obviously the implication is that builtins or other module's functions get executed until encountering one of the
mymod
functions.The output of the program above would be
1 2 a X
(instead of the desired1 a X
).Idea 3: Hacking the runtime
Of course "hacking" the runtime is a possibility but I wouldn't be in favor of that (and I'd have no idea about where to start).
The Question: Other ideas?
Is there any other approach you could think of to solve this problem? Or any way of tweaking Idea 1 to meet the desired behavior?
Beta Was this translation helpful? Give feedback.
All reactions