destroy all signals that the finalized obejct type introduced.
[platform/upstream/glib.git] / gobject / gsignal.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000 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 #include <string.h> 
23
24 #include        "gsignal.h"
25
26 #include        "gbsearcharray.h"
27
28
29 /* pre allocation configurations
30  */
31 #define BSA_PRE_ALLOC           (20)
32 #define HANDLER_PRE_ALLOC       (48)
33 #define EMISSION_PRE_ALLOC      (16)
34
35 #define TIGHT_MEMORY    (1)
36
37 #define REPORT_BUG      "please report occourance circumstances to gtk-devel-list@gnome.org"
38
39
40 /* --- generic allocation --- */
41 /* we can special case allocations generically by replacing
42  * these functions with more speed/memory aware variants
43  */
44 static inline gpointer
45 g_generic_node_alloc (GTrashStack **trash_stack_p,
46                       guint         sizeof_node,
47                       guint         nodes_pre_alloc)
48 {
49   gpointer node = g_trash_stack_pop (trash_stack_p);
50   
51   if (!node)
52     {
53       guint8 *block;
54       
55       nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
56       block = g_malloc (sizeof_node * nodes_pre_alloc);
57       while (--nodes_pre_alloc)
58         {
59           g_trash_stack_push (trash_stack_p, block);
60           block += sizeof_node;
61         }
62       node = block;
63     }
64   
65   return node;
66 }
67 static inline void
68 g_generic_node_free (GTrashStack **trash_stack_p,
69                      gpointer      node)
70 {
71   g_trash_stack_push (trash_stack_p, node);
72 }
73
74
75 /* --- typedefs --- */
76 typedef struct _SignalNode  SignalNode;
77 typedef struct _SignalKey   SignalKey;
78 typedef struct _Emission    Emission;
79 typedef struct _Handler     Handler;
80 typedef struct _HandlerList HandlerList;
81 typedef enum
82 {
83   EMISSION_STOP,
84   EMISSION_RUN,
85   EMISSION_HOOK,
86   EMISSION_RESTART
87 } EmissionState;
88
89
90 /* --- prototypes --- */
91 static inline guint             signal_id_lookup      (GQuark           quark,
92                                                        GType            itype);
93 static        void              signal_destroy_R      (SignalNode      *signal_node);
94 static inline HandlerList*      handler_list_ensure   (guint            signal_id,
95                                                        gpointer         instance);
96 static inline HandlerList*      handler_list_lookup   (guint            signal_id,
97                                                        gpointer         instance);
98 static inline Handler*          handler_new           (gboolean         after);
99 static        void              handler_insert        (guint            signal_id,
100                                                        gpointer         instance,
101                                                        Handler         *handler);
102 static        Handler*          handler_lookup        (gpointer         instance,
103                                                        guint            handler_id,
104                                                        guint           *signal_id_p);
105 static        Handler*          handler_find          (gpointer         instance,
106                                                        GSignalMatchType mask,
107                                                        guint            signal_id,
108                                                        GQuark           detail,
109                                                        GClosure        *closure,
110                                                        gpointer         func,
111                                                        gpointer         data);
112 static inline void              handler_ref           (Handler         *handler);
113 static inline void              handler_unref_R       (guint            signal_id,
114                                                        gpointer         instance,
115                                                        Handler         *handler);
116 static inline void              emission_push         (Emission       **emission_list_p,
117                                                        guint            signal_id,
118                                                        GQuark           detail,
119                                                        gpointer         instance,
120                                                        EmissionState   *state_p);
121 static inline void              emission_pop          (Emission       **emission_list_p,
122                                                        EmissionState   *state_p);
123 static inline Emission*         emission_find         (Emission        *emission_list,
124                                                        guint            signal_id,
125                                                        GQuark           detail,
126                                                        gpointer         instance);
127 static        void              signal_emit_R         (SignalNode      *node,
128                                                        GQuark           detail,
129                                                        gpointer         instance,
130                                                        GValue          *return_value,
131                                                        const GValue    *instance_and_params);
132
133
134 /* --- structures --- */
135 struct _SignalNode
136 {
137   /* permanent portion */
138   guint              signal_id;
139   GType              itype;
140   gchar             *name;
141   guint              destroyed : 1;
142   
143   /* reinitializable portion */
144   guint              flags : 8;
145   guint              n_params : 8;
146   GType             *param_types;
147   GType              return_type;
148   GClosure          *class_closure;
149   GSignalAccumulator accumulator;
150   GSignalCMarshaller c_marshaller;
151   GHookList         *emission_hooks;
152 };
153
154 struct _SignalKey
155 {
156   GType  itype;
157   GQuark quark;
158   guint  signal_id;
159 };
160
161 struct _Emission
162 {
163   Emission      *next;
164   guint          signal_id;
165   GQuark         detail;
166   gpointer       instance;
167   EmissionState *state_p;
168 };
169
170 struct _HandlerList
171 {
172   guint    signal_id;
173   Handler *handlers;
174 };
175
176 struct _Handler
177 {
178   guint         id;
179   Handler      *next;
180   Handler      *prev;
181   GQuark        detail;
182   guint         ref_count : 16;
183 #define HANDLER_MAX_REF_COUNT   (1 << 16)
184   guint         block_count : 12;
185 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
186   guint         after : 1;
187   GClosure     *closure;
188 };
189
190
191 /* --- variables --- */
192 static GBSearchArray  g_signal_key_bsa = { NULL, 0, 0, 0, NULL };
193 static GHashTable    *g_handler_list_bsa_ht = NULL;
194 static Emission      *g_recursive_emissions = NULL;
195 static Emission      *g_restart_emissions = NULL;
196 static GTrashStack   *g_bsa_ts = NULL;
197 static GTrashStack   *g_handler_ts = NULL;
198 static GTrashStack   *g_emission_ts = NULL;
199 G_LOCK_DEFINE_STATIC (g_signal_mutex);
200
201
202 /* --- signal nodes --- */
203 static guint          g_n_signal_nodes = 0;
204 static SignalNode   **g_signal_nodes = NULL;
205
206 static inline SignalNode*
207 LOOKUP_SIGNAL_NODE (register guint signal_id)
208 {
209   if (signal_id < g_n_signal_nodes)
210     return g_signal_nodes[signal_id];
211   else
212     return NULL;
213 }
214
215
216 /* --- functions --- */
217 static inline guint
218 signal_id_lookup (GQuark quark,
219                   GType  itype)
220 {
221   do
222     {
223       SignalKey key, *signal_key;
224       
225       key.itype = itype;
226       key.quark = quark;
227       
228       signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
229       
230       if (signal_key)
231         return signal_key->signal_id;
232       
233       itype = g_type_parent (itype);
234     }
235   while (itype);
236   
237   return 0;
238 }
239
240 static gint
241 handler_lists_cmp (gconstpointer node1,
242                    gconstpointer node2)
243 {
244   const HandlerList *hlist1 = node1, *hlist2 = node2;
245   
246   return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
247 }
248
249 static inline HandlerList*
250 handler_list_ensure (guint    signal_id,
251                      gpointer instance)
252 {
253   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
254   HandlerList key;
255   
256   if (!hlbsa)
257     {
258       hlbsa = g_generic_node_alloc (&g_bsa_ts,
259                                     sizeof (GBSearchArray),
260                                     BSA_PRE_ALLOC);
261       hlbsa->cmp_func = handler_lists_cmp;
262       hlbsa->sizeof_node = sizeof (HandlerList);
263       hlbsa->flags = G_BSEARCH_DEFER_SHRINK;
264       hlbsa->n_nodes = 0;
265       hlbsa->nodes = NULL;
266       g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
267     }
268   key.signal_id = signal_id;
269   key.handlers = NULL;
270   
271   return g_bsearch_array_insert (hlbsa, &key, FALSE);
272 }
273
274 static inline HandlerList*
275 handler_list_lookup (guint    signal_id,
276                      gpointer instance)
277 {
278   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
279   HandlerList key;
280   
281   key.signal_id = signal_id;
282   
283   return hlbsa ? g_bsearch_array_lookup (hlbsa, &key) : NULL;
284 }
285
286 static Handler*
287 handler_lookup (gpointer instance,
288                 guint    handler_id,
289                 guint   *signal_id_p)
290 {
291   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
292   
293   if (hlbsa)
294     {
295       guint i;
296       
297       for (i = 0; i < hlbsa->n_nodes; i++)
298         {
299           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
300           Handler *handler;
301           
302           for (handler = hlist->handlers; handler; handler = handler->next)
303             if (handler->id == handler_id)
304               {
305                 if (signal_id_p)
306                   *signal_id_p = hlist->signal_id;
307                 
308                 return handler;
309               }
310         }
311     }
312   
313   return NULL;
314 }
315
316 static Handler*
317 handler_find (gpointer         instance,
318               GSignalMatchType mask,
319               guint            signal_id,
320               GQuark           detail,
321               GClosure        *closure,
322               gpointer         func,
323               gpointer         data)
324 {
325   if (mask & G_SIGNAL_MATCH_ID)
326     {
327       HandlerList *hlist = handler_list_lookup (signal_id, instance);
328       Handler *handler;
329       SignalNode *node;
330       
331       if (mask & G_SIGNAL_MATCH_FUNC)
332         {
333           node = LOOKUP_SIGNAL_NODE (signal_id);
334           if (!node || !node->c_marshaller)
335             return NULL;
336         }
337           
338       mask = ~mask;
339       for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
340         if (((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
341             ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
342             ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
343             ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
344             ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
345                                               handler->closure->meta_marshal == 0 &&
346                                               ((GCClosure*) handler->closure)->callback == func)))
347           return handler;
348     }
349   else
350     {
351       GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
352       
353       mask = ~mask;
354       if (hlbsa)
355         {
356           guint i;
357           
358           for (i = 0; i < hlbsa->n_nodes; i++)
359             {
360               HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
361               SignalNode *node;
362               Handler *handler;
363               
364               if (!(mask & G_SIGNAL_MATCH_FUNC))
365                 {
366                   node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
367                   if (!node->c_marshaller)
368                     continue;
369                 }
370
371               for (handler = hlist->handlers; handler; handler = handler->next)
372                 if (((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
373                     ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
374                     ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
375                     ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
376                     ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
377                                                       handler->closure->meta_marshal == 0 &&
378                                                       ((GCClosure*) handler->closure)->callback == func)))
379                   return handler;
380             }
381         }
382     }
383   
384   return NULL;
385 }
386
387 static inline Handler*
388 handler_new (gboolean after)
389 {
390   static guint handler_id = 1;
391   Handler *handler = g_generic_node_alloc (&g_handler_ts,
392                                            sizeof (Handler),
393                                            HANDLER_PRE_ALLOC);
394 #ifndef G_DISABLE_CHECKS
395   if (handler_id == 0)
396     g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
397 #endif
398   
399   handler->id = handler_id++;
400   handler->prev = NULL;
401   handler->next = NULL;
402   handler->detail = 0;
403   handler->ref_count = 1;
404   handler->block_count = 0;
405   handler->after = after != FALSE;
406   handler->closure = NULL;
407   
408   return handler;
409 }
410
411 static inline void
412 handler_ref (Handler *handler)
413 {
414   g_return_if_fail (handler->ref_count > 0);
415   
416 #ifndef G_DISABLE_CHECKS
417   if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
418     g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
419 #endif
420   
421   handler->ref_count += 1;
422 }
423
424 static inline void
425 handler_unref_R (guint    signal_id,
426                  gpointer instance,
427                  Handler *handler)
428 {
429   g_return_if_fail (handler->ref_count > 0);
430   
431   handler->ref_count -= 1;
432   if (!handler->ref_count)
433     {
434       if (handler->next)
435         handler->next->prev = handler->prev;
436       if (handler->prev)        /* watch out for g_signal_handlers_destroy()! */
437         handler->prev->next = handler->next;
438       else
439         {
440           HandlerList *hlist = handler_list_lookup (signal_id, instance);
441           
442           hlist->handlers = handler->next;
443         }
444       G_UNLOCK (g_signal_mutex);
445       g_closure_unref (handler->closure);
446       G_LOCK (g_signal_mutex);
447       g_generic_node_free (&g_handler_ts, handler);
448     }
449 }
450
451 static void
452 handler_insert (guint    signal_id,
453                 gpointer instance,
454                 Handler  *handler)
455 {
456   HandlerList *hlist;
457   
458   g_assert (handler->prev == NULL && handler->next == NULL); // FIXME: paranoid
459   
460   hlist = handler_list_ensure (signal_id, instance);
461   if (!hlist->handlers)
462     hlist->handlers = handler;
463   else if (hlist->handlers->after && !handler->after)
464     {
465       handler->next = hlist->handlers;
466       hlist->handlers->prev = handler;
467       hlist->handlers = handler;
468     }
469   else
470     {
471       Handler *tmp = hlist->handlers;
472       
473       if (handler->after)
474         while (tmp->next)
475           tmp = tmp->next;
476       else
477         while (tmp->next && !tmp->next->after)
478           tmp = tmp->next;
479       if (tmp->next)
480         tmp->next->prev = handler;
481       handler->next = tmp->next;
482       handler->prev = tmp;
483       tmp->next = handler;
484     }
485 }
486
487 static inline void
488 emission_push (Emission     **emission_list_p,
489                guint          signal_id,
490                GQuark         detail,
491                gpointer       instance,
492                EmissionState *state_p)
493 {
494   Emission *emission = g_generic_node_alloc (&g_emission_ts,
495                                              sizeof (Emission),
496                                              EMISSION_PRE_ALLOC);
497   emission->next = *emission_list_p;
498   emission->signal_id = signal_id;
499   emission->detail = detail;
500   emission->instance = instance;
501   emission->state_p = state_p;
502   *emission_list_p = emission;
503 }
504
505 static inline void
506 emission_pop (Emission     **emission_list_p,
507               EmissionState *state_p)
508 {
509   Emission **loc = emission_list_p, *emission = *loc;
510
511   while (emission->state_p != state_p)
512     {
513       loc = &emission->next;
514       emission = *loc;
515     }
516   *loc = emission->next;
517   g_generic_node_free (&g_emission_ts, emission);
518 }
519
520 static inline Emission*
521 emission_find (Emission *emission_list,
522                guint     signal_id,
523                GQuark    detail,
524                gpointer  instance)
525 {
526   Emission *emission;
527   
528   for (emission = emission_list; emission; emission = emission->next)
529     if (emission->instance == instance &&
530         emission->signal_id == signal_id &&
531         emission->detail == detail)
532       return emission;
533   return NULL;
534 }
535
536 static gint
537 signal_key_cmp (gconstpointer node1,
538                 gconstpointer node2)
539 {
540   const SignalKey *key1 = node1, *key2 = node2;
541   
542   if (key1->itype == key2->itype)
543     return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
544   else
545     return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
546 }
547
548 void
549 g_signal_init (void) /* sync with gtype.c */
550 {
551   G_LOCK (g_signal_mutex);
552   if (!g_n_signal_nodes)
553     {
554       /* setup signal key array */
555       g_signal_key_bsa.cmp_func = signal_key_cmp;
556       g_signal_key_bsa.sizeof_node = sizeof (SignalKey);
557       g_signal_key_bsa.flags = 0; /* alloc-only */
558       
559       /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
560       g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
561       
562       /* invalid (0) signal_id */
563       g_n_signal_nodes = 1;
564       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
565       g_signal_nodes[0] = NULL;
566     }
567   G_UNLOCK (g_signal_mutex);
568 }
569
570 void
571 g_signals_destroy (GType itype)
572 {
573   guint i;
574   
575   G_LOCK (g_signal_mutex);
576   for (i = 0; i < g_n_signal_nodes; i++)
577     {
578       SignalNode *node = g_signal_nodes[i];
579       
580       if (node->itype == itype)
581         {
582           if (node->destroyed)
583             g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
584                        node->name,
585                        g_type_name (node->itype));
586           else
587             signal_destroy_R (node);
588         }
589     }
590   G_UNLOCK (g_signal_mutex);
591 }
592
593 void
594 g_signal_stop_emission (gpointer instance,
595                         guint    signal_id,
596                         GQuark   detail)
597 {
598   SignalNode *node;
599   
600   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
601   g_return_if_fail (signal_id > 0);
602   
603   G_LOCK (g_signal_mutex);
604   node = LOOKUP_SIGNAL_NODE (signal_id);
605   if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
606     {
607       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
608       G_UNLOCK (g_signal_mutex);
609       return;
610     }
611   if (node && g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
612     {
613       Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
614       Emission *emission = emission_find (emission_list, signal_id, detail, instance);
615       
616       if (emission)
617         {
618           if (*emission->state_p == EMISSION_HOOK)
619             g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
620                        node->name, instance);
621           else if (*emission->state_p == EMISSION_RUN)
622             *emission->state_p = EMISSION_STOP;
623         }
624       else
625         g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
626                    node->name, instance);
627     }
628   else
629     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
630   G_UNLOCK (g_signal_mutex);
631 }
632
633 guint
634 g_signal_lookup (const gchar *name,
635                  GType        itype)
636 {
637   guint signal_id;
638   
639   g_return_val_if_fail (name != NULL, 0);
640   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
641   
642   G_LOCK (g_signal_mutex);
643   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
644   G_UNLOCK (g_signal_mutex);
645   
646   return signal_id;
647 }
648
649 gchar*
650 g_signal_name (guint signal_id)
651 {
652   SignalNode *node;
653   gchar *name;
654
655   G_LOCK (g_signal_mutex);
656   node = LOOKUP_SIGNAL_NODE (signal_id);
657   name = node ? node->name : NULL;
658   G_UNLOCK (g_signal_mutex);
659   
660   return name;
661 }
662
663 void
664 g_signal_query (guint         signal_id,
665                 GSignalQuery *query)
666 {
667   SignalNode *node;
668
669   g_return_if_fail (query != NULL);
670
671   G_LOCK (g_signal_mutex);
672   node = LOOKUP_SIGNAL_NODE (signal_id);
673   if (!node || node->destroyed)
674     query->signal_id = 0;
675   else
676     {
677       query->signal_id = node->signal_id;
678       query->signal_name = node->name;
679       query->itype = node->itype;
680       query->signal_flags = node->flags;
681       query->return_type = node->return_type;
682       query->n_params = node->n_params;
683       query->param_types = node->param_types;
684     }
685   G_UNLOCK (g_signal_mutex);
686 }
687
688 guint*
689 g_signal_list_ids (GType  itype,
690                    guint *n_ids)
691 {
692   SignalKey *keys;
693   GArray *result;
694   guint n_nodes;
695   guint i;
696
697   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
698   g_return_val_if_fail (n_ids != NULL, NULL);
699
700   G_LOCK (g_signal_mutex);
701
702   keys = g_signal_key_bsa.nodes;
703   n_nodes  = g_signal_key_bsa.n_nodes;
704   result = g_array_new (FALSE, FALSE, sizeof (guint));
705   
706   for (i = 0; i < n_nodes; i++)
707     if (keys[i].itype == itype)
708       {
709         gchar *name = g_quark_to_string (keys[i].quark);
710
711         /* Signal names with "_" in them are aliases to the same
712          * name with "-" instead of "_".
713          */
714         if (!strchr (name, '_'))
715           g_array_append_val (result, keys[i].signal_id);
716       }
717
718   *n_ids = result->len;
719
720   G_UNLOCK (g_signal_mutex);
721   
722   return (guint *) g_array_free (result, FALSE);
723 }
724
725 guint
726 g_signal_newv (const gchar       *signal_name,
727                GType              itype,
728                GSignalFlags       signal_flags,
729                GClosure          *class_closure,
730                GSignalAccumulator accumulator,
731                GSignalCMarshaller c_marshaller,
732                GType              return_type,
733                guint              n_params,
734                GType             *param_types)
735 {
736   gchar *name;
737   guint signal_id, i;
738   SignalNode *node;
739   
740   g_return_val_if_fail (signal_name != NULL, 0);
741   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
742   if (n_params)
743     g_return_val_if_fail (param_types != NULL, 0);
744   if (return_type != G_TYPE_NONE)
745     g_return_val_if_fail (accumulator == NULL, 0);
746   
747   name = g_strdup (signal_name);
748   g_strdelimit (name, G_STR_DELIMITERS ":^", '_');  // FIXME do character checks like for types
749   
750   G_LOCK (g_signal_mutex);
751   
752   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
753   node = LOOKUP_SIGNAL_NODE (signal_id);
754   if (node && !node->destroyed)
755     {
756       g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
757                  name,
758                  g_type_name (node->itype),
759                  G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
760       g_free (name);
761       G_UNLOCK (g_signal_mutex);
762       return 0;
763     }
764   if (node && node->itype != itype)
765     {
766       g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
767                  name,
768                  g_type_name (itype),
769                  g_type_name (node->itype));
770       g_free (name);
771       G_UNLOCK (g_signal_mutex);
772       return 0;
773     }
774   for (i = 0; i < n_params; i++)
775     if (!G_TYPE_IS_VALUE (param_types[i]) ||
776         param_types[i] == G_TYPE_ENUM || param_types[i] == G_TYPE_FLAGS) /* FIXME: kludge */
777       {
778         g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
779                    i + 1, g_type_name (param_types[i]), g_type_name (itype), name);
780         g_free (name);
781         G_UNLOCK (g_signal_mutex);
782         return 0;
783       }
784   if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type))
785     {
786       g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
787                  g_type_name (param_types[i]), g_type_name (itype), name);
788       g_free (name);
789       G_UNLOCK (g_signal_mutex);
790       return 0;
791     }
792   
793   /* setup permanent portion of signal node */
794   if (!node)
795     {
796       SignalKey key;
797       
798       signal_id = g_n_signal_nodes++;
799       node = g_new (SignalNode, 1);
800       node->signal_id = signal_id;
801       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
802       g_signal_nodes[signal_id] = node;
803       node->itype = itype;
804       node->name = name;
805       key.itype = itype;
806       key.quark = g_quark_from_string (node->name);
807       key.signal_id = signal_id;
808       g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
809       g_strdelimit (node->name, "_", '-');
810       key.quark = g_quark_from_static_string (node->name);
811       g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
812     }
813   node->destroyed = FALSE;
814   
815   /* setup reinitializable portion */
816   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
817   node->n_params = n_params;
818   node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
819   node->return_type = return_type;
820   node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
821   node->accumulator = accumulator;
822   node->c_marshaller = c_marshaller;
823   node->emission_hooks = NULL;
824   if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
825     g_closure_set_marshal (class_closure, node->c_marshaller);
826   
827   G_UNLOCK (g_signal_mutex);
828   return signal_id;
829 }
830
831 static void
832 signal_destroy_R (SignalNode *signal_node)
833 {
834   SignalNode node = *signal_node;
835   
836   signal_node->destroyed = TRUE;
837   
838   /* reentrancy caution, zero out real contents first */
839   signal_node->n_params = 0;
840   signal_node->param_types = NULL;
841   signal_node->return_type = 0;
842   signal_node->class_closure = NULL;
843   signal_node->accumulator = NULL;
844   signal_node->c_marshaller = NULL;
845   signal_node->emission_hooks = NULL;
846   
847 #ifndef G_DISABLE_CHECKS
848   /* check current emissions */
849   {
850     Emission *emission;
851     
852     for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
853          emission; emission = emission->next)
854       if (emission->signal_id == node.signal_id)
855         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
856                     node.name, emission->instance);
857   }
858 #endif
859   
860   /* free contents that need to
861    */
862   G_UNLOCK (g_signal_mutex);
863   g_free (node.param_types);
864   g_closure_unref (node.class_closure);
865   if (node.emission_hooks)
866     {
867       g_hook_list_clear (node.emission_hooks);
868       g_free (node.emission_hooks);
869     }
870   G_LOCK (g_signal_mutex);
871 }
872
873 guint
874 g_signal_connect_closure (gpointer  instance,
875                           guint     signal_id,
876                           GQuark    detail,
877                           GClosure *closure,
878                           gboolean  after)
879 {
880   SignalNode *node;
881   guint handler_id = 0;
882   
883   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
884   g_return_val_if_fail (signal_id > 0, 0);
885   g_return_val_if_fail (closure != NULL, 0);
886   
887   G_LOCK (g_signal_mutex);
888   node = LOOKUP_SIGNAL_NODE (signal_id);
889   if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
890     {
891       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
892       G_UNLOCK (g_signal_mutex);
893       return 0;
894     }
895   if (node && g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
896     {
897       Handler *handler = handler_new (after);
898       
899       handler_id = handler->id;
900       handler->detail = detail;
901       handler->closure = g_closure_ref (closure);
902       handler_insert (signal_id, instance, handler);
903       if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
904         g_closure_set_marshal (closure, node->c_marshaller);
905     }
906   else
907     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
908   G_UNLOCK (g_signal_mutex);
909   
910   return handler_id;
911 }
912
913 void
914 g_signal_handler_block (gpointer instance,
915                         guint    handler_id)
916 {
917   Handler *handler;
918   
919   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
920   g_return_if_fail (handler_id > 0);
921   
922   G_LOCK (g_signal_mutex);
923   handler = handler_lookup (instance, handler_id, NULL);
924   if (handler)
925     {
926 #ifndef G_DISABLE_CHECKS
927       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
928         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
929 #endif
930       
931       handler->block_count += 1;
932     }
933   else
934     g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
935   G_UNLOCK (g_signal_mutex);
936 }
937
938 void
939 g_signal_handler_unblock (gpointer instance,
940                           guint    handler_id)
941 {
942   Handler *handler;
943   
944   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
945   g_return_if_fail (handler_id > 0);
946   
947   G_LOCK (g_signal_mutex);
948   handler = handler_lookup (instance, handler_id, NULL);
949   if (handler)
950     {
951       if (handler->block_count)
952         handler->block_count -= 1;
953       else
954         g_warning (G_STRLOC ": handler `%u' of instance `%p' is not blocked", handler_id, instance);
955     }
956   else
957     g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
958   G_UNLOCK (g_signal_mutex);
959 }
960
961 void
962 g_signal_handler_disconnect (gpointer instance,
963                              guint    handler_id)
964 {
965   Handler *handler;
966   guint signal_id;
967   
968   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
969   g_return_if_fail (handler_id > 0);
970   
971   G_LOCK (g_signal_mutex);
972   handler = handler_lookup (instance, handler_id, &signal_id);
973   if (handler)
974     {
975       handler->id = 0;
976       handler->block_count = 1;
977       handler_unref_R (signal_id, instance, handler);
978     }
979   else
980     g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
981   G_UNLOCK (g_signal_mutex);
982 }
983
984 void
985 g_signal_handlers_destroy (gpointer instance)
986 {
987   GBSearchArray *hlbsa;
988   
989   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
990   
991   G_LOCK (g_signal_mutex);
992   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
993   if (hlbsa)
994     {
995       guint i;
996       
997       /* reentrancy caution, delete instance trace first */
998       g_hash_table_remove (g_handler_list_bsa_ht, instance);
999
1000       for (i = 0; i < hlbsa->n_nodes; i++)
1001         {
1002           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1003           Handler *handler = hlist->handlers;
1004           
1005           while (handler)
1006             {
1007               Handler *tmp = handler;
1008               
1009               handler = tmp->next;
1010               tmp->block_count = 1;
1011               /* cruel unlink, this works because _all_ handlers vanish */
1012               tmp->next = NULL;
1013               tmp->prev = tmp;
1014               if (tmp->id)
1015                 {
1016                   tmp->id = 0;
1017                   handler_unref_R (0, NULL, tmp);
1018                 }
1019             }
1020         }
1021       g_free (hlbsa->nodes);
1022       g_generic_node_free (&g_bsa_ts, hlbsa);
1023     }
1024   G_UNLOCK (g_signal_mutex);
1025 }
1026
1027 guint
1028 g_signal_handler_find (gpointer         instance,
1029                        GSignalMatchType mask,
1030                        guint            signal_id,
1031                        GQuark           detail,
1032                        GClosure        *closure,
1033                        gpointer         func,
1034                        gpointer         data)
1035 {
1036   Handler *handler;
1037   guint handler_id = 0;
1038   
1039   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1040   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1041
1042   if (mask & G_SIGNAL_MATCH_MASK)
1043     {
1044       G_LOCK (g_signal_mutex);
1045       handler = handler_find (instance, mask, signal_id, detail, closure, func, data);
1046       handler_id = handler ? handler->id : 0;
1047       G_UNLOCK (g_signal_mutex);
1048     }
1049
1050   return handler_id;
1051 }
1052
1053 static guint
1054 signal_handlers_foreach_matched (gpointer         instance,
1055                                  GSignalMatchType mask,
1056                                  guint            signal_id,
1057                                  GQuark           detail,
1058                                  GClosure        *closure,
1059                                  gpointer         func,
1060                                  gpointer         data,
1061                                  void           (*callback) (gpointer instance,
1062                                                              guint    handler_id))
1063 {
1064   Handler *handler;
1065   guint n_handlers = 0;
1066
1067   handler = handler_find (instance, mask, signal_id, detail, closure, func, data);
1068   while (handler && handler->id)
1069     {
1070       n_handlers++;
1071       G_UNLOCK (g_signal_mutex);
1072       callback (instance, handler->id);
1073       G_LOCK (g_signal_mutex);
1074       /* need constant relookups due to callback */
1075       handler = handler_find (instance, mask, signal_id, detail, closure, func, data);
1076     }
1077
1078   return n_handlers;
1079 }
1080
1081 guint
1082 g_signal_handlers_block_matched (gpointer         instance,
1083                                  GSignalMatchType mask,
1084                                  guint            signal_id,
1085                                  GQuark           detail,
1086                                  GClosure        *closure,
1087                                  gpointer         func,
1088                                  gpointer         data)
1089 {
1090   guint n_handlers = 0;
1091
1092   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1093   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1094
1095   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1096     {
1097       G_LOCK (g_signal_mutex);
1098       n_handlers = signal_handlers_foreach_matched (instance, mask, signal_id, detail,
1099                                                     closure, func, data,
1100                                                     g_signal_handler_block);
1101       G_UNLOCK (g_signal_mutex);
1102     }
1103
1104   return n_handlers;
1105 }
1106
1107 guint
1108 g_signal_handlers_unblock_matched (gpointer         instance,
1109                                    GSignalMatchType mask,
1110                                    guint            signal_id,
1111                                    GQuark           detail,
1112                                    GClosure        *closure,
1113                                    gpointer         func,
1114                                    gpointer         data)
1115 {
1116   guint n_handlers = 0;
1117   
1118   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1119   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1120   
1121   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1122     {
1123       G_LOCK (g_signal_mutex);
1124       n_handlers = signal_handlers_foreach_matched (instance, mask, signal_id, detail,
1125                                                     closure, func, data,
1126                                                     g_signal_handler_unblock);
1127       G_UNLOCK (g_signal_mutex);
1128     }
1129
1130   return n_handlers;
1131 }
1132
1133 guint
1134 g_signal_handlers_disconnect_matched (gpointer         instance,
1135                                       GSignalMatchType mask,
1136                                       guint            signal_id,
1137                                       GQuark           detail,
1138                                       GClosure        *closure,
1139                                       gpointer         func,
1140                                       gpointer         data)
1141 {
1142   guint n_handlers = 0;
1143
1144   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1145   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1146
1147   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1148     {
1149       G_LOCK (g_signal_mutex);
1150       n_handlers = signal_handlers_foreach_matched (instance, mask, signal_id, detail,
1151                                                     closure, func, data,
1152                                                     g_signal_handler_disconnect);
1153       G_UNLOCK (g_signal_mutex);
1154     }
1155
1156   return n_handlers;
1157 }
1158
1159 gboolean
1160 g_signal_has_handler_pending (gpointer instance,
1161                               guint    signal_id,
1162                               GQuark   detail,
1163                               gboolean may_be_blocked)
1164 {
1165   Handler *handler = NULL;
1166   
1167   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1168   g_return_val_if_fail (signal_id > 0, FALSE);
1169   
1170   G_LOCK (g_signal_mutex);
1171   if (detail)
1172     {
1173       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1174       
1175       if (!(node->flags & G_SIGNAL_DETAILED))
1176         {
1177           g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1178           G_UNLOCK (g_signal_mutex);
1179           return FALSE;
1180         }
1181     }
1182   handler = handler_find (instance,
1183                           (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL |
1184                            (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1185                           signal_id, detail, NULL, NULL, NULL);
1186   G_UNLOCK (g_signal_mutex);
1187   
1188   return handler != NULL;
1189 }
1190
1191 void
1192 g_signal_emitv (const GValue *instance_and_params,
1193                 guint         signal_id,
1194                 GQuark        detail,
1195                 GValue       *return_value)
1196 {
1197   SignalNode *node;
1198   gpointer instance;
1199   const GValue *param_values;
1200   guint i;
1201   
1202   g_return_if_fail (instance_and_params != NULL);
1203   instance = g_value_get_as_pointer (instance_and_params);
1204   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1205   g_return_if_fail (signal_id > 0);
1206
1207   param_values = instance_and_params + 1;
1208   
1209   G_LOCK (g_signal_mutex);
1210   node = LOOKUP_SIGNAL_NODE (signal_id);
1211 #ifndef G_DISABLE_CHECKS
1212   if (!node || !g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
1213     {
1214       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1215       G_UNLOCK (g_signal_mutex);
1216       return;
1217     }
1218   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1219     {
1220       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1221       G_UNLOCK (g_signal_mutex);
1222       return;
1223     }
1224   for (i = 0; i < node->n_params; i++)
1225     if (!G_VALUE_HOLDS (param_values + i, node->param_types[i]))
1226       {
1227         g_critical (G_STRLOC ": value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1228                     g_type_name (node->param_types[i]),
1229                     i,
1230                     node->name,
1231                     G_VALUE_TYPE_NAME (param_values + i));
1232         G_UNLOCK (g_signal_mutex);
1233         return;
1234       }
1235   if (node->return_type != G_TYPE_NONE)
1236     {
1237       if (!return_value)
1238         {
1239           g_critical (G_STRLOC ": return value `%s' for signal \"%s\" is (NULL)",
1240                       g_type_name (node->return_type),
1241                       node->name);
1242           G_UNLOCK (g_signal_mutex);
1243           return;
1244         }
1245       else if (!node->accumulator && !G_VALUE_HOLDS (return_value, node->return_type))
1246         {
1247           g_critical (G_STRLOC ": return value `%s' for signal \"%s\" is of type `%s'",
1248                       g_type_name (node->return_type),
1249                       node->name,
1250                       G_VALUE_TYPE_NAME (return_value));
1251           G_UNLOCK (g_signal_mutex);
1252           return;
1253         }
1254     }
1255   else
1256     return_value = NULL;
1257 #endif  /* !G_DISABLE_CHECKS */
1258   
1259   signal_emit_R (node, detail, instance, return_value, instance_and_params);
1260   
1261   G_UNLOCK (g_signal_mutex);
1262 }
1263
1264 static void
1265 signal_emit_R (SignalNode   *node,
1266                GQuark        detail,
1267                gpointer      instance,
1268                GValue       *return_value,
1269                const GValue *instance_and_params)
1270 {
1271   EmissionState emission_state = 0;
1272   GSignalAccumulator accumulator;
1273   GSignalInvocationHint ihint;
1274   GClosure *class_closure;
1275   HandlerList *hlist;
1276   Handler *handler_list = NULL;
1277   GValue accu;
1278   gboolean accu_used = FALSE;
1279   guint signal_id = node->signal_id;
1280   
1281   if (node->flags & G_SIGNAL_NO_RECURSE)
1282     {
1283       Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
1284       
1285       if (emission)
1286         {
1287           *emission->state_p = EMISSION_RESTART;
1288           return;
1289         }
1290     }
1291   ihint.signal_id = node->signal_id;
1292   ihint.detail = detail;
1293   accumulator = node->accumulator;
1294   if (accumulator)
1295     {
1296       G_UNLOCK (g_signal_mutex);
1297       g_value_init (&accu, node->return_type);
1298       G_LOCK (g_signal_mutex);
1299     }
1300   emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
1301                  signal_id, detail, instance, &emission_state);
1302   class_closure = node->class_closure;
1303   
1304  EMIT_RESTART:
1305   
1306   if (handler_list)
1307     handler_unref_R (signal_id, instance, handler_list);
1308   hlist = handler_list_lookup (signal_id, instance);
1309   handler_list = hlist ? hlist->handlers : NULL;
1310   if (handler_list)
1311     handler_ref (handler_list);
1312   
1313   ihint.run_type = G_SIGNAL_RUN_FIRST;
1314   
1315   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
1316     {
1317       emission_state = EMISSION_RUN;
1318       
1319       G_UNLOCK (g_signal_mutex);
1320       if (accumulator)
1321         {
1322           if (accu_used)
1323             g_value_reset (&accu);
1324           g_closure_invoke (class_closure,
1325                             &accu,
1326                             node->n_params + 1,
1327                             instance_and_params,
1328                             &ihint);
1329           if (!accumulator (&ihint, return_value, &accu) &&
1330               emission_state == EMISSION_RUN)
1331             emission_state = EMISSION_STOP;
1332           accu_used = TRUE;
1333         }
1334       else
1335         g_closure_invoke (class_closure,
1336                           return_value,
1337                           node->n_params + 1,
1338                           instance_and_params,
1339                           &ihint);
1340       G_LOCK (g_signal_mutex);
1341       
1342       if (emission_state == EMISSION_STOP)
1343         goto EMIT_CLEANUP;
1344       else if (emission_state == EMISSION_RESTART)
1345         goto EMIT_RESTART;
1346     }
1347   
1348   if (node->emission_hooks)
1349     {
1350       emission_state = EMISSION_HOOK;
1351       
1352       G_UNLOCK (g_signal_mutex);
1353       g_print ("emission_hooks()\n");
1354       G_LOCK (g_signal_mutex);
1355       
1356       if (emission_state == EMISSION_RESTART)
1357         goto EMIT_RESTART;
1358     }
1359   
1360   if (handler_list)
1361     {
1362       Handler *handler = handler_list;
1363       
1364       emission_state = EMISSION_RUN;
1365       handler_ref (handler);
1366       do
1367         {
1368           Handler *tmp;
1369           
1370           if (handler->after)
1371             {
1372               handler_unref_R (signal_id, instance, handler_list);
1373               handler_list = handler;
1374               break;
1375             }
1376           else if (!handler->block_count && (!handler->detail || handler->detail == detail))
1377             {
1378               G_UNLOCK (g_signal_mutex);
1379               if (accumulator)
1380                 {
1381                   if (accu_used)
1382                     g_value_reset (&accu);
1383                   g_closure_invoke (handler->closure,
1384                                     &accu,
1385                                     node->n_params + 1,
1386                                     instance_and_params,
1387                                     &ihint);
1388                   if (!accumulator (&ihint, return_value, &accu) &&
1389                       emission_state == EMISSION_RUN)
1390                     emission_state = EMISSION_STOP;
1391                   accu_used = TRUE;
1392                 }
1393               else
1394                 g_closure_invoke (handler->closure,
1395                                   return_value,
1396                                   node->n_params + 1,
1397                                   instance_and_params,
1398                                   &ihint);
1399               G_LOCK (g_signal_mutex);
1400               
1401               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
1402             }
1403           else
1404             tmp = handler->next;
1405           
1406           if (tmp)
1407             handler_ref (tmp);
1408           handler_unref_R (signal_id, instance, handler_list);
1409           handler_list = handler;
1410           handler = tmp;
1411         }
1412       while (handler);
1413       
1414       if (emission_state == EMISSION_STOP)
1415         goto EMIT_CLEANUP;
1416       else if (emission_state == EMISSION_RESTART)
1417         goto EMIT_RESTART;
1418     }
1419   
1420   ihint.run_type = G_SIGNAL_RUN_LAST;
1421   
1422   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
1423     {
1424       emission_state = EMISSION_RUN;
1425       
1426       G_UNLOCK (g_signal_mutex);
1427       if (accumulator)
1428         {
1429           if (accu_used)
1430             g_value_reset (&accu);
1431           g_closure_invoke (class_closure,
1432                             &accu,
1433                             node->n_params + 1,
1434                             instance_and_params,
1435                             &ihint);
1436           if (!accumulator (&ihint, return_value, &accu) &&
1437               emission_state == EMISSION_RUN)
1438             emission_state = EMISSION_STOP;
1439           accu_used = TRUE;
1440         }
1441       else
1442         g_closure_invoke (class_closure,
1443                           return_value,
1444                           node->n_params + 1,
1445                           instance_and_params,
1446                           &ihint);
1447       G_LOCK (g_signal_mutex);
1448       
1449       if (emission_state == EMISSION_STOP)
1450         goto EMIT_CLEANUP;
1451       else if (emission_state == EMISSION_RESTART)
1452         goto EMIT_RESTART;
1453     }
1454   
1455   if (handler_list)
1456     {
1457       Handler *handler = handler_list;
1458       
1459       emission_state = EMISSION_RUN;
1460       handler_ref (handler);
1461       do
1462         {
1463           Handler *tmp;
1464           
1465           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail))
1466             {
1467               G_UNLOCK (g_signal_mutex);
1468               if (accumulator)
1469                 {
1470                   if (accu_used)
1471                     g_value_reset (&accu);
1472                   g_closure_invoke (handler->closure,
1473                                     &accu,
1474                                     node->n_params + 1,
1475                                     instance_and_params,
1476                                     &ihint);
1477                   if (!accumulator (&ihint, return_value, &accu) &&
1478                       emission_state == EMISSION_RUN)
1479                     emission_state = EMISSION_STOP;
1480                   accu_used = TRUE;
1481                 }
1482               else
1483                 g_closure_invoke (handler->closure,
1484                                   return_value,
1485                                   node->n_params + 1,
1486                                   instance_and_params,
1487                                   &ihint);
1488               G_LOCK (g_signal_mutex);
1489               
1490               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
1491             }
1492           else
1493             tmp = handler->next;
1494           
1495           if (tmp)
1496             handler_ref (tmp);
1497           handler_unref_R (signal_id, instance, handler);
1498           handler = tmp;
1499         }
1500       while (handler);
1501       
1502       if (emission_state == EMISSION_STOP)
1503         goto EMIT_CLEANUP;
1504       else if (emission_state == EMISSION_RESTART)
1505         goto EMIT_RESTART;
1506     }
1507   
1508  EMIT_CLEANUP:
1509   
1510   ihint.run_type = G_SIGNAL_RUN_CLEANUP;
1511   
1512   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
1513     {
1514       gboolean need_unset = FALSE;
1515       
1516       emission_state = EMISSION_STOP;
1517       
1518       G_UNLOCK (g_signal_mutex);
1519       if (node->return_type != G_TYPE_NONE)
1520         {
1521           if (!accumulator)
1522             {
1523               g_value_init (&accu, node->return_type);
1524               need_unset = TRUE;
1525             }
1526           else if (accu_used)
1527             g_value_reset (&accu);
1528         }
1529       g_closure_invoke (class_closure,
1530                         node->return_type != G_TYPE_NONE ? &accu : NULL,
1531                         node->n_params + 1,
1532                         instance_and_params,
1533                         &ihint);
1534       if (need_unset)
1535         g_value_unset (&accu);
1536       G_LOCK (g_signal_mutex);
1537       
1538       if (emission_state == EMISSION_RESTART)
1539         goto EMIT_RESTART;
1540     }
1541   
1542   if (handler_list)
1543     handler_unref_R (signal_id, instance, handler_list);
1544   
1545   emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
1546   if (accumulator)
1547     {
1548       G_UNLOCK (g_signal_mutex);
1549       g_value_unset (&accu);
1550       G_LOCK (g_signal_mutex);
1551     }
1552 }