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