"close together" is not race free. I don't think you stand to gain anything by mixing distinct responsibilities. There's a function for getting the directory name, and there's a function for locking the lock file. And all is good.