On Wed, Jan 07, 2004 at 07:20:53 -0000, jnf wrote: > > > 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). > > ok assuming i did it manually, with a goto statement, i assume the code > would look like this: > > function(...) { > label: > interruptible_sleep(&wq) // cant think of the function name off the top of > my head > do_stuff(); > if(fatal_error) > goto label; > done(); > goto label; > } > > my question is, how would I kill this function when I remove the module? > will the module remove functions in the kernel automatically clean up > after me? If the function never finishes, how would I stop it when i > removed the module, is my quesition. Module can be unloaded, when it has zero reference count. The reference count is updated mostly automaticaly when linking entry-points to your module, but you need to at least make sure it really is in your case (see also MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT). When a module is unloaded, it simply removes itself from memory. So if there are pointers to it left, the kernel will crash horribly. It is your responsibility to properly unregister your module. Now, you have to stop your function. You need to use the wait_event[_interruptible] macro (or something like that). You will have to check for the event you are waiting for AND for an exit event and do return on the exit event. You will also have to wait for the function to actualy return, before you can unload your module. If the function runs in kerel thread, you have to use complete_and_exit() function. If the function runs from a user-land process via some gateway, it's enough to make sure this gateway properly increments your use count (it must be done in core code, it won't work if done in your own code). I really don't know what is the correct procedure for handling function running on task-queues (but this does not seem to be the case). > Also thank you for the terminology, googling around gave me a plethora of > information on this subject and helped ease my ego by realizing im not the > first person to waste cpu cycles pondering this. > > thanks > jnf > ------------------------------------------------------------------------------- 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/