Thread: setjmp/longjmp
View Single Post
  #3   Spotlight this post!  
Unread 23-01-2007, 11:19
stevethetinker stevethetinker is offline
Registered User
FRC #1288 (RAVEN)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2004
Location: St. Charles, MO
Posts: 35
stevethetinker is an unknown quantity at this point
Re: setjmp/longjmp

I want to use them to implement coroutines. The state machines in camera.c and camera_menu.c can get rather cumbersome if something needs to be added, for example submenus. With setjmp and longjmp it is possible to allocate a bit of stack to each "process" and write such a program straight rather than with states and a switch statement. Each process would give up the cpu at appropriate points (roughly corresponding to the end of each state's processing in the FSM) by calling a give-up function, and on the next cycle each process is resumed for its next section using longjmp. I did this once on a VAX, and it worked fairly well, but how to do it is not readily apparent on the FRC. (The VAX C runtime's setjmp and longjmp did not let me tinker with the stack pointer, so I had to write my own setjmp and longjmp functions).