Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save state and return to a deep C function?

Tags:

c

emscripten

Background

I am porting an existing C program to work as an online game using Emscripten.

The problem is that Emscripten wants the program to be organised around a single function that is called 60 times a second. This is okay for the main game loop, except that there are quite a few places where the code displays a set of options and then waits for a key to be pressed to select the option. This is expressed as a function deep in a calling hierarchy using getch() to wait for a keypress. I find it hard to see how to fit this into the required Emscripten style of a function that runs and then completes.

Question

When the code has called a function, which has called a function, which has called a function, is there an easy way of saving the entire state of the callstack so that I can return to the same place at a later time?

What I've tried

  1. The approach I am currently using is to set a global state variable to indicate my current location and to write all the things on the stack that seem important into static variables. I then return from all the functions. To reenter I use the global variable to decide which function to call and which variables to reload from saved data. However, this involves writing a lot of extra code and is very error-prone.

  2. I wondered about using a thread for the game logic and just sending messages from the GUI thread, but the current thread API inside Emscripten seems to require me to try and copy all of the game data into a message so this feels like a lot more work for little benefit.

  3. Emscripten supports setjmp/longjmp but as far as I understand, this only does half the job. I think I can use a longjmp to simply return from a deep function back to the upper level, but I don't see anyway that I can use it to later go back to where I was.

Any better ideas on how I can do this?

like image 228
Peter de Rivaz Avatar asked Nov 23 '14 13:11

Peter de Rivaz


People also ask

What is function argument and return in C?

C function argument and return values. A function in C can be called either with arguments or without arguments. These function may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values.

What is function declaration in C programming?

Declaration: the function's name, return type, and parameters (if any) For code optimization, it is recommended to separate the declaration and the definition of the function. You will often see C programs that have function declaration above main (), and function definition below main ().

What happens when a function does not return a value?

Similarly when it does not return a value, the calling function does not receive any data from the called function. Function with arguments but no return value : When a function has arguments, it receive any data from the calling function but it returns no values.


1 Answers

you cannot return from a callstack and re-enter it again. You can only make deeper calls to still be able to return to the current state. Once the function returns, the same stack (same physical memory locations) is reused for the following calls, and values get overwritten.

I don't know Emscripten; could a getch() wrapper recursively drive the loop until a key is pressed?

setjmp/longjmp saves the stack offset, but not the values on the stack. It's only useful for popping multiple frames off the stack; it's the closest C comes to a thrown exception.

like image 198
Andras Avatar answered Oct 05 '22 03:10

Andras