Renamed g_thread_create to g_thread_create_full and added macro
[platform/upstream/glib.git] / ghook.c
diff --git a/ghook.c b/ghook.c
index c81a846..0bce31c 100644 (file)
--- a/ghook.c
+++ b/ghook.c
@@ -5,20 +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
+ */
+
 #include       "glib.h"
 
 
 
 
 /* --- functions --- */
+static void
+default_finalize_hook (GHookList *hook_list,
+                      GHook     *hook)
+{
+  GDestroyNotify destroy = hook->destroy;
+
+  if (destroy)
+    {
+      hook->destroy = NULL;
+      destroy (hook->data);
+    }
+}
+
 void
 g_hook_list_init (GHookList *hook_list,
                  guint      hook_size)
 {
   g_return_if_fail (hook_list != NULL);
   g_return_if_fail (hook_size >= sizeof (GHook));
+  g_return_if_fail (hook_size < 65536);
   
   hook_list->seq_id = 1;
   hook_list->hook_size = hook_size;
@@ -42,6 +68,7 @@ g_hook_list_init (GHookList *hook_list,
                                              hook_size,
                                              hook_size * G_HOOKS_PREALLOC,
                                              G_ALLOC_AND_FREE);
+  hook_list->finalize_hook = default_finalize_hook;
 }
 
 void
@@ -64,7 +91,7 @@ g_hook_list_clear (GHookList *hook_list)
       else
        do
          {
-           register GHook *tmp;
+           GHook *tmp;
            
            g_hook_ref (hook_list, hook);
            g_hook_destroy_link (hook_list, hook);
@@ -73,6 +100,8 @@ g_hook_list_clear (GHookList *hook_list)
            hook = tmp;
          }
        while (hook);
+      if (hook_list->hook_memchunk)
+       g_warning (G_STRLOC ": failed to clear hooklist, unconsolidated references on hooks left");
     }
 }
 
@@ -88,7 +117,7 @@ g_hook_alloc (GHookList *hook_list)
   hook->data = NULL;
   hook->next = NULL;
   hook->prev = NULL;
-  hook->flags = G_HOOK_ACTIVE;
+  hook->flags = G_HOOK_FLAG_ACTIVE;
   hook->ref_count = 0;
   hook->hook_id = 0;
   hook->func = NULL;
@@ -105,7 +134,9 @@ g_hook_free (GHookList *hook_list,
   g_return_if_fail (hook_list->is_setup);
   g_return_if_fail (hook != NULL);
   g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
-  
+  g_return_if_fail (!G_HOOK_IN_CALL (hook));
+
+  hook_list->finalize_hook (hook_list, hook);
   g_chunk_free (hook, hook_list->hook_memchunk);
 }
 
@@ -115,26 +146,18 @@ g_hook_destroy_link (GHookList *hook_list,
 {
   g_return_if_fail (hook_list != NULL);
   g_return_if_fail (hook != NULL);
-  
+
+  hook->flags &= ~G_HOOK_FLAG_ACTIVE;
   if (hook->hook_id)
     {
       hook->hook_id = 0;
-      hook->flags &= ~G_HOOK_ACTIVE;
-      if (hook->destroy)
-       {
-         register GDestroyNotify destroy;
-         
-         destroy = hook->destroy;
-         hook->destroy = NULL;
-         destroy (hook->data);
-       }
       g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
     }
 }
 
 gboolean
 g_hook_destroy (GHookList   *hook_list,
-               guint        hook_id)
+               gulong       hook_id)
 {
   GHook *hook;
   
@@ -156,6 +179,7 @@ g_hook_unref (GHookList *hook_list,
              GHook     *hook)
 {
   g_return_if_fail (hook_list != NULL);
+  g_return_if_fail (hook_list->hook_memchunk != NULL);
   g_return_if_fail (hook != NULL);
   g_return_if_fail (hook->ref_count > 0);
   
@@ -163,8 +187,8 @@ g_hook_unref (GHookList *hook_list,
   if (!hook->ref_count)
     {
       g_return_if_fail (hook->hook_id == 0);
-      g_return_if_fail (!G_HOOK_IS_IN_CALL (hook));
-      
+      g_return_if_fail (!G_HOOK_IN_CALL (hook));
+
       if (hook->prev)
        hook->prev->next = hook->next;
       else
@@ -175,15 +199,21 @@ g_hook_unref (GHookList *hook_list,
          hook->next = NULL;
        }
       hook->prev = NULL;
-      
-      g_chunk_free (hook, hook_list->hook_memchunk);
-      
-      if (!hook_list->hooks &&
-         !hook_list->is_setup)
+
+      if (!hook_list->is_setup)
        {
-         g_mem_chunk_destroy (hook_list->hook_memchunk);
-         hook_list->hook_memchunk = NULL;
+         hook_list->is_setup = TRUE;
+         g_hook_free (hook_list, hook);
+         hook_list->is_setup = FALSE;
+      
+         if (!hook_list->hooks)
+           {
+             g_mem_chunk_destroy (hook_list->hook_memchunk);
+             hook_list->hook_memchunk = NULL;
+           }
        }
+      else
+       g_hook_free (hook_list, hook);
     }
 }
 
@@ -216,7 +246,7 @@ g_hook_insert_before (GHookList *hook_list,
   g_return_if_fail (hook_list->is_setup);
   g_return_if_fail (hook != NULL);
   g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
-  g_return_if_fail (hook->func != NULL);
+  g_return_if_fail (hook->ref_count == 0);
   
   hook->hook_id = hook_list->seq_id++;
   hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
@@ -261,36 +291,21 @@ g_hook_list_invoke (GHookList *hook_list,
   g_return_if_fail (hook_list != NULL);
   g_return_if_fail (hook_list->is_setup);
 
-  if (may_recurse)
-    hook = g_hook_first_valid (hook_list);
-  else
-    do
-      hook = g_hook_first_valid (hook_list);
-    while (hook && G_HOOK_IS_IN_CALL (hook));
+  hook = g_hook_first_valid (hook_list, may_recurse);
   while (hook)
     {
-      register GHook *tmp;
-      register GHookFunc func;
+      GHookFunc func;
       gboolean was_in_call;
       
-      g_hook_ref (hook_list, hook);
-      func = hook->func;
+      func = (GHookFunc) hook->func;
       
-      was_in_call = G_HOOK_IS_IN_CALL (hook);
-      hook->flags |= G_HOOK_IN_CALL;
+      was_in_call = G_HOOK_IN_CALL (hook);
+      hook->flags |= G_HOOK_FLAG_IN_CALL;
       func (hook->data);
       if (!was_in_call)
-       hook->flags &= ~G_HOOK_IN_CALL;
-      
-      if (may_recurse)
-       tmp = g_hook_next_valid (hook);
-      else
-       do
-         tmp = g_hook_next_valid (hook);
-       while (tmp && G_HOOK_IS_IN_CALL (tmp));
+       hook->flags &= ~G_HOOK_FLAG_IN_CALL;
       
-      g_hook_unref (hook_list, hook);
-      hook = tmp;
+      hook = g_hook_next_valid (hook_list, hook, may_recurse);
     }
 }
 
@@ -303,39 +318,54 @@ g_hook_list_invoke_check (GHookList *hook_list,
   g_return_if_fail (hook_list != NULL);
   g_return_if_fail (hook_list->is_setup);
   
-  if (may_recurse)
-    hook = g_hook_first_valid (hook_list);
-  else
-    do
-      hook = g_hook_first_valid (hook_list);
-    while (hook && G_HOOK_IS_IN_CALL (hook));
+  hook = g_hook_first_valid (hook_list, may_recurse);
   while (hook)
     {
-      register GHook *tmp;
-      register GHookCheckFunc func;
+      GHookCheckFunc func;
       gboolean was_in_call;
-      register gboolean need_destroy;
+      gboolean need_destroy;
       
-      g_hook_ref (hook_list, hook);
-      func = hook->func;
+      func = (GHookCheckFunc) hook->func;
       
-      was_in_call = G_HOOK_IS_IN_CALL (hook);
-      hook->flags |= G_HOOK_IN_CALL;
+      was_in_call = G_HOOK_IN_CALL (hook);
+      hook->flags |= G_HOOK_FLAG_IN_CALL;
       need_destroy = !func (hook->data);
       if (!was_in_call)
-       hook->flags &= ~G_HOOK_IN_CALL;
+       hook->flags &= ~G_HOOK_FLAG_IN_CALL;
       if (need_destroy)
        g_hook_destroy_link (hook_list, hook);
       
-      if (may_recurse)
-       tmp = g_hook_next_valid (hook);
-      else
-       do
-         tmp = g_hook_next_valid (hook);
-       while (tmp && G_HOOK_IS_IN_CALL (tmp));
+      hook = g_hook_next_valid (hook_list, hook, may_recurse);
+    }
+}
+
+void
+g_hook_list_marshal_check (GHookList          *hook_list,
+                          gboolean             may_recurse,
+                          GHookCheckMarshaller marshaller,
+                          gpointer             data)
+{
+  GHook *hook;
+  
+  g_return_if_fail (hook_list != NULL);
+  g_return_if_fail (hook_list->is_setup);
+  g_return_if_fail (marshaller != NULL);
+  
+  hook = g_hook_first_valid (hook_list, may_recurse);
+  while (hook)
+    {
+      gboolean was_in_call;
+      gboolean need_destroy;
       
-      g_hook_unref (hook_list, hook);
-      hook = tmp;
+      was_in_call = G_HOOK_IN_CALL (hook);
+      hook->flags |= G_HOOK_FLAG_IN_CALL;
+      need_destroy = !marshaller (hook, data);
+      if (!was_in_call)
+       hook->flags &= ~G_HOOK_FLAG_IN_CALL;
+      if (need_destroy)
+       g_hook_destroy_link (hook_list, hook);
+      
+      hook = g_hook_next_valid (hook_list, hook, may_recurse);
     }
 }
 
@@ -351,39 +381,24 @@ g_hook_list_marshal (GHookList                 *hook_list,
   g_return_if_fail (hook_list->is_setup);
   g_return_if_fail (marshaller != NULL);
   
-  if (may_recurse)
-    hook = g_hook_first_valid (hook_list);
-  else
-    do
-      hook = g_hook_first_valid (hook_list);
-    while (hook && G_HOOK_IS_IN_CALL (hook));
+  hook = g_hook_first_valid (hook_list, may_recurse);
   while (hook)
     {
-      register GHook *tmp;
       gboolean was_in_call;
       
-      g_hook_ref (hook_list, hook);
-      
-      was_in_call = G_HOOK_IS_IN_CALL (hook);
-      hook->flags |= G_HOOK_IN_CALL;
+      was_in_call = G_HOOK_IN_CALL (hook);
+      hook->flags |= G_HOOK_FLAG_IN_CALL;
       marshaller (hook, data);
       if (!was_in_call)
-       hook->flags &= ~G_HOOK_IN_CALL;
+       hook->flags &= ~G_HOOK_FLAG_IN_CALL;
       
-      if (may_recurse)
-       tmp = g_hook_next_valid (hook);
-      else
-       do
-         tmp = g_hook_next_valid (hook);
-       while (tmp && G_HOOK_IS_IN_CALL (tmp));
-      
-      g_hook_unref (hook_list, hook);
-      hook = tmp;
+      hook = g_hook_next_valid (hook_list, hook, may_recurse);
     }
 }
 
 GHook*
-g_hook_first_valid (GHookList *hook_list)
+g_hook_first_valid (GHookList *hook_list,
+                   gboolean   may_be_in_call)
 {
   g_return_val_if_fail (hook_list != NULL, NULL);
   
@@ -394,10 +409,11 @@ g_hook_first_valid (GHookList *hook_list)
       hook = hook_list->hooks;
       if (hook)
        {
-         if (G_HOOK_IS_VALID (hook))
+         g_hook_ref (hook_list, hook);
+         if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
            return hook;
          else
-           return g_hook_next_valid (hook);
+           return g_hook_next_valid (hook_list, hook, may_be_in_call);
        }
     }
   
@@ -405,25 +421,37 @@ g_hook_first_valid (GHookList *hook_list)
 }
 
 GHook*
-g_hook_next_valid (GHook *hook)
+g_hook_next_valid (GHookList *hook_list,
+                  GHook     *hook,
+                  gboolean   may_be_in_call)
 {
+  GHook *ohook = hook;
+
+  g_return_val_if_fail (hook_list != NULL, NULL);
+
   if (!hook)
     return NULL;
   
   hook = hook->next;
   while (hook)
     {
-      if (G_HOOK_IS_VALID (hook))
-       return hook;
+      if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
+       {
+         g_hook_ref (hook_list, hook);
+         g_hook_unref (hook_list, ohook);
+         
+         return hook;
+       }
       hook = hook->next;
     }
-  
+  g_hook_unref (hook_list, ohook);
+
   return NULL;
 }
 
 GHook*
 g_hook_get (GHookList *hook_list,
-           guint      hook_id)
+           gulong     hook_id)
 {
   GHook *hook;
   
@@ -452,20 +480,28 @@ g_hook_find (GHookList      *hook_list,
   g_return_val_if_fail (hook_list != NULL, NULL);
   g_return_val_if_fail (func != NULL, NULL);
   
-  hook = g_hook_first_valid (hook_list);
+  hook = hook_list->hooks;
   while (hook)
     {
-      register GHook *tmp;
+      GHook *tmp;
+
+      /* test only non-destroyed hooks */
+      if (!hook->hook_id)
+       {
+         hook = hook->next;
+         continue;
+       }
       
       g_hook_ref (hook_list, hook);
       
-      if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_IS_ACTIVE (hook)))
+      if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
        {
          g_hook_unref (hook_list, hook);
          
          return hook;
        }
-      tmp = g_hook_next_valid (hook);
+
+      tmp = hook->next;
       g_hook_unref (hook_list, hook);
       hook = tmp;
     }
@@ -478,17 +514,20 @@ g_hook_find_data (GHookList *hook_list,
                  gboolean   need_valids,
                  gpointer   data)
 {
-  register GHook *hook;
+  GHook *hook;
   
   g_return_val_if_fail (hook_list != NULL, NULL);
   
-  hook = g_hook_first_valid (hook_list);
+  hook = hook_list->hooks;
   while (hook)
     {
-      if (hook->data == data && hook->hook_id && (!need_valids || G_HOOK_IS_ACTIVE (hook)))
+      /* test only non-destroyed hooks */
+      if (hook->data == data &&
+         hook->hook_id &&
+         (!need_valids || G_HOOK_ACTIVE (hook)))
        return hook;
-      
-      hook = g_hook_next_valid (hook);
+
+      hook = hook->next;
     }
   
   return NULL;
@@ -499,18 +538,21 @@ g_hook_find_func (GHookList *hook_list,
                  gboolean   need_valids,
                  gpointer   func)
 {
-  register GHook *hook;
+  GHook *hook;
   
   g_return_val_if_fail (hook_list != NULL, NULL);
   g_return_val_if_fail (func != NULL, NULL);
   
-  hook = g_hook_first_valid (hook_list);
+  hook = hook_list->hooks;
   while (hook)
     {
-      if (hook->func == func && hook->hook_id && (!need_valids || G_HOOK_IS_ACTIVE (hook)))
+      /* test only non-destroyed hooks */
+      if (hook->func == func &&
+         hook->hook_id &&
+         (!need_valids || G_HOOK_ACTIVE (hook)))
        return hook;
-      
-      hook = g_hook_next_valid (hook);
+
+      hook = hook->next;
     }
   
   return NULL;
@@ -522,18 +564,22 @@ g_hook_find_func_data (GHookList *hook_list,
                       gpointer   func,
                       gpointer   data)
 {
-  register GHook *hook;
+  GHook *hook;
   
   g_return_val_if_fail (hook_list != NULL, NULL);
   g_return_val_if_fail (func != NULL, NULL);
   
-  hook = g_hook_first_valid (hook_list);
+  hook = hook_list->hooks;
   while (hook)
     {
-      if (hook->data == data && hook->func == func && hook->hook_id && (!need_valids || G_HOOK_IS_ACTIVE (hook)))
+      /* test only non-destroyed hooks */
+      if (hook->data == data &&
+         hook->func == func &&
+         hook->hook_id &&
+         (!need_valids || G_HOOK_ACTIVE (hook)))
        return hook;
-      
-      hook = g_hook_next_valid (hook);
+
+      hook = hook->next;
     }
   
   return NULL;
@@ -552,12 +598,15 @@ g_hook_insert_sorted (GHookList         *hook_list,
   g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
   g_return_if_fail (hook->func != NULL);
   g_return_if_fail (func != NULL);
-  
-  sibling = g_hook_first_valid (hook_list);
+
+  /* first non-destroyed hook */
+  sibling = hook_list->hooks;
+  while (sibling && !sibling->hook_id)
+    sibling = sibling->next;
   
   while (sibling)
     {
-      register GHook *tmp;
+      GHook *tmp;
       
       g_hook_ref (hook_list, sibling);
       if (func (hook, sibling) <= 0 && sibling->hook_id)
@@ -565,7 +614,12 @@ g_hook_insert_sorted (GHookList          *hook_list,
          g_hook_unref (hook_list, sibling);
          break;
        }
-      tmp = g_hook_next_valid (sibling);
+
+      /* next non-destroyed hook */
+      tmp = sibling->next;
+      while (tmp && !tmp->hook_id)
+       tmp = tmp->next;
+
       g_hook_unref (hook_list, sibling);
       sibling = tmp;
     }