path: root/lockfile.h
diff options
authorMichael Haggerty <>2015-05-11 10:35:25 (GMT)
committerJunio C Hamano <>2015-05-14 21:51:08 (GMT)
commit044b6a9efecf9941073b99ced4cb2881b18aee62 (patch)
tree5cfbf39335b835c77d09d94250c2e4aaa8c2897a /lockfile.h
parent1ea28e149403ba92f95205f324cc3119fb92df07 (diff)
lockfile: allow file locking to be retried with a timeout
Currently, there is only one attempt to lock a file. If it fails, the whole operation fails. But it might sometimes be advantageous to try acquiring a file lock a few times before giving up. So add a new function, hold_lock_file_for_update_timeout(), that allows a timeout to be specified. Make hold_lock_file_for_update() a thin wrapper around the new function. If timeout_ms is positive, then retry for at least that many milliseconds to acquire the lock. On each failed attempt, use select() to wait for a backoff time that increases quadratically (capped at 1 second) and has a random component to prevent two processes from getting synchronized. If timeout_ms is negative, retry indefinitely. In a moment we will switch to using the new function when locking packed-refs. Signed-off-by: Michael Haggerty <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'lockfile.h')
1 files changed, 14 insertions, 2 deletions
diff --git a/lockfile.h b/lockfile.h
index cd2ec95..b4abc61 100644
--- a/lockfile.h
+++ b/lockfile.h
@@ -74,8 +74,20 @@ struct lock_file {
extern void unable_to_lock_message(const char *path, int err,
struct strbuf *buf);
extern NORETURN void unable_to_lock_die(const char *path, int err);
-extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
-extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
+extern int hold_lock_file_for_update_timeout(
+ struct lock_file *lk, const char *path,
+ int flags, long timeout_ms);
+static inline int hold_lock_file_for_update(
+ struct lock_file *lk, const char *path,
+ int flags)
+ return hold_lock_file_for_update_timeout(lk, path, flags, 0);
+extern int hold_lock_file_for_append(struct lock_file *lk, const char *path,
+ int flags);
extern FILE *fdopen_lock_file(struct lock_file *, const char *mode);
extern char *get_locked_file_path(struct lock_file *);
extern int commit_lock_file_to(struct lock_file *, const char *path);