Now that cleanup.h is in common code, we can use it in our filters where it makes sense. Many uses of pthread_mutex_unlock() are not function-wide, and over small enough snippets of code as to be easier to read when left as-is; but when the scope is indeed function-wide or across multiple exit paths, it is nicer to use the macro for automatic unlock. Signed-off-by: Eric Blake <eblake@redhat.com> --- filters/log/log.c | 10 ++++------ filters/readahead/readahead.c | 15 +++++---------- filters/log/Makefile.am | 5 ++++- filters/readahead/Makefile.am | 5 ++++- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/filters/log/log.c b/filters/log/log.c index 02a2522..513d390 100644 --- a/filters/log/log.c +++ b/filters/log/log.c @@ -45,6 +45,8 @@ #include <nbdkit-filter.h> +#include "cleanup.h" + #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL static uint64_t connections; @@ -114,12 +116,8 @@ struct handle { static uint64_t get_id (struct handle *h) { - uint64_t r; - - pthread_mutex_lock (&lock); - r = ++h->id; - pthread_mutex_unlock (&lock); - return r; + ACQUIRE_LOCK_FOR_CURRENT_SCOPE(&lock); + return ++h->id; } /* Output a timestamp and the log message. */ diff --git a/filters/readahead/readahead.c b/filters/readahead/readahead.c index 5e14347..f46b6b0 100644 --- a/filters/readahead/readahead.c +++ b/filters/readahead/readahead.c @@ -42,6 +42,7 @@ #include <nbdkit-filter.h> +#include "cleanup.h" #include "minmax.h" #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL @@ -150,7 +151,7 @@ readahead_pread (struct nbdkit_next_ops *next_ops, void *nxdata, void *handle, void *buf, uint32_t count, uint64_t offset, uint32_t flags, int *err) { - pthread_mutex_lock (&lock); + ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock); while (count > 0) { if (length == 0) { @@ -159,7 +160,7 @@ readahead_pread (struct nbdkit_next_ops *next_ops, void *nxdata, */ window = READAHEAD_MIN; if (fill_readahead (next_ops, nxdata, count, offset, flags, err) == -1) - goto err; + return -1; } /* Can we satisfy this request partly or entirely from the prefetch @@ -179,7 +180,7 @@ readahead_pread (struct nbdkit_next_ops *next_ops, void *nxdata, else if (offset == position + length) { window = MIN (window * 2, READAHEAD_MAX); if (fill_readahead (next_ops, nxdata, count, offset, flags, err) == -1) - goto err; + return -1; } /* Else it's a “miss”. Reset everything and start again. */ @@ -187,12 +188,7 @@ readahead_pread (struct nbdkit_next_ops *next_ops, void *nxdata, length = 0; } - pthread_mutex_unlock (&lock); return 0; - - err: - pthread_mutex_unlock (&lock); - return -1; } /* Any writes or write-like operations kill the prefetch buffer. @@ -204,10 +200,9 @@ readahead_pread (struct nbdkit_next_ops *next_ops, void *nxdata, static void kill_readahead (void) { - pthread_mutex_lock (&lock); + ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock); window = READAHEAD_MIN; length = 0; - pthread_mutex_unlock (&lock); } static int diff --git a/filters/log/Makefile.am b/filters/log/Makefile.am index 271d54e..08cdd00 100644 --- a/filters/log/Makefile.am +++ b/filters/log/Makefile.am @@ -40,12 +40,15 @@ nbdkit_log_filter_la_SOURCES = \ $(top_srcdir)/include/nbdkit-filter.h nbdkit_log_filter_la_CPPFLAGS = \ - -I$(top_srcdir)/include + -I$(top_srcdir)/include \ + -I$(top_srcdir)/common/utils nbdkit_log_filter_la_CFLAGS = \ $(WARNINGS_CFLAGS) nbdkit_log_filter_la_LDFLAGS = \ -module -avoid-version -shared \ -Wl,--version-script=$(top_srcdir)/filters/filters.syms +nbdkit_log_filter_la_LIBADD = \ + $(top_builddir)/common/utils/libutils.la if HAVE_POD diff --git a/filters/readahead/Makefile.am b/filters/readahead/Makefile.am index 0e7a4a8..54adfb8 100644 --- a/filters/readahead/Makefile.am +++ b/filters/readahead/Makefile.am @@ -41,12 +41,15 @@ nbdkit_readahead_filter_la_SOURCES = \ nbdkit_readahead_filter_la_CPPFLAGS = \ -I$(top_srcdir)/include \ - -I$(top_srcdir)/common/include + -I$(top_srcdir)/common/include \ + -I$(top_srcdir)/common/utils nbdkit_readahead_filter_la_CFLAGS = \ $(WARNINGS_CFLAGS) nbdkit_readahead_filter_la_LDFLAGS = \ -module -avoid-version -shared \ -Wl,--version-script=$(top_srcdir)/filters/filters.syms +nbdkit_readahead_filter_la_LIBADD = \ + $(top_builddir)/common/utils/libutils.la if HAVE_POD -- 2.20.1