]> git.ipfire.org Git - thirdparty/ccache.git/commit
enhance: Improve lock file implementation
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 5 Jul 2022 18:04:25 +0000 (20:04 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 5 Jul 2022 18:04:25 +0000 (20:04 +0200)
commit0babd33e84147e923a729ee07a3b85097ec8baa8
tree5443885641b4b754448189f235b027c3820b0e5a
parent62df0178c0e7915a95399986d5685990ac924703
enhance: Improve lock file implementation

This commit improves brings two improvements to the lock file
implementation:

1. Support for long-lived locks.
2. Support for non-blocking lock acquisition.

There are now two types of lock file classes: ShortLivedLockFile and
LongLivedLockFile. On Windows the implementations are identical. On
other systems, LongLivedLockFile creates a separate "alive file" whose
mtime will be updated regularly by a helper thread until the lock is
released. This makes it possible for another process to wait for the
lock indefinitely but also to know then a lock is stale and can be
broken. The ShortLivedLockFile class works like the lock file class used
to work before: it considers a lock stale after waiting for two seconds
and noticing that the symlink target has not changed during that time.
ShortLivedLockFile is to be used when the lock is expected to be held
for a very short time so that it's a waste of resources to start a
helper thread to keep the lock alive.

On some systems it would be possible to update mtime of the symlink
itself instead of a separate file, but that does not seem to be portable
enough.

Also worth mentioning is that the reason to not use proper fcntl/flock
locks on non-Windows systems is to continue supporting a cache directory
on NFS since file locks on NFS don't have a good track record.
13 files changed:
src/CMakeLists.txt
src/Lockfile.cpp [deleted file]
src/Lockfile.hpp [deleted file]
src/ccache.cpp
src/storage/primary/StatsFile.cpp
src/test_lockfile.cpp
src/util/CMakeLists.txt
src/util/LockFile.cpp [new file with mode: 0644]
src/util/LockFile.hpp [new file with mode: 0644]
src/util/file.cpp
unittest/CMakeLists.txt
unittest/test_Lockfile.cpp [deleted file]
unittest/test_util_LockFile.cpp [new file with mode: 0644]