From f61b5371c492b0685c179090372f35329e8a2028 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 29 Sep 2011 16:51:48 +0200 Subject: [PATCH] Add dotlock_get_fd and dotlock_set_fd. --- common/ChangeLog | 1 + common/dotlock.c | 36 ++++++++++++++++++++++++++++++++++-- common/dotlock.h | 2 ++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/common/ChangeLog b/common/ChangeLog index 7ed2673ba..1c067cdf5 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -4,6 +4,7 @@ [DOTLOCK_USE_PTHREAD] (all_lockfiles_mutex): New. (LOCK_all_lockfiles, UNLOCK_all_lockfiles): New. Use them to protect access to all_lockfiles. + (dotlock_set_fd, dotlock_get_fd): New. 2011-09-28 Werner Koch diff --git a/common/dotlock.c b/common/dotlock.c index 146394412..c65f6f637 100644 --- a/common/dotlock.c +++ b/common/dotlock.c @@ -100,8 +100,10 @@ if (dotlock_release (h)) error ("error releasing lock: %s\n", strerror (errno)); - or, if the lock file is not anymore needed, you may call - dotlock_destroy. + or, if the lock file is not anymore needed, you may just call + dotlock_destroy. However dotlock_release does some extra checks + before releasing the lock and prints diagnostics to help detecting + bugs. If you want to explicitly destroy all lock files you may call @@ -114,6 +116,15 @@ before any locks are created. + There are two convenience functions to store an integer (e.g. a + file descriptor) value with the handle: + + void dotlock_set_fd (dotlock_t h, int fd); + int dotlock_get_fd (dotlock_t h); + + If nothing has been stored dotlock_get_fd returns -1. + + How to build: ============= @@ -336,6 +347,8 @@ struct dotlock_handle unsigned int disable:1; /* If true, locking is disabled. */ unsigned int use_o_excl:1; /* Use open (O_EXCL) for locking. */ + int extra_fd; /* A place for the caller to store an FD. */ + #ifdef HAVE_DOSISH_SYSTEM HANDLE lockhd; /* The W32 handle of the lock file. */ #else /*!HAVE_DOSISH_SYSTEM */ @@ -769,6 +782,7 @@ dotlock_create (const char *file_to_lock, unsigned int flags) h = jnlib_calloc (1, sizeof *h); if (!h) return NULL; + h->extra_fd = -1; if (never_lock) { @@ -788,6 +802,24 @@ dotlock_create (const char *file_to_lock, unsigned int flags) } + +/* Convenience function to store a file descriptor (or any any other + integer value) in the context of handle H. */ +void +dotlock_set_fd (dotlock_t h, int fd) +{ + h->extra_fd = fd; +} + +/* Convenience function to retrieve a file descriptor (or any any other + integer value) stored in the context of handle H. */ +int +dotlock_get_fd (dotlock_t h) +{ + return h->extra_fd; +} + + #ifdef HAVE_POSIX_SYSTEM /* Unix specific code of destroy_dotlock. */ diff --git a/common/dotlock.h b/common/dotlock.h index 666d0b72c..6ae7836c5 100644 --- a/common/dotlock.h +++ b/common/dotlock.h @@ -27,6 +27,8 @@ typedef struct dotlock_handle *dotlock_t; void dotlock_disable (void); dotlock_t dotlock_create (const char *file_to_lock, unsigned int flags); +void dotlock_set_fd (dotlock_t h, int fd); +int dotlock_get_fd (dotlock_t h); void dotlock_destroy (dotlock_t h); int dotlock_take (dotlock_t h, long timeout); int dotlock_release (dotlock_t h);