[syslog-ng] [PATCH 5/7] [nvtables]: thread safe nv_registry manipulation

Balazs Scheidler bazsi at balabit.hu
Sat May 28 15:56:32 CEST 2011


Hi,

This locking is certainly necessary, however the scope is not enough in
nv_registry_alloc_handle(). The lookup must also be locked, since the
same name can be associated to a different value otherwise.

It should also be checked if this would happen on a fastpath though.

On Mon, 2011-05-23 at 11:56 +0200, Gergely Nagy wrote:
> From: Juhasz Viktor <jviktor at balabit.hu>
> 
> Since nv_registry_alloc_handle() can be indirectly called from
> multiple threads, when it modifies the registry, that needs to be
> protected with a mutex.
> 
> Same goes for nv_registry_add_alias().
> 
> Signed-off-by: Viktor Juhasz <jviktor at balabit.hu>
> ---
>  lib/nvtable.c |    6 ++++++
>  1 files changed, 6 insertions(+), 0 deletions(-)
> 
> diff --git a/lib/nvtable.c b/lib/nvtable.c
> index aa9d8a1..d0a35d3 100644
> --- a/lib/nvtable.c
> +++ b/lib/nvtable.c
> @@ -27,6 +27,8 @@
>  #include <string.h>
>  #include <stdlib.h>
>  
> +GStaticMutex nv_registry_lock = G_STATIC_MUTEX_INIT;
> +
>  const gchar *null_string = "";
>  
>  NVHandle
> @@ -78,8 +80,10 @@ nv_registry_alloc_handle(NVRegistry *self, const gchar *name)
>    stored.flags = 0;
>    stored.name_len = len;
>    stored.name = g_strdup(name);
> +  g_static_mutex_lock(&nv_registry_lock);
>    g_array_append_val(self->names, stored);
>    g_hash_table_insert(self->name_map, stored.name, GUINT_TO_POINTER(self->names->len));
> +  g_static_mutex_unlock(&nv_registry_lock);
>    return self->names->len;
>  }
>  
> @@ -91,7 +95,9 @@ nv_registry_alloc_handle(NVRegistry *self, const gchar *name)
>  void
>  nv_registry_add_alias(NVRegistry *self, NVHandle handle, const gchar *alias)
>  {
> +  g_static_mutex_lock(&nv_registry_lock);
>    g_hash_table_insert(self->name_map, (gchar *) alias, GUINT_TO_POINTER((glong) handle));
> +  g_static_mutex_unlock(&nv_registry_lock);
>  }
>  
>  void

-- 
Bazsi




More information about the syslog-ng mailing list