added g_list_nth_prev() which walks ->prev instead of ->next.
[platform/upstream/glib.git] / gdataset.c
index 7dafc21..136141e 100644 (file)
@@ -5,19 +5,32 @@
  * Copyright (C) 1998 Tim Janik
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  */
+
+/*
+ * Modified by the GLib Team and others 1997-2000.  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 ; FIXME: might still freeze, watch out, not thoroughly
+ * looked at yet.  
+ */
+
 #include        <string.h>
 #include       "glib.h"
 
@@ -51,7 +64,7 @@ struct _GDataset
 static inline GDataset*        g_dataset_lookup                (gconstpointer    dataset_location);
 static inline void     g_datalist_clear_i              (GData          **datalist);
 static void            g_dataset_destroy_internal      (GDataset        *dataset);
-static inline void     g_data_set_internal             (GData          **datalist,
+static inline gpointer g_data_set_internal             (GData          **datalist,
                                                         GQuark           key_id,
                                                         gpointer         data,
                                                         GDestroyNotify   destroy_func,
@@ -61,18 +74,24 @@ static inline GQuark        g_quark_new                     (gchar          *string);
 
 
 /* --- variables --- */
+G_LOCK_DEFINE_STATIC (g_dataset_global);
 static GHashTable   *g_dataset_location_ht = NULL;
-static GDataset     *g_dataset_cached = NULL;
+static GDataset     *g_dataset_cached = NULL; /* should this be
+                                                threadspecific? */
 static GMemChunk    *g_dataset_mem_chunk = NULL;
 static GMemChunk    *g_data_mem_chunk = NULL;
 static GData       *g_data_cache = NULL;
 static guint        g_data_cache_length = 0;
+
+G_LOCK_DEFINE_STATIC (g_quark_global);
 static GHashTable   *g_quark_ht = NULL;
 static gchar       **g_quarks = NULL;
 static GQuark        g_quark_seq_id = 0;
 
 
 /* --- functions --- */
+
+/* HOLDS: g_dataset_global_lock */
 static inline void
 g_datalist_clear_i (GData **datalist)
 {
@@ -91,7 +110,11 @@ g_datalist_clear_i (GData **datalist)
       list = prev->next;
       
       if (prev->destroy_func)
-       prev->destroy_func (prev->data);
+       {
+         G_UNLOCK (g_dataset_global);
+         prev->destroy_func (prev->data);
+         G_LOCK (g_dataset_global);
+       }
       
       if (g_data_cache_length < G_DATA_CACHE_MAX)
        {
@@ -109,13 +132,16 @@ g_datalist_clear (GData **datalist)
 {
   g_return_if_fail (datalist != NULL);
   
+  G_LOCK (g_dataset_global);
   if (!g_dataset_location_ht)
     g_data_initialize ();
 
   while (*datalist)
     g_datalist_clear_i (datalist);
+  G_UNLOCK (g_dataset_global);
 }
 
+/* HOLDS: g_dataset_global_lock */
 static inline GDataset*
 g_dataset_lookup (gconstpointer        dataset_location)
 {
@@ -131,6 +157,7 @@ g_dataset_lookup (gconstpointer     dataset_location)
   return dataset;
 }
 
+/* HOLDS: g_dataset_global_lock */
 static void
 g_dataset_destroy_internal (GDataset *dataset)
 {
@@ -158,6 +185,7 @@ g_dataset_destroy (gconstpointer  dataset_location)
 {
   g_return_if_fail (dataset_location != NULL);
   
+  G_LOCK (g_dataset_global);
   if (g_dataset_location_ht)
     {
       register GDataset *dataset;
@@ -166,9 +194,11 @@ g_dataset_destroy (gconstpointer  dataset_location)
       if (dataset)
        g_dataset_destroy_internal (dataset);
     }
+  G_UNLOCK (g_dataset_global);
 }
 
-static inline void
+/* HOLDS: g_dataset_global_lock */
+static inline gpointer
 g_data_set_internal (GData       **datalist,
                     GQuark         key_id,
                     gpointer       data,
@@ -187,6 +217,8 @@ g_data_set_internal (GData    **datalist,
        {
          if (list->id == key_id)
            {
+             gpointer ret_data = NULL;
+
              if (prev)
                prev->next = list->next;
              else
@@ -207,7 +239,13 @@ g_data_set_internal (GData   **datalist,
               * data without destroy notification
               */
              if (list->destroy_func && !destroy_func)
-               list->destroy_func (list->data);
+               {
+                 G_UNLOCK (g_dataset_global);
+                 list->destroy_func (list->data);
+                 G_LOCK (g_dataset_global);
+               }
+             else
+               ret_data = list->data;
              
              if (g_data_cache_length < G_DATA_CACHE_MAX)
                {
@@ -218,7 +256,7 @@ g_data_set_internal (GData    **datalist,
              else
                g_mem_chunk_free (g_data_mem_chunk, list);
              
-             return;
+             return ret_data;
            }
          
          prev = list;
@@ -249,10 +287,12 @@ g_data_set_internal (GData          **datalist,
                  /* we need to have updated all structures prior to
                   * invokation of the destroy function
                   */
+                 G_UNLOCK (g_dataset_global);
                  dfunc (ddata);
+                 G_LOCK (g_dataset_global);
                }
              
-             return;
+             return NULL;
            }
          
          list = list->next;
@@ -272,6 +312,8 @@ g_data_set_internal (GData    **datalist,
       list->destroy_func = destroy_func;
       *datalist = list;
     }
+
+  return NULL;
 }
 
 void
@@ -293,9 +335,10 @@ g_dataset_id_set_data_full (gconstpointer  dataset_location,
        return;
     }
   
+  G_LOCK (g_dataset_global);
   if (!g_dataset_location_ht)
     g_data_initialize ();
-  
   dataset = g_dataset_lookup (dataset_location);
   if (!dataset)
     {
@@ -308,6 +351,7 @@ g_dataset_id_set_data_full (gconstpointer  dataset_location,
     }
   
   g_data_set_internal (&dataset->datalist, key_id, data, destroy_func, dataset);
+  G_UNLOCK (g_dataset_global);
 }
 
 void
@@ -327,36 +371,50 @@ g_datalist_id_set_data_full (GData          **datalist,
        return;
     }
 
+  G_LOCK (g_dataset_global);
   if (!g_dataset_location_ht)
     g_data_initialize ();
   
   g_data_set_internal (datalist, key_id, data, destroy_func, NULL);
+  G_UNLOCK (g_dataset_global);
 }
 
-void
+gpointer
 g_dataset_id_remove_no_notify (gconstpointer  dataset_location,
                               GQuark         key_id)
 {
-  g_return_if_fail (dataset_location != NULL);
+  gpointer ret_data = NULL;
+
+  g_return_val_if_fail (dataset_location != NULL, NULL);
   
+  G_LOCK (g_dataset_global);
   if (key_id && g_dataset_location_ht)
     {
       GDataset *dataset;
   
       dataset = g_dataset_lookup (dataset_location);
       if (dataset)
-       g_data_set_internal (&dataset->datalist, key_id, NULL, (GDestroyNotify) 42, dataset);
-    }
+       ret_data = g_data_set_internal (&dataset->datalist, key_id, NULL, (GDestroyNotify) 42, dataset);
+    } 
+  G_UNLOCK (g_dataset_global);
+
+  return ret_data;
 }
 
-void
+gpointer
 g_datalist_id_remove_no_notify (GData  **datalist,
                                GQuark    key_id)
 {
-  g_return_if_fail (datalist != NULL);
+  gpointer ret_data = NULL;
 
+  g_return_val_if_fail (datalist != NULL, NULL);
+
+  G_LOCK (g_dataset_global);
   if (key_id && g_dataset_location_ht)
-    g_data_set_internal (datalist, key_id, NULL, (GDestroyNotify) 42, NULL);
+    ret_data = g_data_set_internal (datalist, key_id, NULL, (GDestroyNotify) 42, NULL);
+  G_UNLOCK (g_dataset_global);
+
+  return ret_data;
 }
 
 gpointer
@@ -365,6 +423,7 @@ g_dataset_id_get_data (gconstpointer  dataset_location,
 {
   g_return_val_if_fail (dataset_location != NULL, NULL);
   
+  G_LOCK (g_dataset_global);
   if (key_id && g_dataset_location_ht)
     {
       register GDataset *dataset;
@@ -376,10 +435,14 @@ g_dataset_id_get_data (gconstpointer  dataset_location,
          
          for (list = dataset->datalist; list; list = list->next)
            if (list->id == key_id)
-             return list->data;
+             {
+               G_UNLOCK (g_dataset_global);
+               return list->data;
+             }
        }
     }
-  
+  G_UNLOCK (g_dataset_global);
   return NULL;
 }
 
@@ -411,17 +474,23 @@ g_dataset_foreach (gconstpointer    dataset_location,
   g_return_if_fail (dataset_location != NULL);
   g_return_if_fail (func != NULL);
 
+  G_LOCK (g_dataset_global);
   if (g_dataset_location_ht)
     {
       dataset = g_dataset_lookup (dataset_location);
+      G_UNLOCK (g_dataset_global);
       if (dataset)
        {
          register GData *list;
          
          for (list = dataset->datalist; list; list = list->next)
-           func (list->id, list->data, user_data);
+             func (list->id, list->data, user_data);
        }
     }
+  else
+    {
+      G_UNLOCK (g_dataset_global);
+    }
 }
 
 void
@@ -446,6 +515,7 @@ g_datalist_init (GData **datalist)
   *datalist = NULL;
 }
 
+/* HOLDS: g_dataset_global_lock */
 static void
 g_data_initialize (void)
 {
@@ -468,12 +538,15 @@ g_data_initialize (void)
 GQuark
 g_quark_try_string (const gchar *string)
 {
+  GQuark quark = 0;
   g_return_val_if_fail (string != NULL, 0);
   
+  G_LOCK (g_quark_global);
   if (g_quark_ht)
-    return (gulong) g_hash_table_lookup (g_quark_ht, string);
-  else
-    return 0;
+    quark = GPOINTER_TO_UINT (g_hash_table_lookup (g_quark_ht, string));
+  G_UNLOCK (g_quark_global);
+  
+  return quark;
 }
 
 GQuark
@@ -483,6 +556,7 @@ g_quark_from_string (const gchar *string)
   
   g_return_val_if_fail (string != NULL, 0);
   
+  G_LOCK (g_quark_global);
   if (g_quark_ht)
     quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
   else
@@ -493,6 +567,7 @@ g_quark_from_string (const gchar *string)
   
   if (!quark)
     quark = g_quark_new (g_strdup (string));
+  G_UNLOCK (g_quark_global);
   
   return quark;
 }
@@ -504,6 +579,7 @@ g_quark_from_static_string (const gchar *string)
   
   g_return_val_if_fail (string != NULL, 0);
   
+  G_LOCK (g_quark_global);
   if (g_quark_ht)
     quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
   else
@@ -514,19 +590,24 @@ g_quark_from_static_string (const gchar *string)
 
   if (!quark)
     quark = g_quark_new ((gchar*) string);
-  
+  G_UNLOCK (g_quark_global);
   return quark;
 }
 
-gchar*
+G_CONST_RETURN gchar*
 g_quark_to_string (GQuark quark)
 {
+  gchar* result = NULL;
+  G_LOCK (g_quark_global);
   if (quark > 0 && quark <= g_quark_seq_id)
-    return g_quarks[quark - 1];
-  else
-    return NULL;
+    result = g_quarks[quark - 1];
+  G_UNLOCK (g_quark_global);
+
+  return result;
 }
 
+/* HOLDS: g_quark_global_lock */
 static inline GQuark
 g_quark_new (gchar *string)
 {