Multiple users need to access the same directory of files using an interface created in Common Lisp. Many race conditions appear when this happens. For example, when more than one user adds or deletes a file with the same time. Is there a way within lisp to "lock" a specific directory while work is being done? This would be a similar concept to the "synchronized" block in a multithreaded environment, but I have separate Lisp instances. I am using Allegro CL on Windows.
Edit: Ideas for a different solution to this problem would also be appreciated.
OS-level
CLISP provides
stream-lockandwith-stream-lockwhich interface tofcntlorLockFileEx. These will lock both open streams and files.You can use FFI to call those OS function in other CL implementations.
A directory is merely a (special) file, so
fcntlshould be able to lock it (one has to think carefully about what it means to "write to a directory" though).Windows world is much more complicated though. I don't think it is possible to lock a directory using a library function.
App-level
You can implement collaborative locking yourself. This would mean that only the applications using your library would respect the locking, so you will be able to fix possible issues outside the app.
E.g. (untested!):
Locking whole directories would require non-trivial finesse to avoid deadlocks: locking a directory means locking all its descendants, so acquiring a lock on a file requires first locking everyting above that file, then locking the file, then unlocking everyhing above. This opens us to a race condition.
The simple solution is to have a master lock which is required for any locking operation: