using setjmp and longjmp

joe_her wrote on Sunday, October 16, 2016:

Is it OK to use longjmp inside a task?
What if I have an error detected deeply nested, and I want to jump to a “clean” location in the task ( for example some error in communication hardware, needs to restart it, etc.)
Thanks

rtel wrote on Sunday, October 16, 2016:

I have to be honest and say I have no idea, although I would suspect not
unless you can find some way of making it thread aware. I have used
these functions to jump from a running FreeRTOS application back to a
host environment, but not within a task.

richard_damon wrote on Sunday, October 16, 2016:

My guess is that a setjmp/longjmp within a single task will likely work (and between tasks will cause all sorts of problems). setjmp/longjmp shouldn’t have thread issue themselves, as all the information should be in the jmpbuf, but they would have no knowledge of how to change threads.

happygimp0 wrote on Friday, February 17, 2017:

Possible Problem with Microcontrollers which need more then a single instruction to manipulate the stack pointer:
In longjmp() you change the stack pointer, while you change it, a scheduler interrupt can happen, scheduler want to store the current registers on stack, the stack pointer points on a more or less random location and the push commands rewrite some random locations.
Such a bug will be very hard to find, it is very unlikely that the timer interrupt is exactly at this moment and it is possible that you overwrite always other places.