Don't define WIN32 and NATIVE_WIN32.
[platform/upstream/glib.git] / glib / gdataset.c
index 35c7be8..b35c911 100644 (file)
  * 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-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 ; FIXME: might still freeze, watch out, not thoroughly
+ * looked at yet.  
+ */
+
 #include        <string.h>
 #include       "glib.h"
 
@@ -57,22 +70,28 @@ static inline void  g_data_set_internal             (GData          **datalist,
                                                         GDestroyNotify   destroy_func,
                                                         GDataset        *dataset);
 static void            g_data_initialize               (void);
-static inline GQuark   g_quark_new                     (const gchar    *string);
+static inline GQuark   g_quark_new                     (gchar          *string);
 
 
 /* --- variables --- */
-static GHashTable   *g_quark_ht = NULL;
-static gchar       **g_quarks = NULL;
-static GQuark        g_quark_seq_id = 0;
+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)
 {
@@ -109,10 +128,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)
 {
@@ -128,6 +153,7 @@ g_dataset_lookup (gconstpointer     dataset_location)
   return dataset;
 }
 
+/* HOLDS: g_dataset_global_lock */
 static void
 g_dataset_destroy_internal (GDataset *dataset)
 {
@@ -155,6 +181,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;
@@ -163,8 +190,10 @@ g_dataset_destroy (gconstpointer  dataset_location)
       if (dataset)
        g_dataset_destroy_internal (dataset);
     }
+  G_UNLOCK (g_dataset_global);
 }
 
+/* HOLDS: g_dataset_global_lock */
 static inline void
 g_data_set_internal (GData       **datalist,
                     GQuark         key_id,
@@ -198,7 +227,7 @@ g_data_set_internal (GData    **datalist,
                }
              
              /* the GData struct *must* already be unlinked
-              * when invoking the destroy function
+              * when invoking the destroy function.
               * we use (data==NULL && destroy_func!=NULL) as
               * a special hint combination to "steal"
               * data without destroy notification
@@ -290,9 +319,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)
     {
@@ -305,6 +335,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
@@ -323,8 +354,13 @@ g_datalist_id_set_data_full (GData   **datalist,
       else
        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
@@ -333,6 +369,7 @@ g_dataset_id_remove_no_notify (gconstpointer  dataset_location,
 {
   g_return_if_fail (dataset_location != NULL);
   
+  G_LOCK (g_dataset_global);
   if (key_id && g_dataset_location_ht)
     {
       GDataset *dataset;
@@ -340,7 +377,8 @@ g_dataset_id_remove_no_notify (gconstpointer  dataset_location,
       dataset = g_dataset_lookup (dataset_location);
       if (dataset)
        g_data_set_internal (&dataset->datalist, key_id, NULL, (GDestroyNotify) 42, dataset);
-    }
+    } 
+  G_UNLOCK (g_dataset_global);
 }
 
 void
@@ -349,8 +387,10 @@ g_datalist_id_remove_no_notify (GData      **datalist,
 {
   g_return_if_fail (datalist != NULL);
 
-  if (key_id)
+  G_LOCK (g_dataset_global);
+  if (key_id && g_dataset_location_ht)
     g_data_set_internal (datalist, key_id, NULL, (GDestroyNotify) 42, NULL);
+  G_UNLOCK (g_dataset_global);
 }
 
 gpointer
@@ -359,6 +399,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;
@@ -370,10 +411,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;
 }
 
@@ -404,14 +449,23 @@ g_dataset_foreach (gconstpointer    dataset_location,
   
   g_return_if_fail (dataset_location != NULL);
   g_return_if_fail (func != NULL);
-  
-  dataset = g_dataset_lookup (dataset_location);
-  if (dataset)
+
+  G_LOCK (g_dataset_global);
+  if (g_dataset_location_ht)
     {
-      register GData *list;
-      
-      for (list = dataset->datalist; list; list = list->next)
-       func (list->id, list->data, user_data);
+      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);
+       }
+    }
+  else
+    {
+      G_UNLOCK (g_dataset_global);
     }
 }
 
@@ -437,36 +491,38 @@ g_datalist_init (GData **datalist)
   *datalist = NULL;
 }
 
+/* HOLDS: g_dataset_global_lock */
 static void
 g_data_initialize (void)
 {
-  if (!g_dataset_location_ht)
-    {
-      g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
-      g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL);
-      g_dataset_cached = NULL;
-      g_dataset_mem_chunk =
-       g_mem_chunk_new ("GDataset MemChunk",
-                        sizeof (GDataset),
-                        sizeof (GDataset) * G_DATASET_MEM_CHUNK_PREALLOC,
-                        G_ALLOC_AND_FREE);
-      g_data_mem_chunk =
-       g_mem_chunk_new ("GData MemChunk",
-                        sizeof (GData),
-                        sizeof (GData) * G_DATA_MEM_CHUNK_PREALLOC,
-                        G_ALLOC_AND_FREE);
-    }
+  g_return_if_fail (g_dataset_location_ht == NULL);
+
+  g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL);
+  g_dataset_cached = NULL;
+  g_dataset_mem_chunk =
+    g_mem_chunk_new ("GDataset MemChunk",
+                    sizeof (GDataset),
+                    sizeof (GDataset) * G_DATASET_MEM_CHUNK_PREALLOC,
+                    G_ALLOC_AND_FREE);
+  g_data_mem_chunk =
+    g_mem_chunk_new ("GData MemChunk",
+                    sizeof (GData),
+                    sizeof (GData) * G_DATA_MEM_CHUNK_PREALLOC,
+                    G_ALLOC_AND_FREE);
 }
 
 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
@@ -476,12 +532,18 @@ g_quark_from_string (const gchar *string)
   
   g_return_val_if_fail (string != NULL, 0);
   
-  if (!g_quark_ht)
-    g_data_initialize ();
+  G_LOCK (g_quark_global);
+  if (g_quark_ht)
+    quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
+  else
+    {
+      g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
+      quark = 0;
+    }
   
-  quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
   if (!quark)
     quark = g_quark_new (g_strdup (string));
+  G_UNLOCK (g_quark_global);
   
   return quark;
 }
@@ -493,39 +555,47 @@ g_quark_from_static_string (const gchar *string)
   
   g_return_val_if_fail (string != NULL, 0);
   
-  if (!g_quark_ht)
-    g_data_initialize ();
-  
-  quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
+  G_LOCK (g_quark_global);
+  if (g_quark_ht)
+    quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
+  else
+    {
+      g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
+      quark = 0;
+    }
+
   if (!quark)
-    quark = g_quark_new (string);
-  
+    quark = g_quark_new ((gchar*) string);
+  G_UNLOCK (g_quark_global);
   return quark;
 }
 
 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 (const gchar *string)
+g_quark_new (gchar *string)
 {
   GQuark quark;
   
   if (g_quark_seq_id % G_QUARK_BLOCK_SIZE == 0)
-    g_quarks = g_realloc (g_quarks,
-                         (g_quark_seq_id + G_QUARK_BLOCK_SIZE) * sizeof (gchar*));
-  
+    g_quarks = g_renew (gchar*, g_quarks, g_quark_seq_id + G_QUARK_BLOCK_SIZE);
   
-  g_quarks[g_quark_seq_id] = (gchar*) string;
+  g_quarks[g_quark_seq_id] = string;
   g_quark_seq_id++;
   quark = g_quark_seq_id;
-  g_hash_table_insert (g_quark_ht, (gchar*) string, GUINT_TO_POINTER (quark));
+  g_hash_table_insert (g_quark_ht, string, GUINT_TO_POINTER (quark));
   
   return quark;
 }