I'm trying to add a timeout for a c function in the kernel.
I'd like to do something like the function try_until_timeout of the following pseudocode:
void myfunction() {/* [...] */}
void myfunction_cleanup() {/* [...] */}
int main( void ) {
boolean hasFinished = try_until_timeout(myfunction, HZ/10 ) // The task has 100 ms of processor time to complete
if(!hasFinished) { // if the task have timeouted
hasFinished = try_until_timeout(myfunction_cleanup, HZ/10 ) // The task has 100 ms of processor time to come back to a clean state
if(!hasFinished) { // if the cleanup also timeouted
printk(KERN_WARN "Task and cleanup failed to finish in time. Aborting.");
my_kill()
}
}
}
In some ways my need is somewhat similar to pthread_timedjoin_np except that I'd like to avoid the creation of a new thread for performance reasons.
But I don't know how to do that cleanly in the kernel. I think it might be possible by adding a timer_list before the call of the function and deleting it after. In case the timeout is reached, it might be possible to modify the value of the next instruction $eip but that approach is extremely dirty, overkill and non-portable thus I'd like to find a cleaner solution to achieve the same thing...