Your pseudo-algorithm is dubious:
name = generate_cache_name(request);
top:
if (!exists(name)) {
if (lock_cache(name)) {
generate_cache(request, name);
unlock_cache(name);
} else {
sched_yield();
goto top;
}
} else if (expired(name)) {
if (lock_cache(name)) {
generate_cache(request, name);
unlock_cache(name);
}
}
print_file(name);
You really should have:
if (!exists) {
if (!lock)
delay-and-repeat;
/* RETEST exists _after_ getting the lock */
if (!exists) {
generate into lock-file
mv lockfile exists;
} else {
rm lockfile
}
}
because you really want to re-check the existence after you got the lock,
otherwise you would race with somebody else that got the lock, generated
the data, and then unlocked (and you got the lock _after_ the data was
generated, so now you generate it unnecessarily).
As a side note: how do you release your caches?
Linus
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html