On Tue, Jan 06, 2004 at 05:52:03 -0000, jnf wrote: > Hello, > > I have a function that adds itself to a wait queue and waits for an event > to wake it up, then on any (fatal) error condition or when it finishes, it > needs to readd itself to the wait queue and wait for the same event to > happen again later. What I am doing right now is just adding to myself to > the wait queue first thing in the function, then calling myself > recursivley where necessary, but as i understand it, the stack space in > the kernel is somewhat limited (anyone know exactly what the limits are?) > and it seems to me that a function that never returns will net call ret, > and thus each time it calls itself the stack will be decremented to make > room for the new frame (assuming it all works the same in the kernel as it > does in user space), and thus over a long enough time line I should run > out of stack space? Are my thoughts there correct, and what would be a > better way to accomplish the same thing? Yes, your thoughts are correct. What you need to do is a "tail call elimination". You have two ways to accomplish it: * Manualy: this will surely work. Add a label to the first statement of the function and instead of recursive call, set appropriate variables and goto that label. * Automaticaly: I recently saw an option in gcc, for which the documentation claimed it enables a tail-call elimination in simple cases (this is a simple case). It's somewhere among the optimization options (I don't recall the exact name). Just remember to do the recursion as "return the_function(the_arguments...)" so that gcc can recognize it's a tail-call. I would not rely on this method actualy working however (but it might be fun to try it out). ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz> -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/