Eric Blake
2018-Jan-25 15:32 UTC
[Libguestfs] [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
Richard W.M. Jones
2018-Jan-25 16:22 UTC
Re: [Libguestfs] [nbdkit PATCH] errors: Use lighter-weight locking
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
Apparently Analagous Threads
- -all-static problems when linking apps which require -lpthread and -lc
- Re: [nbdkit PATCH 1/4] errors: Avoid interleaved errors from parallel threads
- [nbdkit PATCH 0/4] thread-safety issues prior to parallel handling
- [nbdkit PATCH 1/4] errors: Avoid interleaved errors from parallel threads
- Fwd: (KAME-snap 9012) racoon in the kame project