destruction cleanup. there's one ->finalize_hook member in the hooklist
[platform/upstream/glib.git] / gobject / gsignal.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000-2001 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * this code is based on the original GtkSignal implementation
20  * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
21  */
22
23 /*
24  * MT safe
25  */
26
27 #include        "gsignal.h"
28 #include        "gbsearcharray.h"
29 #include        "gvaluecollector.h"
30 #include        "gvaluetypes.h"
31 #include        "gboxed.h"
32 #include        <string.h> 
33
34
35 /* pre allocation configurations
36  */
37 #define MAX_STACK_VALUES        (16)
38 #define BSA_PRE_ALLOC           (20)
39 #define HANDLER_PRE_ALLOC       (48)
40 #define EMISSION_PRE_ALLOC      (16)
41
42 #define REPORT_BUG      "please report occourance circumstances to gtk-devel-list@gnome.org"
43 #ifdef  G_ENABLE_DEBUG
44 #define IF_DEBUG(debug_type, cond)      if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
45 static volatile gpointer *g_trace_instance_signals = NULL;
46 static volatile gpointer *g_trap_instance_signals = NULL;
47 #endif  /* G_ENABLE_DEBUG */
48
49
50 /* --- generic allocation --- */
51 /* we special case allocations generically by replacing
52  * these functions with more speed/memory aware variants
53  */
54 #ifndef DISABLE_MEM_POOLS
55 static inline gpointer
56 g_generic_node_alloc (GTrashStack **trash_stack_p,
57                       guint         sizeof_node,
58                       guint         nodes_pre_alloc)
59 {
60   gpointer node = g_trash_stack_pop (trash_stack_p);
61   
62   if (!node)
63     {
64       guint8 *block;
65       
66       nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
67       block = g_malloc (sizeof_node * nodes_pre_alloc);
68       while (--nodes_pre_alloc)
69         {
70           g_trash_stack_push (trash_stack_p, block);
71           block += sizeof_node;
72         }
73       node = block;
74     }
75   
76   return node;
77 }
78 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
79 #else   /* !DISABLE_MEM_POOLS */
80 #define g_generic_node_alloc(t,sizeof_node,p)    g_malloc (sizeof_node)
81 #define g_generic_node_free(t,node)              g_free (node)
82 #endif  /* !DISABLE_MEM_POOLS */
83
84
85 /* --- typedefs --- */
86 typedef struct _SignalNode   SignalNode;
87 typedef struct _SignalKey    SignalKey;
88 typedef struct _Emission     Emission;
89 typedef struct _Handler      Handler;
90 typedef struct _HandlerList  HandlerList;
91 typedef struct _HandlerMatch HandlerMatch;
92 typedef enum
93 {
94   EMISSION_STOP,
95   EMISSION_RUN,
96   EMISSION_HOOK,
97   EMISSION_RESTART
98 } EmissionState;
99
100
101 /* --- prototypes --- */
102 static inline guint             signal_id_lookup        (GQuark           quark,
103                                                          GType            itype);
104 static        void              signal_destroy_R        (SignalNode      *signal_node);
105 static inline HandlerList*      handler_list_ensure     (guint            signal_id,
106                                                          gpointer         instance);
107 static inline HandlerList*      handler_list_lookup     (guint            signal_id,
108                                                          gpointer         instance);
109 static inline Handler*          handler_new             (gboolean         after);
110 static        void              handler_insert          (guint            signal_id,
111                                                          gpointer         instance,
112                                                          Handler         *handler);
113 static        Handler*          handler_lookup          (gpointer         instance,
114                                                          guint            handler_id,
115                                                          guint           *signal_id_p);
116 static inline HandlerMatch*     handler_match_prepend   (HandlerMatch    *list,
117                                                          Handler         *handler,
118                                                          guint            signal_id);
119 static inline HandlerMatch*     handler_match_free1_R   (HandlerMatch    *node,
120                                                          gpointer         instance);
121 static        HandlerMatch*     handlers_find           (gpointer         instance,
122                                                          GSignalMatchType mask,
123                                                          guint            signal_id,
124                                                          GQuark           detail,
125                                                          GClosure        *closure,
126                                                          gpointer         func,
127                                                          gpointer         data,
128                                                          gboolean         one_and_only);
129 static inline void              handler_ref             (Handler         *handler);
130 static inline void              handler_unref_R         (guint            signal_id,
131                                                          gpointer         instance,
132                                                          Handler         *handler);
133 static inline void              emission_push           (Emission       **emission_list_p,
134                                                          guint            signal_id,
135                                                          GQuark           detail,
136                                                          gpointer         instance,
137                                                          EmissionState   *state_p);
138 static inline void              emission_pop            (Emission       **emission_list_p,
139                                                          EmissionState   *state_p);
140 static inline Emission*         emission_find           (Emission        *emission_list,
141                                                          guint            signal_id,
142                                                          GQuark           detail,
143                                                          gpointer         instance);
144 static        gboolean          signal_emit_R           (SignalNode      *node,
145                                                          GQuark           detail,
146                                                          gpointer         instance,
147                                                          GValue          *return_value,
148                                                          const GValue    *instance_and_params);
149
150
151 /* --- structures --- */
152 typedef struct
153 {
154   GSignalAccumulator func;
155   gpointer           data;
156 } SignalAccumulator;
157 struct _SignalNode
158 {
159   /* permanent portion */
160   guint              signal_id;
161   GType              itype;
162   gchar             *name;
163   guint              destroyed : 1;
164   
165   /* reinitializable portion */
166   guint              flags : 8;
167   guint              n_params : 8;
168   GType             *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
169   GType              return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
170   GClosure          *class_closure;
171   SignalAccumulator *accumulator;
172   GSignalCMarshaller c_marshaller;
173   GHookList         *emission_hooks;
174 };
175
176 struct _SignalKey
177 {
178   GType  itype;
179   GQuark quark;
180   guint  signal_id;
181 };
182
183 struct _Emission
184 {
185   Emission      *next;
186   guint          signal_id;
187   GQuark         detail;
188   gpointer       instance;
189   EmissionState *state_p;
190 };
191
192 struct _HandlerList
193 {
194   guint    signal_id;
195   Handler *handlers;
196 };
197 struct _Handler
198 {
199   guint         id;
200   Handler      *next;
201   Handler      *prev;
202   GQuark        detail;
203   guint         ref_count : 16;
204 #define HANDLER_MAX_REF_COUNT   (1 << 16)
205   guint         block_count : 12;
206 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
207   guint         after : 1;
208   GClosure     *closure;
209 };
210 struct _HandlerMatch
211 {
212   Handler      *handler;
213   HandlerMatch *next;
214   union {
215     guint       signal_id;
216     gpointer    dummy;
217   } d;
218 };
219
220
221 /* --- variables --- */
222 static GBSearchArray  g_signal_key_bsa = { NULL, 0, 0, 0, NULL };
223 static GHashTable    *g_handler_list_bsa_ht = NULL;
224 static Emission      *g_recursive_emissions = NULL;
225 static Emission      *g_restart_emissions = NULL;
226 static GTrashStack   *g_bsa_ts = NULL;
227 static GTrashStack   *g_handler_ts = NULL;
228 static GTrashStack   *g_emission_ts = NULL;
229 G_LOCK_DEFINE_STATIC (g_signal_mutex);
230
231
232 /* --- signal nodes --- */
233 static guint          g_n_signal_nodes = 0;
234 static SignalNode   **g_signal_nodes = NULL;
235
236 static inline SignalNode*
237 LOOKUP_SIGNAL_NODE (register guint signal_id)
238 {
239   if (signal_id < g_n_signal_nodes)
240     return g_signal_nodes[signal_id];
241   else
242     return NULL;
243 }
244
245
246 /* --- functions --- */
247 static inline guint
248 signal_id_lookup (GQuark quark,
249                   GType  itype)
250 {
251   GType *ifaces, type = itype;
252   SignalKey key;
253   guint n_ifaces;
254
255   key.quark = quark;
256
257   /* try looking up signals for this type and its anchestors */
258   do
259     {
260       SignalKey *signal_key;
261       
262       key.itype = type;
263       signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
264       
265       if (signal_key)
266         return signal_key->signal_id;
267       
268       type = g_type_parent (type);
269     }
270   while (type);
271
272   /* no luck, try interfaces it exports */
273   ifaces = g_type_interfaces (itype, &n_ifaces);
274   while (n_ifaces--)
275     {
276       SignalKey *signal_key;
277
278       key.itype = ifaces[n_ifaces];
279       signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
280
281       if (signal_key)
282         {
283           g_free (ifaces);
284           return signal_key->signal_id;
285         }
286     }
287   g_free (ifaces);
288   
289   return 0;
290 }
291
292 static gint
293 handler_lists_cmp (gconstpointer node1,
294                    gconstpointer node2)
295 {
296   const HandlerList *hlist1 = node1, *hlist2 = node2;
297   
298   return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
299 }
300
301 static inline HandlerList*
302 handler_list_ensure (guint    signal_id,
303                      gpointer instance)
304 {
305   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
306   HandlerList key;
307   
308   if (!hlbsa)
309     {
310       hlbsa = g_generic_node_alloc (&g_bsa_ts,
311                                     sizeof (GBSearchArray),
312                                     BSA_PRE_ALLOC);
313       hlbsa->cmp_func = handler_lists_cmp;
314       hlbsa->sizeof_node = sizeof (HandlerList);
315       hlbsa->flags = G_BSEARCH_DEFER_SHRINK;
316       hlbsa->n_nodes = 0;
317       hlbsa->nodes = NULL;
318       g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
319     }
320   key.signal_id = signal_id;
321   key.handlers = NULL;
322   
323   return g_bsearch_array_insert (hlbsa, &key, FALSE);
324 }
325
326 static inline HandlerList*
327 handler_list_lookup (guint    signal_id,
328                      gpointer instance)
329 {
330   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
331   HandlerList key;
332   
333   key.signal_id = signal_id;
334   
335   return hlbsa ? g_bsearch_array_lookup (hlbsa, &key) : NULL;
336 }
337
338 static Handler*
339 handler_lookup (gpointer instance,
340                 guint    handler_id,
341                 guint   *signal_id_p)
342 {
343   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
344   
345   if (hlbsa)
346     {
347       guint i;
348       
349       for (i = 0; i < hlbsa->n_nodes; i++)
350         {
351           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
352           Handler *handler;
353           
354           for (handler = hlist->handlers; handler; handler = handler->next)
355             if (handler->id == handler_id)
356               {
357                 if (signal_id_p)
358                   *signal_id_p = hlist->signal_id;
359                 
360                 return handler;
361               }
362         }
363     }
364   
365   return NULL;
366 }
367
368 static inline HandlerMatch*
369 handler_match_prepend (HandlerMatch *list,
370                        Handler      *handler,
371                        guint         signal_id)
372 {
373   HandlerMatch *node;
374   
375   /* yeah, we could use our own memchunk here, introducing yet more
376    * rarely used cached nodes and extra allocation overhead.
377    * instead, we use GList* nodes, since they are exactly the size
378    * we need and are already cached. g_signal_init() asserts this.
379    */
380   node = (HandlerMatch*) g_list_alloc ();
381   node->handler = handler;
382   node->next = list;
383   node->d.signal_id = signal_id;
384   handler_ref (handler);
385   
386   return node;
387 }
388 static inline HandlerMatch*
389 handler_match_free1_R (HandlerMatch *node,
390                        gpointer      instance)
391 {
392   HandlerMatch *next = node->next;
393   
394   handler_unref_R (node->d.signal_id, instance, node->handler);
395   g_list_free_1 ((GList*) node);
396   
397   return next;
398 }
399
400 static HandlerMatch*
401 handlers_find (gpointer         instance,
402                GSignalMatchType mask,
403                guint            signal_id,
404                GQuark           detail,
405                GClosure        *closure,
406                gpointer         func,
407                gpointer         data,
408                gboolean         one_and_only)
409 {
410   HandlerMatch *mlist = NULL;
411   
412   if (mask & G_SIGNAL_MATCH_ID)
413     {
414       HandlerList *hlist = handler_list_lookup (signal_id, instance);
415       Handler *handler;
416       SignalNode *node = NULL;
417       
418       if (mask & G_SIGNAL_MATCH_FUNC)
419         {
420           node = LOOKUP_SIGNAL_NODE (signal_id);
421           if (!node || !node->c_marshaller)
422             return NULL;
423         }
424       
425       mask = ~mask;
426       for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
427         if (handler->id &&
428             ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
429             ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
430             ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
431             ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
432             ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
433                                               handler->closure->meta_marshal == 0 &&
434                                               ((GCClosure*) handler->closure)->callback == func)))
435           {
436             mlist = handler_match_prepend (mlist, handler, signal_id);
437             if (one_and_only)
438               return mlist;
439           }
440     }
441   else
442     {
443       GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
444       
445       mask = ~mask;
446       if (hlbsa)
447         {
448           guint i;
449           
450           for (i = 0; i < hlbsa->n_nodes; i++)
451             {
452               HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
453               SignalNode *node = NULL;
454               Handler *handler;
455               
456               if (!(mask & G_SIGNAL_MATCH_FUNC))
457                 {
458                   node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
459                   if (!node->c_marshaller)
460                     continue;
461                 }
462               
463               for (handler = hlist->handlers; handler; handler = handler->next)
464                 if (handler->id &&
465                     ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
466                     ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
467                     ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
468                     ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
469                     ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
470                                                       handler->closure->meta_marshal == 0 &&
471                                                       ((GCClosure*) handler->closure)->callback == func)))
472                   {
473                     mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
474                     if (one_and_only)
475                       return mlist;
476                   }
477             }
478         }
479     }
480   
481   return mlist;
482 }
483
484 static inline Handler*
485 handler_new (gboolean after)
486 {
487   static guint handler_id = 1;
488   Handler *handler = g_generic_node_alloc (&g_handler_ts,
489                                            sizeof (Handler),
490                                            HANDLER_PRE_ALLOC);
491 #ifndef G_DISABLE_CHECKS
492   if (handler_id == 0)
493     g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
494 #endif
495   
496   handler->id = handler_id++;
497   handler->prev = NULL;
498   handler->next = NULL;
499   handler->detail = 0;
500   handler->ref_count = 1;
501   handler->block_count = 0;
502   handler->after = after != FALSE;
503   handler->closure = NULL;
504   
505   return handler;
506 }
507
508 static inline void
509 handler_ref (Handler *handler)
510 {
511   g_return_if_fail (handler->ref_count > 0);
512   
513 #ifndef G_DISABLE_CHECKS
514   if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
515     g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
516 #endif
517   
518   handler->ref_count += 1;
519 }
520
521 static inline void
522 handler_unref_R (guint    signal_id,
523                  gpointer instance,
524                  Handler *handler)
525 {
526   g_return_if_fail (handler->ref_count > 0);
527   
528   handler->ref_count -= 1;
529   if (!handler->ref_count)
530     {
531       if (handler->next)
532         handler->next->prev = handler->prev;
533       if (handler->prev)        /* watch out for g_signal_handlers_destroy()! */
534         handler->prev->next = handler->next;
535       else
536         {
537           HandlerList *hlist = handler_list_lookup (signal_id, instance);
538           
539           hlist->handlers = handler->next;
540         }
541       G_UNLOCK (g_signal_mutex);
542       g_closure_unref (handler->closure);
543       G_LOCK (g_signal_mutex);
544       g_generic_node_free (&g_handler_ts, handler);
545     }
546 }
547
548 static void
549 handler_insert (guint    signal_id,
550                 gpointer instance,
551                 Handler  *handler)
552 {
553   HandlerList *hlist;
554   
555   g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
556   
557   hlist = handler_list_ensure (signal_id, instance);
558   if (!hlist->handlers)
559     hlist->handlers = handler;
560   else if (hlist->handlers->after && !handler->after)
561     {
562       handler->next = hlist->handlers;
563       hlist->handlers->prev = handler;
564       hlist->handlers = handler;
565     }
566   else
567     {
568       Handler *tmp = hlist->handlers;
569       
570       if (handler->after)
571         while (tmp->next)
572           tmp = tmp->next;
573       else
574         while (tmp->next && !tmp->next->after)
575           tmp = tmp->next;
576       if (tmp->next)
577         tmp->next->prev = handler;
578       handler->next = tmp->next;
579       handler->prev = tmp;
580       tmp->next = handler;
581     }
582 }
583
584 static inline void
585 emission_push (Emission     **emission_list_p,
586                guint          signal_id,
587                GQuark         detail,
588                gpointer       instance,
589                EmissionState *state_p)
590 {
591   Emission *emission = g_generic_node_alloc (&g_emission_ts,
592                                              sizeof (Emission),
593                                              EMISSION_PRE_ALLOC);
594   emission->next = *emission_list_p;
595   emission->signal_id = signal_id;
596   emission->detail = detail;
597   emission->instance = instance;
598   emission->state_p = state_p;
599   *emission_list_p = emission;
600 }
601
602 static inline void
603 emission_pop (Emission     **emission_list_p,
604               EmissionState *state_p)
605 {
606   Emission **loc = emission_list_p, *emission = *loc;
607   
608   while (emission->state_p != state_p)
609     {
610       loc = &emission->next;
611       emission = *loc;
612     }
613   *loc = emission->next;
614   g_generic_node_free (&g_emission_ts, emission);
615 }
616
617 static inline Emission*
618 emission_find (Emission *emission_list,
619                guint     signal_id,
620                GQuark    detail,
621                gpointer  instance)
622 {
623   Emission *emission;
624   
625   for (emission = emission_list; emission; emission = emission->next)
626     if (emission->instance == instance &&
627         emission->signal_id == signal_id &&
628         emission->detail == detail)
629       return emission;
630   return NULL;
631 }
632
633 static gint
634 signal_key_cmp (gconstpointer node1,
635                 gconstpointer node2)
636 {
637   const SignalKey *key1 = node1, *key2 = node2;
638   
639   if (key1->itype == key2->itype)
640     return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
641   else
642     return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
643 }
644
645 void
646 g_signal_init (void) /* sync with gtype.c */
647 {
648   G_LOCK (g_signal_mutex);
649   if (!g_n_signal_nodes)
650     {
651       /* handler_id_node_prepend() requires this */
652       g_assert (sizeof (GList) == sizeof (HandlerMatch));
653       
654       /* setup signal key array */
655       g_signal_key_bsa.cmp_func = signal_key_cmp;
656       g_signal_key_bsa.sizeof_node = sizeof (SignalKey);
657       g_signal_key_bsa.flags = G_BSEARCH_ALIGN_POWER2; /* alloc-only */
658       
659       /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
660       g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
661       
662       /* invalid (0) signal_id */
663       g_n_signal_nodes = 1;
664       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
665       g_signal_nodes[0] = NULL;
666     }
667   G_UNLOCK (g_signal_mutex);
668 }
669
670 void
671 _g_signals_destroy (GType itype)
672 {
673   guint i;
674   
675   G_LOCK (g_signal_mutex);
676   for (i = 1; i < g_n_signal_nodes; i++)
677     {
678       SignalNode *node = g_signal_nodes[i];
679       
680       if (node->itype == itype)
681         {
682           if (node->destroyed)
683             g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
684                        node->name,
685                        g_type_name (node->itype));
686           else
687             signal_destroy_R (node);
688         }
689     }
690   G_UNLOCK (g_signal_mutex);
691 }
692
693 void
694 g_signal_stop_emission (gpointer instance,
695                         guint    signal_id,
696                         GQuark   detail)
697 {
698   SignalNode *node;
699   
700   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
701   g_return_if_fail (signal_id > 0);
702   
703   G_LOCK (g_signal_mutex);
704   node = LOOKUP_SIGNAL_NODE (signal_id);
705   if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
706     {
707       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
708       G_UNLOCK (g_signal_mutex);
709       return;
710     }
711   if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
712     {
713       Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
714       Emission *emission = emission_find (emission_list, signal_id, detail, instance);
715       
716       if (emission)
717         {
718           if (*emission->state_p == EMISSION_HOOK)
719             g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
720                        node->name, instance);
721           else if (*emission->state_p == EMISSION_RUN)
722             *emission->state_p = EMISSION_STOP;
723         }
724       else
725         g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
726                    node->name, instance);
727     }
728   else
729     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
730   G_UNLOCK (g_signal_mutex);
731 }
732
733 static void
734 signal_finalize_hook (GHookList *hook_list,
735                       GHook     *hook)
736 {
737   GDestroyNotify destroy = hook->destroy;
738
739   if (destroy)
740     {
741       hook->destroy = NULL;
742       G_UNLOCK (g_signal_mutex);
743       destroy (hook->data);
744       G_LOCK (g_signal_mutex);
745     }
746 }
747
748 guint
749 g_signal_add_emission_hook (guint               signal_id,
750                             GQuark              detail,
751                             GSignalEmissionHook hook_func,
752                             gpointer            hook_data,
753                             GDestroyNotify      data_destroy)
754 {
755   static guint seq_hook_id = 1;
756   SignalNode *node;
757   GHook *hook;
758
759   g_return_val_if_fail (signal_id > 0, 0);
760   g_return_val_if_fail (hook_func != NULL, 0);
761
762   G_LOCK (g_signal_mutex);
763   node = LOOKUP_SIGNAL_NODE (signal_id);
764   if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
765     {
766       g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
767       G_UNLOCK (g_signal_mutex);
768       return 0;
769     }
770   if (detail && !(node->flags & G_SIGNAL_DETAILED))
771     {
772       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
773       G_UNLOCK (g_signal_mutex);
774       return 0;
775     }
776   if (!node->emission_hooks)
777     {
778       node->emission_hooks = g_new (GHookList, 1);
779       g_hook_list_init (node->emission_hooks, sizeof (GHook));
780       node->emission_hooks->finalize_hook = signal_finalize_hook;
781     }
782   hook = g_hook_alloc (node->emission_hooks);
783   hook->data = hook_data;
784   hook->func = hook_func;
785   hook->destroy = data_destroy;
786   node->emission_hooks->seq_id = seq_hook_id;
787   g_hook_append (node->emission_hooks, hook);
788   seq_hook_id = node->emission_hooks->seq_id;
789   G_UNLOCK (g_signal_mutex);
790
791   return hook->hook_id;
792 }
793
794 void
795 g_signal_remove_emission_hook (guint signal_id,
796                                guint hook_id)
797 {
798   SignalNode *node;
799
800   g_return_if_fail (signal_id > 0);
801   g_return_if_fail (hook_id > 0);
802
803   G_LOCK (g_signal_mutex);
804   node = LOOKUP_SIGNAL_NODE (signal_id);
805   if (!node || node->destroyed)
806     g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
807   else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
808     g_warning ("%s: signal \"%s\" had no hook (%u) to remove", G_STRLOC, node->name, hook_id);
809   G_UNLOCK (g_signal_mutex);
810 }
811
812 static inline guint
813 signal_parse_name (const gchar *name,
814                    GType        itype,
815                    GQuark      *detail_p,
816                    gboolean     force_quark)
817 {
818   const gchar *colon = strchr (name, ':');
819   guint signal_id;
820   
821   if (!colon)
822     {
823       signal_id = signal_id_lookup (g_quark_try_string (name), itype);
824       if (signal_id && detail_p)
825         *detail_p = 0;
826     }
827   else if (colon[1] == ':')
828     {
829       gchar buffer[32];
830       guint l = colon - name;
831       
832       if (l < 32)
833         {
834           memcpy (buffer, name, l);
835           buffer[l] = 0;
836           signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
837         }
838       else
839         {
840           gchar *signal = g_new (gchar, l + 1);
841           
842           memcpy (signal, name, l);
843           signal[l] = 0;
844           signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
845           g_free (signal);
846         }
847       
848       if (signal_id && detail_p)
849         *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
850     }
851   else
852     signal_id = 0;
853   return signal_id;
854 }
855
856 gboolean
857 g_signal_parse_name (const gchar *detailed_signal,
858                      GType        itype,
859                      guint       *signal_id_p,
860                      GQuark      *detail_p,
861                      gboolean     force_detail_quark)
862 {
863   SignalNode *node;
864   GQuark detail = 0;
865   guint signal_id;
866   
867   g_return_val_if_fail (detailed_signal != NULL, FALSE);
868   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
869   
870   G_LOCK (g_signal_mutex);
871   signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
872   G_UNLOCK (g_signal_mutex);
873
874   node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
875   if (!node || node->destroyed ||
876       (detail && !(node->flags & G_SIGNAL_DETAILED)))
877     return FALSE;
878
879   if (signal_id_p)
880     *signal_id_p = signal_id;
881   if (detail_p)
882     *detail_p = detail;
883   
884   return TRUE;
885 }
886
887 guint
888 g_signal_lookup (const gchar *name,
889                  GType        itype)
890 {
891   guint signal_id;
892   
893   g_return_val_if_fail (name != NULL, 0);
894   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
895   
896   G_LOCK (g_signal_mutex);
897   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
898   G_UNLOCK (g_signal_mutex);
899   
900   return signal_id;
901 }
902
903 G_CONST_RETURN gchar*
904 g_signal_name (guint signal_id)
905 {
906   SignalNode *node;
907   gchar *name;
908   
909   G_LOCK (g_signal_mutex);
910   node = LOOKUP_SIGNAL_NODE (signal_id);
911   name = node ? node->name : NULL;
912   G_UNLOCK (g_signal_mutex);
913   
914   return name;
915 }
916
917 void
918 g_signal_query (guint         signal_id,
919                 GSignalQuery *query)
920 {
921   SignalNode *node;
922   
923   g_return_if_fail (query != NULL);
924   
925   G_LOCK (g_signal_mutex);
926   node = LOOKUP_SIGNAL_NODE (signal_id);
927   if (!node || node->destroyed)
928     query->signal_id = 0;
929   else
930     {
931       query->signal_id = node->signal_id;
932       query->signal_name = node->name;
933       query->itype = node->itype;
934       query->signal_flags = node->flags;
935       query->return_type = node->return_type;
936       query->n_params = node->n_params;
937       query->param_types = node->param_types;
938     }
939   G_UNLOCK (g_signal_mutex);
940 }
941
942 guint*
943 g_signal_list_ids (GType  itype,
944                    guint *n_ids)
945 {
946   SignalKey *keys;
947   GArray *result;
948   guint n_nodes;
949   guint i;
950   
951   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
952   g_return_val_if_fail (n_ids != NULL, NULL);
953   
954   G_LOCK (g_signal_mutex);
955   
956   keys = g_signal_key_bsa.nodes;
957   n_nodes  = g_signal_key_bsa.n_nodes;
958   result = g_array_new (FALSE, FALSE, sizeof (guint));
959   
960   for (i = 0; i < n_nodes; i++)
961     if (keys[i].itype == itype)
962       {
963         const gchar *name = g_quark_to_string (keys[i].quark);
964         
965         /* Signal names with "_" in them are aliases to the same
966          * name with "-" instead of "_".
967          */
968         if (!strchr (name, '_'))
969           g_array_append_val (result, keys[i].signal_id);
970       }
971   
972   *n_ids = result->len;
973   
974   G_UNLOCK (g_signal_mutex);
975   
976   return (guint *) g_array_free (result, FALSE);
977 }
978
979 guint
980 g_signal_new_valist (const gchar       *signal_name,
981                      GType              itype,
982                      GSignalFlags       signal_flags,
983                      GClosure          *class_closure,
984                      GSignalAccumulator accumulator,
985                      gpointer           accu_data,
986                      GSignalCMarshaller c_marshaller,
987                      GType              return_type,
988                      guint              n_params,
989                      va_list            args)
990 {
991   GType *param_types;
992   guint i;
993   guint signal_id;
994
995   if (n_params > 0)
996     {
997       param_types = g_new (GType, n_params);
998
999       for (i = 0; i < n_params; i++)
1000         param_types[i] = va_arg (args, GType);
1001     }
1002   else
1003     param_types = NULL;
1004
1005   signal_id = g_signal_newv (signal_name, itype, signal_flags,
1006                              class_closure, accumulator, accu_data, c_marshaller,
1007                              return_type, n_params, param_types);
1008   g_free (param_types);
1009
1010   return signal_id;
1011 }
1012
1013 guint
1014 g_signal_newc (const gchar       *signal_name,
1015                GType              itype,
1016                GSignalFlags       signal_flags,
1017                guint              class_offset,
1018                GSignalAccumulator accumulator,
1019                gpointer           accu_data,
1020                GSignalCMarshaller c_marshaller,
1021                GType              return_type,
1022                guint              n_params,
1023                ...)
1024 {
1025   va_list args;
1026   guint signal_id;
1027
1028   g_return_val_if_fail (signal_name != NULL, 0);
1029   
1030   va_start (args, n_params);
1031
1032   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1033                                    g_signal_type_cclosure_new (itype, class_offset),
1034                                    accumulator, accu_data, c_marshaller,
1035                                    return_type, n_params, args);
1036
1037   va_end (args);
1038  
1039   return signal_id;
1040 }
1041
1042 guint
1043 g_signal_newv (const gchar       *signal_name,
1044                GType              itype,
1045                GSignalFlags       signal_flags,
1046                GClosure          *class_closure,
1047                GSignalAccumulator accumulator,
1048                gpointer           accu_data,
1049                GSignalCMarshaller c_marshaller,
1050                GType              return_type,
1051                guint              n_params,
1052                GType             *param_types)
1053 {
1054   gchar *name;
1055   guint signal_id, i;
1056   SignalNode *node;
1057   
1058   g_return_val_if_fail (signal_name != NULL, 0);
1059   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1060   if (n_params)
1061     g_return_val_if_fail (param_types != NULL, 0);
1062   if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1063     g_return_val_if_fail (accumulator == NULL, 0);
1064   if (!accumulator)
1065     g_return_val_if_fail (accu_data == NULL, 0);
1066
1067   name = g_strdup (signal_name);
1068   g_strdelimit (name, G_STR_DELIMITERS ":^", '_');  // FIXME do character checks like for types
1069   
1070   G_LOCK (g_signal_mutex);
1071   
1072   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1073   node = LOOKUP_SIGNAL_NODE (signal_id);
1074   if (node && !node->destroyed)
1075     {
1076       g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1077                  name,
1078                  g_type_name (node->itype),
1079                  G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1080       g_free (name);
1081       G_UNLOCK (g_signal_mutex);
1082       return 0;
1083     }
1084   if (node && node->itype != itype)
1085     {
1086       g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1087                  name,
1088                  g_type_name (itype),
1089                  g_type_name (node->itype));
1090       g_free (name);
1091       G_UNLOCK (g_signal_mutex);
1092       return 0;
1093     }
1094   for (i = 0; i < n_params; i++)
1095     if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1096       {
1097         g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1098                    i + 1, g_type_name (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1099         g_free (name);
1100         G_UNLOCK (g_signal_mutex);
1101         return 0;
1102       }
1103   if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1104     {
1105       g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1106                  g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1107       g_free (name);
1108       G_UNLOCK (g_signal_mutex);
1109       return 0;
1110     }
1111   
1112   /* setup permanent portion of signal node */
1113   if (!node)
1114     {
1115       SignalKey key;
1116       
1117       signal_id = g_n_signal_nodes++;
1118       node = g_new (SignalNode, 1);
1119       node->signal_id = signal_id;
1120       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1121       g_signal_nodes[signal_id] = node;
1122       node->itype = itype;
1123       node->name = name;
1124       key.itype = itype;
1125       key.quark = g_quark_from_string (node->name);
1126       key.signal_id = signal_id;
1127       g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1128       g_strdelimit (node->name, "_", '-');
1129       key.quark = g_quark_from_static_string (node->name);
1130       g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1131     }
1132   node->destroyed = FALSE;
1133   
1134   /* setup reinitializable portion */
1135   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1136   node->n_params = n_params;
1137   node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1138   node->return_type = return_type;
1139   node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
1140   if (class_closure)
1141     g_closure_sink (class_closure);
1142   if (accumulator)
1143     {
1144       node->accumulator = g_new (SignalAccumulator, 1);
1145       node->accumulator->func = accumulator;
1146       node->accumulator->data = accu_data;
1147     }
1148   else
1149     node->accumulator = NULL;
1150   node->c_marshaller = c_marshaller;
1151   node->emission_hooks = NULL;
1152   if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
1153     g_closure_set_marshal (class_closure, node->c_marshaller);
1154   G_UNLOCK (g_signal_mutex);
1155   return signal_id;
1156 }
1157
1158 static void
1159 signal_destroy_R (SignalNode *signal_node)
1160 {
1161   SignalNode node = *signal_node;
1162   
1163   signal_node->destroyed = TRUE;
1164   
1165   /* reentrancy caution, zero out real contents first */
1166   signal_node->n_params = 0;
1167   signal_node->param_types = NULL;
1168   signal_node->return_type = 0;
1169   signal_node->class_closure = NULL;
1170   signal_node->accumulator = NULL;
1171   signal_node->c_marshaller = NULL;
1172   signal_node->emission_hooks = NULL;
1173   
1174 #ifdef  G_ENABLE_DEBUG
1175   /* check current emissions */
1176   {
1177     Emission *emission;
1178     
1179     for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1180          emission; emission = emission->next)
1181       if (emission->signal_id == node.signal_id)
1182         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1183                     node.name, emission->instance);
1184   }
1185 #endif
1186   
1187   /* free contents that need to
1188    */
1189   G_UNLOCK (g_signal_mutex);
1190   g_free (node.param_types);
1191   g_closure_unref (node.class_closure);
1192   g_free (node.accumulator);
1193   if (node.emission_hooks)
1194     {
1195       g_hook_list_clear (node.emission_hooks);
1196       g_free (node.emission_hooks);
1197     }
1198   G_LOCK (g_signal_mutex);
1199 }
1200
1201 guint
1202 g_signal_connect_closure_by_id (gpointer  instance,
1203                                 guint     signal_id,
1204                                 GQuark    detail,
1205                                 GClosure *closure,
1206                                 gboolean  after)
1207 {
1208   SignalNode *node;
1209   guint handler_id = 0;
1210   
1211   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1212   g_return_val_if_fail (signal_id > 0, 0);
1213   g_return_val_if_fail (closure != NULL, 0);
1214   
1215   G_LOCK (g_signal_mutex);
1216   node = LOOKUP_SIGNAL_NODE (signal_id);
1217   if (node)
1218     {
1219       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1220         g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1221       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1222         g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1223       else
1224         {
1225           Handler *handler = handler_new (after);
1226           
1227           handler_id = handler->id;
1228           handler->detail = detail;
1229           handler->closure = g_closure_ref (closure);
1230           g_closure_sink (closure);
1231           handler_insert (signal_id, instance, handler);
1232           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1233             g_closure_set_marshal (closure, node->c_marshaller);
1234         }
1235     }
1236   else
1237     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1238   G_UNLOCK (g_signal_mutex);
1239   
1240   return handler_id;
1241 }
1242
1243 guint
1244 g_signal_connect_closure (gpointer     instance,
1245                           const gchar *detailed_signal,
1246                           GClosure    *closure,
1247                           gboolean     after)
1248 {
1249   guint signal_id, handler_id = 0;
1250   GQuark detail = 0;
1251   GType itype;
1252
1253   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1254   g_return_val_if_fail (detailed_signal != NULL, 0);
1255   g_return_val_if_fail (closure != NULL, 0);
1256
1257   G_LOCK (g_signal_mutex);
1258   itype = G_TYPE_FROM_INSTANCE (instance);
1259   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1260   if (signal_id)
1261     {
1262       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1263
1264       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1265         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1266       else if (!g_type_is_a (itype, node->itype))
1267         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1268       else
1269         {
1270           Handler *handler = handler_new (after);
1271
1272           handler_id = handler->id;
1273           handler->detail = detail;
1274           handler->closure = g_closure_ref (closure);
1275           g_closure_sink (closure);
1276           handler_insert (signal_id, instance, handler);
1277           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1278             g_closure_set_marshal (handler->closure, node->c_marshaller);
1279         }
1280     }
1281   else
1282     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1283   G_UNLOCK (g_signal_mutex);
1284
1285   return handler_id;
1286 }
1287
1288 guint
1289 g_signal_connect_data (gpointer       instance,
1290                        const gchar   *detailed_signal,
1291                        GCallback      c_handler,
1292                        gpointer       data,
1293                        GClosureNotify destroy_data,
1294                        gboolean       swapped,
1295                        gboolean       after)
1296 {
1297   guint signal_id, handler_id = 0;
1298   GQuark detail = 0;
1299   GType itype;
1300
1301   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1302   g_return_val_if_fail (detailed_signal != NULL, 0);
1303   g_return_val_if_fail (c_handler != NULL, 0);
1304
1305   G_LOCK (g_signal_mutex);
1306   itype = G_TYPE_FROM_INSTANCE (instance);
1307   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1308   if (signal_id)
1309     {
1310       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1311
1312       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1313         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1314       else if (!g_type_is_a (itype, node->itype))
1315         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1316       else
1317         {
1318           Handler *handler = handler_new (after);
1319
1320           handler_id = handler->id;
1321           handler->detail = detail;
1322           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1323           g_closure_sink (handler->closure);
1324           handler_insert (signal_id, instance, handler);
1325           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1326             g_closure_set_marshal (handler->closure, node->c_marshaller);
1327         }
1328     }
1329   else
1330     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1331   G_UNLOCK (g_signal_mutex);
1332
1333   return handler_id;
1334 }
1335
1336 void
1337 g_signal_handler_block (gpointer instance,
1338                         guint    handler_id)
1339 {
1340   Handler *handler;
1341   
1342   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1343   g_return_if_fail (handler_id > 0);
1344   
1345   G_LOCK (g_signal_mutex);
1346   handler = handler_lookup (instance, handler_id, NULL);
1347   if (handler)
1348     {
1349 #ifndef G_DISABLE_CHECKS
1350       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1351         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1352 #endif
1353       
1354       handler->block_count += 1;
1355     }
1356   else
1357     g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1358   G_UNLOCK (g_signal_mutex);
1359 }
1360
1361 void
1362 g_signal_handler_unblock (gpointer instance,
1363                           guint    handler_id)
1364 {
1365   Handler *handler;
1366   
1367   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1368   g_return_if_fail (handler_id > 0);
1369   
1370   G_LOCK (g_signal_mutex);
1371   handler = handler_lookup (instance, handler_id, NULL);
1372   if (handler)
1373     {
1374       if (handler->block_count)
1375         handler->block_count -= 1;
1376       else
1377         g_warning (G_STRLOC ": handler `%u' of instance `%p' is not blocked", handler_id, instance);
1378     }
1379   else
1380     g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1381   G_UNLOCK (g_signal_mutex);
1382 }
1383
1384 void
1385 g_signal_handler_disconnect (gpointer instance,
1386                              guint    handler_id)
1387 {
1388   Handler *handler;
1389   guint signal_id;
1390   
1391   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1392   g_return_if_fail (handler_id > 0);
1393   
1394   G_LOCK (g_signal_mutex);
1395   handler = handler_lookup (instance, handler_id, &signal_id);
1396   if (handler)
1397     {
1398       handler->id = 0;
1399       handler->block_count = 1;
1400       handler_unref_R (signal_id, instance, handler);
1401     }
1402   else
1403     g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1404   G_UNLOCK (g_signal_mutex);
1405 }
1406
1407 void
1408 g_signal_handlers_destroy (gpointer instance)
1409 {
1410   GBSearchArray *hlbsa;
1411   
1412   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1413   
1414   G_LOCK (g_signal_mutex);
1415   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1416   if (hlbsa)
1417     {
1418       guint i;
1419       
1420       /* reentrancy caution, delete instance trace first */
1421       g_hash_table_remove (g_handler_list_bsa_ht, instance);
1422       
1423       for (i = 0; i < hlbsa->n_nodes; i++)
1424         {
1425           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1426           Handler *handler = hlist->handlers;
1427           
1428           while (handler)
1429             {
1430               Handler *tmp = handler;
1431               
1432               handler = tmp->next;
1433               tmp->block_count = 1;
1434               /* cruel unlink, this works because _all_ handlers vanish */
1435               tmp->next = NULL;
1436               tmp->prev = tmp;
1437               if (tmp->id)
1438                 {
1439                   tmp->id = 0;
1440                   handler_unref_R (0, NULL, tmp);
1441                 }
1442             }
1443         }
1444       g_free (hlbsa->nodes);
1445       g_generic_node_free (&g_bsa_ts, hlbsa);
1446     }
1447   G_UNLOCK (g_signal_mutex);
1448 }
1449
1450 guint
1451 g_signal_handler_find (gpointer         instance,
1452                        GSignalMatchType mask,
1453                        guint            signal_id,
1454                        GQuark           detail,
1455                        GClosure        *closure,
1456                        gpointer         func,
1457                        gpointer         data)
1458 {
1459   guint handler_id = 0;
1460   
1461   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1462   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1463   
1464   if (mask & G_SIGNAL_MATCH_MASK)
1465     {
1466       HandlerMatch *mlist;
1467       
1468       G_LOCK (g_signal_mutex);
1469       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1470       if (mlist)
1471         {
1472           handler_id = mlist->handler->id;
1473           handler_match_free1_R (mlist, instance);
1474         }
1475       G_UNLOCK (g_signal_mutex);
1476     }
1477   
1478   return handler_id;
1479 }
1480
1481 static guint
1482 signal_handlers_foreach_matched_R (gpointer         instance,
1483                                    GSignalMatchType mask,
1484                                    guint            signal_id,
1485                                    GQuark           detail,
1486                                    GClosure        *closure,
1487                                    gpointer         func,
1488                                    gpointer         data,
1489                                    void           (*callback) (gpointer instance,
1490                                                                guint    handler_id))
1491 {
1492   HandlerMatch *mlist;
1493   guint n_handlers = 0;
1494   
1495   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1496   while (mlist)
1497     {
1498       n_handlers++;
1499       G_UNLOCK (g_signal_mutex);
1500       callback (instance, mlist->handler->id);
1501       G_LOCK (g_signal_mutex);
1502       mlist = handler_match_free1_R (mlist, instance);
1503     }
1504   
1505   return n_handlers;
1506 }
1507
1508 guint
1509 g_signal_handlers_block_matched (gpointer         instance,
1510                                  GSignalMatchType mask,
1511                                  guint            signal_id,
1512                                  GQuark           detail,
1513                                  GClosure        *closure,
1514                                  gpointer         func,
1515                                  gpointer         data)
1516 {
1517   guint n_handlers = 0;
1518   
1519   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1520   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1521   
1522   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1523     {
1524       G_LOCK (g_signal_mutex);
1525       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1526                                                       closure, func, data,
1527                                                       g_signal_handler_block);
1528       G_UNLOCK (g_signal_mutex);
1529     }
1530   
1531   return n_handlers;
1532 }
1533
1534 guint
1535 g_signal_handlers_unblock_matched (gpointer         instance,
1536                                    GSignalMatchType mask,
1537                                    guint            signal_id,
1538                                    GQuark           detail,
1539                                    GClosure        *closure,
1540                                    gpointer         func,
1541                                    gpointer         data)
1542 {
1543   guint n_handlers = 0;
1544   
1545   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1546   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1547   
1548   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1549     {
1550       G_LOCK (g_signal_mutex);
1551       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1552                                                       closure, func, data,
1553                                                       g_signal_handler_unblock);
1554       G_UNLOCK (g_signal_mutex);
1555     }
1556   
1557   return n_handlers;
1558 }
1559
1560 guint
1561 g_signal_handlers_disconnect_matched (gpointer         instance,
1562                                       GSignalMatchType mask,
1563                                       guint            signal_id,
1564                                       GQuark           detail,
1565                                       GClosure        *closure,
1566                                       gpointer         func,
1567                                       gpointer         data)
1568 {
1569   guint n_handlers = 0;
1570   
1571   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1572   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1573   
1574   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1575     {
1576       G_LOCK (g_signal_mutex);
1577       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1578                                                       closure, func, data,
1579                                                       g_signal_handler_disconnect);
1580       G_UNLOCK (g_signal_mutex);
1581     }
1582   
1583   return n_handlers;
1584 }
1585
1586 gboolean
1587 g_signal_has_handler_pending (gpointer instance,
1588                               guint    signal_id,
1589                               GQuark   detail,
1590                               gboolean may_be_blocked)
1591 {
1592   HandlerMatch *mlist;
1593   gboolean has_pending;
1594   
1595   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1596   g_return_val_if_fail (signal_id > 0, FALSE);
1597   
1598   G_LOCK (g_signal_mutex);
1599   if (detail)
1600     {
1601       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1602       
1603       if (!(node->flags & G_SIGNAL_DETAILED))
1604         {
1605           g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1606           G_UNLOCK (g_signal_mutex);
1607           return FALSE;
1608         }
1609     }
1610   mlist = handlers_find (instance,
1611                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1612                          signal_id, detail, NULL, NULL, NULL, TRUE);
1613   if (mlist)
1614     {
1615       has_pending = TRUE;
1616       handler_match_free1_R (mlist, instance);
1617     }
1618   else
1619     has_pending = FALSE;
1620   G_UNLOCK (g_signal_mutex);
1621   
1622   return has_pending;
1623 }
1624
1625 void
1626 g_signal_emitv (const GValue *instance_and_params,
1627                 guint         signal_id,
1628                 GQuark        detail,
1629                 GValue       *return_value)
1630 {
1631   const GValue *param_values;
1632   gpointer instance;
1633   SignalNode *node;
1634   guint i;
1635   
1636   g_return_if_fail (instance_and_params != NULL);
1637   instance = g_value_peek_pointer (instance_and_params);
1638   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1639   g_return_if_fail (signal_id > 0);
1640
1641   param_values = instance_and_params + 1;
1642   
1643   G_LOCK (g_signal_mutex);
1644   node = LOOKUP_SIGNAL_NODE (signal_id);
1645   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1646     {
1647       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1648       G_UNLOCK (g_signal_mutex);
1649       return;
1650     }
1651 #ifdef G_ENABLE_DEBUG
1652   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1653     {
1654       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1655       G_UNLOCK (g_signal_mutex);
1656       return;
1657     }
1658   for (i = 0; i < node->n_params; i++)
1659     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1660       {
1661         g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1662                     G_STRLOC,
1663                     g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1664                     i,
1665                     node->name,
1666                     G_VALUE_TYPE_NAME (param_values + i));
1667         G_UNLOCK (g_signal_mutex);
1668         return;
1669       }
1670   if (node->return_type != G_TYPE_NONE)
1671     {
1672       if (!return_value)
1673         {
1674           g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1675                       G_STRLOC,
1676                       g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1677                       node->name);
1678           G_UNLOCK (g_signal_mutex);
1679           return;
1680         }
1681       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1682         {
1683           g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1684                       G_STRLOC,
1685                       g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1686                       node->name,
1687                       G_VALUE_TYPE_NAME (return_value));
1688           G_UNLOCK (g_signal_mutex);
1689           return;
1690         }
1691     }
1692   else
1693     return_value = NULL;
1694 #endif  /* G_ENABLE_DEBUG */
1695
1696   signal_emit_R (node, detail, instance, return_value, instance_and_params);
1697   G_UNLOCK (g_signal_mutex);
1698 }
1699
1700 void
1701 g_signal_emit_valist (gpointer instance,
1702                       guint    signal_id,
1703                       GQuark   detail,
1704                       va_list  var_args)
1705 {
1706   GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1707   GValue *param_values;
1708   SignalNode *node;
1709   guint i;
1710
1711   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1712   g_return_if_fail (signal_id > 0);
1713
1714   G_LOCK (g_signal_mutex);
1715   node = LOOKUP_SIGNAL_NODE (signal_id);
1716   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1717     {
1718       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1719       G_UNLOCK (g_signal_mutex);
1720       return;
1721     }
1722 #ifndef G_DISABLE_CHECKS
1723   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1724     {
1725       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1726       G_UNLOCK (g_signal_mutex);
1727       return;
1728     }
1729 #endif  /* !G_DISABLE_CHECKS */
1730
1731   if (node->n_params < MAX_STACK_VALUES)
1732     instance_and_params = stack_values;
1733   else
1734     {
1735       free_me = g_new (GValue, node->n_params + 1);
1736       instance_and_params = free_me;
1737     }
1738   param_values = instance_and_params + 1;
1739   for (i = 0; i < node->n_params; i++)
1740     {
1741       gchar *error;
1742
1743       param_values[i].g_type = 0;
1744       g_value_init (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1745       G_VALUE_COLLECT (param_values + i,
1746                        var_args,
1747                        node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1748                        &error);
1749       if (error)
1750         {
1751           g_warning ("%s: %s", G_STRLOC, error);
1752           g_free (error);
1753
1754           /* we purposely leak the value here, it might not be
1755            * in a sane state if an error condition occoured
1756            */
1757           while (i--)
1758             g_value_unset (param_values + i);
1759
1760           G_UNLOCK (g_signal_mutex);
1761           g_free (free_me);
1762           return;
1763         }
1764     }
1765   instance_and_params->g_type = 0;
1766   g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1767   g_value_set_instance (instance_and_params, instance);
1768   if (node->return_type == G_TYPE_NONE)
1769     signal_emit_R (node, detail, instance, NULL, instance_and_params);
1770   else
1771     {
1772       GValue return_value = { 0, };
1773       gchar *error = NULL;
1774
1775       g_value_init (&return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1776       if (signal_emit_R (node, detail, instance, &return_value, instance_and_params))
1777         G_VALUE_LCOPY (&return_value,
1778                        var_args,
1779                        node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1780                        &error);
1781       if (!error)
1782         g_value_unset (&return_value);
1783       else
1784         {
1785           g_warning ("%s: %s", G_STRLOC, error);
1786           g_free (error);
1787
1788           /* we purposely leak the value here, it might not be
1789            * in a sane state if an error condition occoured
1790            */
1791         }
1792     }
1793   for (i = 0; i < node->n_params; i++)
1794     g_value_unset (param_values + i);
1795   g_value_unset (instance_and_params);
1796   if (free_me)
1797     g_free (free_me);
1798   G_UNLOCK (g_signal_mutex);
1799 }
1800
1801 void
1802 g_signal_emit (gpointer instance,
1803                guint    signal_id,
1804                GQuark   detail,
1805                ...)
1806 {
1807   va_list var_args;
1808
1809   va_start (var_args, detail);
1810   g_signal_emit_valist (instance, signal_id, detail, var_args);
1811   va_end (var_args);
1812 }
1813
1814 void
1815 g_signal_emit_by_name (gpointer     instance,
1816                        const gchar *detailed_signal,
1817                        ...)
1818 {
1819   GQuark detail = 0;
1820   guint signal_id;
1821
1822   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1823   g_return_if_fail (detailed_signal != NULL);
1824
1825   G_LOCK (g_signal_mutex);
1826   signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1827   G_UNLOCK (g_signal_mutex);
1828
1829   if (signal_id)
1830     {
1831       va_list var_args;
1832
1833       va_start (var_args, detailed_signal);
1834       g_signal_emit_valist (instance, signal_id, detail, var_args);
1835       va_end (var_args);
1836     }
1837   else
1838     g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1839 }
1840
1841 static inline gboolean
1842 accumulate (GSignalInvocationHint *ihint,
1843             GValue                *return_accu,
1844             GValue                *handler_return,
1845             SignalAccumulator     *accumulator)
1846 {
1847   gboolean continue_emission;
1848
1849   if (!accumulator)
1850     return TRUE;
1851
1852   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1853   g_value_reset (handler_return);
1854
1855   return continue_emission;
1856 }
1857
1858 static gboolean
1859 signal_emit_R (SignalNode   *node,
1860                GQuark        detail,
1861                gpointer      instance,
1862                GValue       *emission_return,
1863                const GValue *instance_and_params)
1864 {
1865   EmissionState emission_state = 0;
1866   SignalAccumulator *accumulator;
1867   GSignalInvocationHint ihint;
1868   GClosure *class_closure;
1869   HandlerList *hlist;
1870   Handler *handler_list = NULL;
1871   GValue *return_accu, accu = { 0, };
1872   guint signal_id = node->signal_id;
1873   gboolean return_value_altered = FALSE;
1874   
1875 #ifdef  G_ENABLE_DEBUG
1876   IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
1877     {
1878       g_message ("%s::%s(%u) emitted (instance=%p signal-node=%p)\n",
1879                  g_type_name (G_TYPE_FROM_INSTANCE (instance)),
1880                  node->name, detail,
1881                  instance, node);
1882       if (g_trap_instance_signals == instance)
1883         G_BREAKPOINT ();
1884     }
1885 #endif  /* G_ENABLE_DEBUG */
1886   
1887   if (node->flags & G_SIGNAL_NO_RECURSE)
1888     {
1889       Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
1890       
1891       if (emission)
1892         {
1893           *emission->state_p = EMISSION_RESTART;
1894           return return_value_altered;
1895         }
1896     }
1897   ihint.signal_id = node->signal_id;
1898   ihint.detail = detail;
1899   accumulator = node->accumulator;
1900   if (accumulator)
1901     {
1902       g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1903       return_accu = &accu;
1904     }
1905   else
1906     return_accu = emission_return;
1907   emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
1908                  signal_id, detail, instance, &emission_state);
1909   class_closure = node->class_closure;
1910   
1911  EMIT_RESTART:
1912   
1913   if (handler_list)
1914     handler_unref_R (signal_id, instance, handler_list);
1915   hlist = handler_list_lookup (signal_id, instance);
1916   handler_list = hlist ? hlist->handlers : NULL;
1917   if (handler_list)
1918     handler_ref (handler_list);
1919   
1920   ihint.run_type = G_SIGNAL_RUN_FIRST;
1921   
1922   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
1923     {
1924       emission_state = EMISSION_RUN;
1925       
1926       G_UNLOCK (g_signal_mutex);
1927       g_closure_invoke (class_closure,
1928                         return_accu,
1929                         node->n_params + 1,
1930                         instance_and_params,
1931                         &ihint);
1932       if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
1933           emission_state == EMISSION_RUN)
1934         emission_state = EMISSION_STOP;
1935       G_LOCK (g_signal_mutex);
1936       return_value_altered = TRUE;
1937       
1938       if (emission_state == EMISSION_STOP)
1939         goto EMIT_CLEANUP;
1940       else if (emission_state == EMISSION_RESTART)
1941         goto EMIT_RESTART;
1942     }
1943   
1944   if (node->emission_hooks)
1945     {
1946       gboolean need_destroy, was_in_call, may_recurse = TRUE;
1947       GHook *hook;
1948       
1949       emission_state = EMISSION_HOOK;
1950       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
1951       while (hook)
1952         {
1953           GQuark hook_detail = GPOINTER_TO_UINT (hook->func);
1954           
1955           if (!hook_detail || hook_detail == detail)
1956             {
1957               GSignalEmissionHook hook_func = hook->func;
1958               
1959               was_in_call = G_HOOK_IN_CALL (hook);
1960               hook->flags |= G_HOOK_FLAG_IN_CALL;
1961               G_UNLOCK (g_signal_mutex);
1962               need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
1963               G_LOCK (g_signal_mutex);
1964               if (!was_in_call)
1965                 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
1966               if (need_destroy)
1967                 g_hook_destroy_link (node->emission_hooks, hook);
1968             }
1969           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
1970         }
1971       
1972       if (emission_state == EMISSION_RESTART)
1973         goto EMIT_RESTART;
1974     }
1975   
1976   if (handler_list)
1977     {
1978       Handler *handler = handler_list;
1979       
1980       emission_state = EMISSION_RUN;
1981       handler_ref (handler);
1982       do
1983         {
1984           Handler *tmp;
1985           
1986           if (handler->after)
1987             {
1988               handler_unref_R (signal_id, instance, handler_list);
1989               handler_list = handler;
1990               break;
1991             }
1992           else if (!handler->block_count && (!handler->detail || handler->detail == detail))
1993             {
1994               G_UNLOCK (g_signal_mutex);
1995               g_closure_invoke (handler->closure,
1996                                 return_accu,
1997                                 node->n_params + 1,
1998                                 instance_and_params,
1999                                 &ihint);
2000               if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2001                   emission_state == EMISSION_RUN)
2002                 emission_state = EMISSION_STOP;
2003               G_LOCK (g_signal_mutex);
2004               return_value_altered = TRUE;
2005               
2006               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2007             }
2008           else
2009             tmp = handler->next;
2010           
2011           if (tmp)
2012             handler_ref (tmp);
2013           handler_unref_R (signal_id, instance, handler_list);
2014           handler_list = handler;
2015           handler = tmp;
2016         }
2017       while (handler);
2018       
2019       if (emission_state == EMISSION_STOP)
2020         goto EMIT_CLEANUP;
2021       else if (emission_state == EMISSION_RESTART)
2022         goto EMIT_RESTART;
2023     }
2024   
2025   ihint.run_type = G_SIGNAL_RUN_LAST;
2026   
2027   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2028     {
2029       emission_state = EMISSION_RUN;
2030       
2031       G_UNLOCK (g_signal_mutex);
2032       g_closure_invoke (class_closure,
2033                         return_accu,
2034                         node->n_params + 1,
2035                         instance_and_params,
2036                         &ihint);
2037       if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2038           emission_state == EMISSION_RUN)
2039         emission_state = EMISSION_STOP;
2040       G_LOCK (g_signal_mutex);
2041       return_value_altered = TRUE;
2042       
2043       if (emission_state == EMISSION_STOP)
2044         goto EMIT_CLEANUP;
2045       else if (emission_state == EMISSION_RESTART)
2046         goto EMIT_RESTART;
2047     }
2048   
2049   if (handler_list)
2050     {
2051       Handler *handler = handler_list;
2052       
2053       emission_state = EMISSION_RUN;
2054       handler_ref (handler);
2055       do
2056         {
2057           Handler *tmp;
2058           
2059           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail))
2060             {
2061               G_UNLOCK (g_signal_mutex);
2062               g_closure_invoke (handler->closure,
2063                                 return_accu,
2064                                 node->n_params + 1,
2065                                 instance_and_params,
2066                                 &ihint);
2067               if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2068                   emission_state == EMISSION_RUN)
2069                 emission_state = EMISSION_STOP;
2070               G_LOCK (g_signal_mutex);
2071               return_value_altered = TRUE;
2072               
2073               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2074             }
2075           else
2076             tmp = handler->next;
2077           
2078           if (tmp)
2079             handler_ref (tmp);
2080           handler_unref_R (signal_id, instance, handler);
2081           handler = tmp;
2082         }
2083       while (handler);
2084       
2085       if (emission_state == EMISSION_STOP)
2086         goto EMIT_CLEANUP;
2087       else if (emission_state == EMISSION_RESTART)
2088         goto EMIT_RESTART;
2089     }
2090   
2091  EMIT_CLEANUP:
2092   
2093   ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2094   
2095   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2096     {
2097       gboolean need_unset = FALSE;
2098       
2099       emission_state = EMISSION_STOP;
2100       
2101       G_UNLOCK (g_signal_mutex);
2102       if (node->return_type != G_TYPE_NONE && !accumulator)
2103         {
2104           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2105           need_unset = TRUE;
2106         }
2107       g_closure_invoke (class_closure,
2108                         node->return_type != G_TYPE_NONE ? &accu : NULL,
2109                         node->n_params + 1,
2110                         instance_and_params,
2111                         &ihint);
2112       if (need_unset)
2113         g_value_unset (&accu);
2114       G_LOCK (g_signal_mutex);
2115       
2116       if (emission_state == EMISSION_RESTART)
2117         goto EMIT_RESTART;
2118     }
2119   
2120   if (handler_list)
2121     handler_unref_R (signal_id, instance, handler_list);
2122   
2123   emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2124   if (accumulator)
2125     g_value_unset (&accu);
2126   
2127   return return_value_altered;
2128 }
2129
2130
2131 /* --- compile standard marshallers --- */
2132 #include        "gobject.h"
2133 #include        "genums.h"
2134 #include        "gmarshal.c"