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

Gergely Nagy algernon at balabit.hu
Mon May 23 11:56:13 CEST 2011


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
-- 
1.7.2.5



More information about the syslog-ng mailing list