dimo414 / bkt

a subprocess caching utility, available as a command line binary and a Rust library.
https://www.bkt.rs
MIT License
251 stars 13 forks source link

Cooperative locking #11

Open dimo414 opened 2 years ago

dimo414 commented 2 years ago

bash-cache provides optional support for cooperative locking via bc::locking_cache, which ensures concurrent calls to the same function will not race. It would be nice to add something similar to bkt.

There's already a simple FileLock implementation, though it doesn't support waiting for the lock to release at present. notify looks useful, but a simple polling loop might be sufficient (and more lightweight) given that the process won't be doing anything else until the lock releases.

zbentley commented 2 years ago

flock(2) doesn't take a timeout, but otherwise may satisfy locking needs here. I'm unsure how portable it is to Windows, or if that's a priority.

Given bkt's nature, the lack of a timeout to flock is not a big deal; do a blocking flock in a thread and simply give up on the thread if a timer expires (or use alarm, but that's harder to get right).

I wouldn't recommend notify/any of the fsnotify/dnotify/inotify family. They tend to have complicated APIs and be unexpectedly lossy when it comes to buffer overflows.