EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

goto across functions/isrs?

Started by galapogos December 12, 2006
Hi,
Is it possible to goto a label that's not in the same functions? I've
always been taught that gotos are evil so I don't have much experience
with it, but I'm currently in a situation where I think I might have to
use it.

I have an ISR that triggers when I detect an event, and based on that
event I want to unconditionally goto a label that's in main(). Is that
possible? If so what's the syntax?

Thanks.

Hi,
Is it possible to goto a label that's not in the same functions? I've
always been taught that gotos are evil so I don't have much experience
with it, but I'm currently in a situation where I think I might have to
use it.

I have an ISR that triggers when I detect an event, and based on that
event I want to unconditionally goto a label that's in main(). Is that
possible? If so what's the syntax?

Thanks.

galapogos wrote:
> Hi, > Is it possible to goto a label that's not in the same functions? I've > always been taught that gotos are evil so I don't have much experience > with it, but I'm currently in a situation where I think I might have to > use it. > > I have an ISR that triggers when I detect an event, and based on that > event I want to unconditionally goto a label that's in main(). Is that > possible? If so what's the syntax? > > Thanks.
IMHO you can't call it as compiler will report "label 'Your_Label_Name' was undefined". I don't know what compiler you ar using but it should report more or less the same thing. ali

galapogos wrote:

> Hi, > Is it possible to goto a label that's not in the same functions?
Yes. I've
> always been taught that gotos are evil
No. so I don't have much experience
> with it,
I can see that. but I'm currently in a situation where I think I might have to
> use it.
No.
> I have an ISR that triggers when I detect an event, and based on that > event I want to unconditionally goto a label that's in main(). Is that > possible? If so what's the syntax?
longjmp() However what are you trying to do does not make much sense. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Vladimir Vassilevsky wrote:
> galapogos wrote: > > > Hi, > > Is it possible to goto a label that's not in the same functions? > > Yes. > > I've > > always been taught that gotos are evil > > No. > > so I don't have much experience > > with it, > > I can see that. > > but I'm currently in a situation where I think I might have to > > use it. > > No. > > > > I have an ISR that triggers when I detect an event, and based on that > > event I want to unconditionally goto a label that's in main(). Is that > > possible? If so what's the syntax? > > longjmp() > > However what are you trying to do does not make much sense.
OK what I'm trying to do is as follows. I have an isr that is called every 20ms that checks if something is connected. If so it changes a global state variable to "connected". In my main() I have a loop that checks if the device is connected. If it's not it keeps looping doing nothing. If it is, then it will start to talk to the device. This works fine if the device is connected and stays connected, but it doesn't work so well if a stupid user decides to suddenly disconnect the device halfway. What I'm trying to do is that on disconnection detection in my ISR, I can unconditionally jump to the beginning of the main() loop where it does the state checking so that my main() doesn't continue to try and talk to the device only to suddenly find it not there and getting all sorts of weird errors. If I don't use goto, I'd have to check the state of the connection at multiple locations within the loop(basically at every instruction/command that I try to send to the device), which is quite a hassle I think.
galapogos wrote:
> Vladimir Vassilevsky wrote: > > galapogos wrote: > > > > > Hi, > > > Is it possible to goto a label that's not in the same functions? > > > > Yes. > > > > I've > > > always been taught that gotos are evil > > > > No. > > > > so I don't have much experience > > > with it, > > > > I can see that. > > > > but I'm currently in a situation where I think I might have to > > > use it. > > > > No. > > > > > > > I have an ISR that triggers when I detect an event, and based on that > > > event I want to unconditionally goto a label that's in main(). Is that > > > possible? If so what's the syntax? > > > > longjmp() > > > > However what are you trying to do does not make much sense. > > OK what I'm trying to do is as follows. > > I have an isr that is called every 20ms that checks if something is > connected. If so it changes a global state variable to "connected". In > my main() I have a loop that checks if the device is connected. If it's > not it keeps looping doing nothing. If it is, then it will start to > talk to the device. This works fine if the device is connected and > stays connected, but it doesn't work so well if a stupid user decides > to suddenly disconnect the device halfway. What I'm trying to do is > that on disconnection detection in my ISR, I can unconditionally jump > to the beginning of the main() loop where it does the state checking so > that my main() doesn't continue to try and talk to the device only to > suddenly find it not there and getting all sorts of weird errors. If I > don't use goto, I'd have to check the state of the connection at > multiple locations within the loop(basically at every > instruction/command that I try to send to the device), which is quite a > hassle I think.
How about following Pseudo code ? volatile int flag; main(){ HERE: ........ ........ while(1){ Do whatever you want to do but keep pooling the flag variable. if(flag) goto HERE; } } Your_ISR(){ Set the flag variable if you detect dissconnection. } ali

galapogos wrote:

>>>I have an ISR that triggers when I detect an event, and based on that >>>event I want to unconditionally goto a label that's in main(). Is that >>>possible? If so what's the syntax? >> >>longjmp() >> >>However what are you trying to do does not make much sense. > > > OK what I'm trying to do is as follows. > > I have an isr that is called every 20ms that checks if something is > connected. If so it changes a global state variable to "connected". In > my main() I have a loop that checks if the device is connected. If it's > not it keeps looping doing nothing. If it is, then it will start to > talk to the device. This works fine if the device is connected and > stays connected, but it doesn't work so well if a stupid user decides > to suddenly disconnect the device halfway. What I'm trying to do is > that on disconnection detection in my ISR, I can unconditionally jump > to the beginning of the main() loop where it does the state checking so > that my main() doesn't continue to try and talk to the device only to > suddenly find it not there and getting all sorts of weird errors. If I > don't use goto, I'd have to check the state of the connection at > multiple locations within the loop(basically at every > instruction/command that I try to send to the device), which is quite a > hassle I think. >
Implement the processing function as a thread. Kill the thread if the device gets disconnected. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Neither goto nor longjmp() will do what you want. They don't work like
you think they do.

I can think of two options:

1 - check 'connected' at every opportunity
2 - rewrite the saved PC from main() in the stack

Option 2 is really dangerous and desperate. Don't do it.

Option 1 is, indeed, a PITA, but you probably won't find an easy way
round it. If you do, though, by all means post it here!

-- mpa

ps I can't help myself: "stupid user", who disconnects things at
inopportune moments, is almost certainly going to be me. :)

On Dec 12, 7:45 pm, "galapogos" <gois...@gmail.com> wrote:

> I have an isr that is called every 20ms that checks if something is > connected. If so it changes a global state variable to "connected". In > my main() I have a loop that checks if the device is connected. If it's > not it keeps looping doing nothing. If it is, then it will start to > talk to the device. This works fine if the device is connected and > stays connected, but it doesn't work so well if a stupid user decides > to suddenly disconnect the device halfway. What I'm trying to do is > that on disconnection detection in my ISR, I can unconditionally jump > to the beginning of the main() loop where it does the state checking so > that my main() doesn't continue to try and talk to the device only to > suddenly find it not there and getting all sorts of weird errors. If I > don't use goto, I'd have to check the state of the connection at > multiple locations within the loop(basically at every > instruction/command that I try to send to the device), which is quite a > hassle I think.
On 12 Dec 2006 18:01:38 -0800, "galapogos" <goister@gmail.com> wrote
in comp.arch.embedded:

> Hi, > Is it possible to goto a label that's not in the same functions? I've > always been taught that gotos are evil so I don't have much experience > with it, but I'm currently in a situation where I think I might have to > use it. > > I have an ISR that triggers when I detect an event, and based on that > event I want to unconditionally goto a label that's in main(). Is that > possible? If so what's the syntax? > > Thanks.
NO!!! Even if you could find a C compiler that provided a truly whacked out extension that allowed this, trying to jump from an ISR back into foreground code without going through proper interrupt clean up and an interrupt return would be INSTANTLY and ABSOLUTELY FATAL. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
You can unconditionally branch to any label using inline asm code. (
Few compiler support it).As like gotos it is very poor practice to call
a function from ISR. It is a always advisable to keep ISR very short.
You can use flag polling.

Visweswara R


The 2024 Embedded Online Conference