[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Libguestfs] [nbdkit PATCH 1/4] errors: Avoid interleaved errors from parallel threads



On Thu, Nov 16, 2017 at 09:13:55PM -0600, Eric Blake wrote:
> Since we construct our error/debug messages via multiple calls
> to stdio primitives, we are at risk of multiple threads interleaving
> their output if they try to output at once.  Add a mutex to group
> related outputs into an atomic chunk.
> 
> Signed-off-by: Eric Blake <eblake redhat com>
> ---
>  src/errors.c | 30 +++++++++++++++++++++++++++++-
>  1 file changed, 29 insertions(+), 1 deletion(-)
> 
> diff --git a/src/errors.c b/src/errors.c
> index 5f14315..2fc83ab 100644
> --- a/src/errors.c
> +++ b/src/errors.c
> @@ -1,5 +1,5 @@
>  /* nbdkit
> - * Copyright (C) 2013 Red Hat Inc.
> + * Copyright (C) 2013-2017 Red Hat Inc.
>   * All rights reserved.
>   *
>   * Redistribution and use in source and binary forms, with or without
> @@ -38,10 +38,30 @@
>  #include <stdarg.h>
>  #include <string.h>
>  #include <errno.h>
> +#include <assert.h>
> +#include <pthread.h>
> 
>  #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);
> +}
> +
> +static void
> +unlock (void)
> +{
> +  int r = pthread_mutex_unlock(&errors_lock);
> +  assert(!r);

Spaces between assert (in 2 cases above) and parens.

The rest of this patch is fine, so ACK.

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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]