Interpret EINTR as G_IO_ERROR_AGAIN.
[platform/upstream/glib.git] / ghash.c
diff --git a/ghash.c b/ghash.c
index 4ab82b3..331a1ab 100644 (file)
--- a/ghash.c
+++ b/ghash.c
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
+/*
+ * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
+ * file for a list of people on the GLib Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
+/* 
+ * MT safe
+ */
+
 #include "glib.h"
 
 
@@ -52,6 +64,8 @@ static void           g_hash_node_destroy      (GHashNode     *hash_node);
 static void            g_hash_nodes_destroy     (GHashNode     *hash_node);
 
 
+G_LOCK_DEFINE_STATIC (g_hash_global);
+
 static GMemChunk *node_mem_chunk = NULL;
 static GHashNode *node_free_list = NULL;
 
@@ -166,16 +180,18 @@ g_hash_table_remove (GHashTable        *hash_table,
   
   g_return_if_fail (hash_table != NULL);
   
-  while (*(node = g_hash_table_lookup_node (hash_table, key)))
+  node = g_hash_table_lookup_node (hash_table, key);
+
+  if (*node)
     {
       dest = *node;
       (*node) = dest->next;
       g_hash_node_destroy (dest);
       hash_table->nnodes--;
-    }
   
-  if (!hash_table->frozen)
-    g_hash_table_resize (hash_table);
+      if (!hash_table->frozen)
+        g_hash_table_resize (hash_table);
+    }
 }
 
 gboolean
@@ -220,14 +236,14 @@ g_hash_table_thaw (GHashTable *hash_table)
       g_hash_table_resize (hash_table);
 }
 
-gint
+guint
 g_hash_table_foreach_remove (GHashTable        *hash_table,
                             GHRFunc     func,
                             gpointer    user_data)
 {
   GHashNode *node, *prev;
   guint i;
-  gint deleted = 0;
+  guint deleted = 0;
   
   g_return_val_if_fail (hash_table != NULL, 0);
   g_return_val_if_fail (func != NULL, 0);
@@ -285,7 +301,7 @@ g_hash_table_foreach (GHashTable *hash_table,
 }
 
 /* Returns the number of elements contained in the hash table. */
-gint
+guint
 g_hash_table_size (GHashTable *hash_table)
 {
   g_return_val_if_fail (hash_table != NULL, 0);
@@ -313,16 +329,15 @@ g_hash_table_resize (GHashTable *hash_table)
   new_size = CLAMP(g_spaced_primes_closest (hash_table->nnodes),
                   HASH_TABLE_MIN_SIZE,
                   HASH_TABLE_MAX_SIZE);
-  new_nodes = g_new (GHashNode*, new_size);
-  
-  for (i = 0; i < new_size; i++)
-    new_nodes[i] = NULL;
+  new_nodes = g_new0 (GHashNode*, new_size);
   
   for (i = 0; i < hash_table->size; i++)
     for (node = hash_table->nodes[i]; node; node = next)
       {
        next = node->next;
+
        hash_val = (* hash_table->hash_func) (node->key) % new_size;
+
        node->next = new_nodes[hash_val];
        new_nodes[hash_val] = node;
       }
@@ -338,6 +353,7 @@ g_hash_node_new (gpointer key,
 {
   GHashNode *hash_node;
   
+  G_LOCK (g_hash_global);
   if (node_free_list)
     {
       hash_node = node_free_list;
@@ -352,6 +368,7 @@ g_hash_node_new (gpointer key,
       
       hash_node = g_chunk_new (GHashNode, node_mem_chunk);
     }
+  G_UNLOCK (g_hash_global);
   
   hash_node->key = key;
   hash_node->value = value;
@@ -363,23 +380,25 @@ g_hash_node_new (gpointer key,
 static void
 g_hash_node_destroy (GHashNode *hash_node)
 {
+  G_LOCK (g_hash_global);
   hash_node->next = node_free_list;
   node_free_list = hash_node;
+  G_UNLOCK (g_hash_global);
 }
 
 static void
 g_hash_nodes_destroy (GHashNode *hash_node)
 {
-  GHashNode *node;
-  
-  if (!hash_node)
-    return;
-  
-  node = hash_node;
+  if (hash_node)
+    {
+      GHashNode *node = hash_node;
   
-  while (node->next)
-    node = node->next;
+      while (node->next)
+        node = node->next;
   
-  node->next = node_free_list;
-  node_free_list = hash_node;
+      G_LOCK (g_hash_global);
+      node->next = node_free_list;
+      node_free_list = hash_node;
+      G_UNLOCK (g_hash_global);
+    }
 }