Ensures that a directory exists, and protects against a common race condition when multiple processes try to create the same directory.