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