[nbdkit PATCH] errors: Use lighter-weight locking
Commit d02d9c9d used pthread_mutex to avoid interleaving output. However, the standard provides flockfile() for grouping related FILE* I/O that must not be interleaved; and that may be lighter-weight than rolling our own locking. Signed-off-by: Eric Blake <eblake@redhat.com> --- Pushing this one; as a related conversation on another mailing list reminded me about flockfile(). src/errors.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/errors.c b/src/errors.c index c1efae6..471e3ea 100644 --- a/src/errors.c +++ b/src/errors.c @@ -1,5 +1,5 @@ /* nbdkit - * Copyright (C) 2013-2017 Red Hat Inc. + * Copyright (C) 2013-2018 Red Hat Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -44,21 +44,16 @@ #include "nbdkit-plugin.h" #include "internal.h" -/* Used to group piecemeal message construction into atomic output. */ -static pthread_mutex_t errors_lock = PTHREAD_MUTEX_INITIALIZER; - static void lock (void) { - int r = pthread_mutex_lock (&errors_lock); - assert (!r); + flockfile (stderr); } static void unlock (void) { - int r = pthread_mutex_unlock (&errors_lock); - assert (!r); + funlockfile (stderr); } /* Called with lock taken. */ -- 2.14.3
On Thu, Jan 25, 2018 at 09:32:49AM -0600, Eric Blake wrote:
Commit d02d9c9d used pthread_mutex to avoid interleaving output. However, the standard provides flockfile() for grouping related FILE* I/O that must not be interleaved; and that may be lighter-weight than rolling our own locking.
Signed-off-by: Eric Blake <eblake@redhat.com> ---
Pushing this one; as a related conversation on another mailing list reminded me about flockfile().
src/errors.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
diff --git a/src/errors.c b/src/errors.c index c1efae6..471e3ea 100644 --- a/src/errors.c +++ b/src/errors.c @@ -1,5 +1,5 @@ /* nbdkit - * Copyright (C) 2013-2017 Red Hat Inc. + * Copyright (C) 2013-2018 Red Hat Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -44,21 +44,16 @@ #include "nbdkit-plugin.h" #include "internal.h"
-/* Used to group piecemeal message construction into atomic output. */ -static pthread_mutex_t errors_lock = PTHREAD_MUTEX_INITIALIZER; - static void lock (void) { - int r = pthread_mutex_lock (&errors_lock); - assert (!r); + flockfile (stderr); }
static void unlock (void) { - int r = pthread_mutex_unlock (&errors_lock); - assert (!r); + funlockfile (stderr); }
/* Called with lock taken. */ --
ACK. Maybe even inline those functions? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
participants (2)
-
Eric Blake -
Richard W.M. Jones