Note that if this were C++ (11 or later) this could be done with the static variable in a thread-safe way, since function-scoped static variables initialized by functions are guaranteed to be initialized exactly once. So this code could be something like:
int set_the_mtime(...) {
static int starting_from = set_mtime_starting_from(0);
set_mtime_starting_from(starting_from);
}
// Try various ways of setting mtime until it works.
int set_mtime_starting_from(int starting_from) {
// horrible switch statement
}