Update.
[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   if (!signal_id)
947     {
948       /* give elaborate warnings */
949       if (!g_type_name (itype))
950         g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%u'",
951                    name, itype);
952       else if (!G_TYPE_IS_INSTANTIATABLE (itype))
953         g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
954                    name, g_type_name (itype));
955       else if (!g_type_class_peek (itype))
956         g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
957                    name, g_type_name (itype));
958     }
959   
960   return signal_id;
961 }
962
963 guint*
964 g_signal_list_ids (GType  itype,
965                    guint *n_ids)
966 {
967   SignalKey *keys;
968   GArray *result;
969   guint n_nodes;
970   guint i;
971   
972   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
973   g_return_val_if_fail (n_ids != NULL, NULL);
974   
975   SIGNAL_LOCK ();
976   keys = g_signal_key_bsa.nodes;
977   n_nodes  = g_signal_key_bsa.n_nodes;
978   result = g_array_new (FALSE, FALSE, sizeof (guint));
979   
980   for (i = 0; i < n_nodes; i++)
981     if (keys[i].itype == itype)
982       {
983         const gchar *name = g_quark_to_string (keys[i].quark);
984         
985         /* Signal names with "_" in them are aliases to the same
986          * name with "-" instead of "_".
987          */
988         if (!strchr (name, '_'))
989           g_array_append_val (result, keys[i].signal_id);
990       }
991   *n_ids = result->len;
992   SIGNAL_UNLOCK ();
993   if (!n_nodes)
994     {
995       /* give elaborate warnings */
996       if (!g_type_name (itype))
997         g_warning (G_STRLOC ": unable to list signals for invalid type id `%u'",
998                    itype);
999       else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1000         g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1001                    g_type_name (itype));
1002       else if (!g_type_class_peek (itype))
1003         g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1004                    g_type_name (itype));
1005     }
1006   
1007   return (guint*) g_array_free (result, FALSE);
1008 }
1009
1010 G_CONST_RETURN gchar*
1011 g_signal_name (guint signal_id)
1012 {
1013   SignalNode *node;
1014   gchar *name;
1015   
1016   SIGNAL_LOCK ();
1017   node = LOOKUP_SIGNAL_NODE (signal_id);
1018   name = node ? node->name : NULL;
1019   SIGNAL_UNLOCK ();
1020   
1021   return name;
1022 }
1023
1024 void
1025 g_signal_query (guint         signal_id,
1026                 GSignalQuery *query)
1027 {
1028   SignalNode *node;
1029   
1030   g_return_if_fail (query != NULL);
1031   
1032   SIGNAL_LOCK ();
1033   node = LOOKUP_SIGNAL_NODE (signal_id);
1034   if (!node || node->destroyed)
1035     query->signal_id = 0;
1036   else
1037     {
1038       query->signal_id = node->signal_id;
1039       query->signal_name = node->name;
1040       query->itype = node->itype;
1041       query->signal_flags = node->flags;
1042       query->return_type = node->return_type;
1043       query->n_params = node->n_params;
1044       query->param_types = node->param_types;
1045     }
1046   SIGNAL_UNLOCK ();
1047 }
1048
1049 guint
1050 g_signal_new_valist (const gchar       *signal_name,
1051                      GType              itype,
1052                      GSignalFlags       signal_flags,
1053                      GClosure          *class_closure,
1054                      GSignalAccumulator accumulator,
1055                      gpointer           accu_data,
1056                      GSignalCMarshaller c_marshaller,
1057                      GType              return_type,
1058                      guint              n_params,
1059                      va_list            args)
1060 {
1061   GType *param_types;
1062   guint i;
1063   guint signal_id;
1064
1065   if (n_params > 0)
1066     {
1067       param_types = g_new (GType, n_params);
1068
1069       for (i = 0; i < n_params; i++)
1070         param_types[i] = va_arg (args, GType);
1071     }
1072   else
1073     param_types = NULL;
1074
1075   signal_id = g_signal_newv (signal_name, itype, signal_flags,
1076                              class_closure, accumulator, accu_data, c_marshaller,
1077                              return_type, n_params, param_types);
1078   g_free (param_types);
1079
1080   return signal_id;
1081 }
1082
1083 guint
1084 g_signal_new (const gchar        *signal_name,
1085               GType               itype,
1086               GSignalFlags        signal_flags,
1087               guint               class_offset,
1088               GSignalAccumulator  accumulator,
1089               gpointer            accu_data,
1090               GSignalCMarshaller  c_marshaller,
1091               GType               return_type,
1092               guint               n_params,
1093               ...)
1094 {
1095   va_list args;
1096   guint signal_id;
1097
1098   g_return_val_if_fail (signal_name != NULL, 0);
1099   
1100   va_start (args, n_params);
1101
1102   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1103                                    class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1104                                    accumulator, accu_data, c_marshaller,
1105                                    return_type, n_params, args);
1106
1107   va_end (args);
1108  
1109   return signal_id;
1110 }
1111
1112 guint
1113 g_signal_newv (const gchar       *signal_name,
1114                GType              itype,
1115                GSignalFlags       signal_flags,
1116                GClosure          *class_closure,
1117                GSignalAccumulator accumulator,
1118                gpointer           accu_data,
1119                GSignalCMarshaller c_marshaller,
1120                GType              return_type,
1121                guint              n_params,
1122                GType             *param_types)
1123 {
1124   gchar *name;
1125   guint signal_id, i;
1126   SignalNode *node;
1127   
1128   g_return_val_if_fail (signal_name != NULL, 0);
1129   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1130   if (n_params)
1131     g_return_val_if_fail (param_types != NULL, 0);
1132   g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1133   if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1134     g_return_val_if_fail (accumulator == NULL, 0);
1135   if (!accumulator)
1136     g_return_val_if_fail (accu_data == NULL, 0);
1137
1138   name = g_strdup (signal_name);
1139   g_strdelimit (name, G_STR_DELIMITERS ":^", '_');  /* FIXME do character checks like for types */
1140   
1141   SIGNAL_LOCK ();
1142   
1143   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1144   node = LOOKUP_SIGNAL_NODE (signal_id);
1145   if (node && !node->destroyed)
1146     {
1147       g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1148                  name,
1149                  g_type_name (node->itype),
1150                  G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1151       g_free (name);
1152       SIGNAL_UNLOCK ();
1153       return 0;
1154     }
1155   if (node && node->itype != itype)
1156     {
1157       g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1158                  name,
1159                  g_type_name (itype),
1160                  g_type_name (node->itype));
1161       g_free (name);
1162       SIGNAL_UNLOCK ();
1163       return 0;
1164     }
1165   for (i = 0; i < n_params; i++)
1166     if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1167       {
1168         g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1169                    i + 1, g_type_name (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1170         g_free (name);
1171         SIGNAL_UNLOCK ();
1172         return 0;
1173       }
1174   if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1175     {
1176       g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1177                  g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1178       g_free (name);
1179       SIGNAL_UNLOCK ();
1180       return 0;
1181     }
1182   if (return_type != G_TYPE_NONE &&
1183       (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1184     {
1185       g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1186                  g_type_name (itype), name,
1187                  g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE));
1188       g_free (name);
1189       SIGNAL_UNLOCK ();
1190       return 0;
1191     }
1192   
1193   /* setup permanent portion of signal node */
1194   if (!node)
1195     {
1196       SignalKey key;
1197       
1198       signal_id = g_n_signal_nodes++;
1199       node = g_new (SignalNode, 1);
1200       node->signal_id = signal_id;
1201       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1202       g_signal_nodes[signal_id] = node;
1203       node->itype = itype;
1204       node->name = name;
1205       key.itype = itype;
1206       key.quark = g_quark_from_string (node->name);
1207       key.signal_id = signal_id;
1208       g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1209       g_strdelimit (node->name, "_", '-');
1210       key.quark = g_quark_from_static_string (node->name);
1211       g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1212     }
1213   node->destroyed = FALSE;
1214   
1215   /* setup reinitializable portion */
1216   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1217   node->n_params = n_params;
1218   node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1219   node->return_type = return_type;
1220   node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
1221   if (class_closure)
1222     g_closure_sink (class_closure);
1223   if (accumulator)
1224     {
1225       node->accumulator = g_new (SignalAccumulator, 1);
1226       node->accumulator->func = accumulator;
1227       node->accumulator->data = accu_data;
1228     }
1229   else
1230     node->accumulator = NULL;
1231   node->c_marshaller = c_marshaller;
1232   node->emission_hooks = NULL;
1233   if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
1234     g_closure_set_marshal (class_closure, node->c_marshaller);
1235   SIGNAL_UNLOCK ();
1236   return signal_id;
1237 }
1238
1239 static void
1240 signal_destroy_R (SignalNode *signal_node)
1241 {
1242   SignalNode node = *signal_node;
1243   
1244   signal_node->destroyed = TRUE;
1245   
1246   /* reentrancy caution, zero out real contents first */
1247   signal_node->n_params = 0;
1248   signal_node->param_types = NULL;
1249   signal_node->return_type = 0;
1250   signal_node->class_closure = NULL;
1251   signal_node->accumulator = NULL;
1252   signal_node->c_marshaller = NULL;
1253   signal_node->emission_hooks = NULL;
1254   
1255 #ifdef  G_ENABLE_DEBUG
1256   /* check current emissions */
1257   {
1258     Emission *emission;
1259     
1260     for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1261          emission; emission = emission->next)
1262       if (emission->signal_id == node.signal_id)
1263         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1264                     node.name, emission->instance);
1265   }
1266 #endif
1267   
1268   /* free contents that need to
1269    */
1270   SIGNAL_UNLOCK ();
1271   g_free (node.param_types);
1272   g_closure_unref (node.class_closure);
1273   g_free (node.accumulator);
1274   if (node.emission_hooks)
1275     {
1276       g_hook_list_clear (node.emission_hooks);
1277       g_free (node.emission_hooks);
1278     }
1279   SIGNAL_LOCK ();
1280 }
1281
1282 gulong
1283 g_signal_connect_closure_by_id (gpointer  instance,
1284                                 guint     signal_id,
1285                                 GQuark    detail,
1286                                 GClosure *closure,
1287                                 gboolean  after)
1288 {
1289   SignalNode *node;
1290   gulong handler_seq_no = 0;
1291   
1292   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1293   g_return_val_if_fail (signal_id > 0, 0);
1294   g_return_val_if_fail (closure != NULL, 0);
1295   
1296   SIGNAL_LOCK ();
1297   node = LOOKUP_SIGNAL_NODE (signal_id);
1298   if (node)
1299     {
1300       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1301         g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1302       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1303         g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1304       else
1305         {
1306           Handler *handler = handler_new (after);
1307           
1308           handler_seq_no = handler->sequential_number;
1309           handler->detail = detail;
1310           handler->closure = g_closure_ref (closure);
1311           g_closure_sink (closure);
1312           handler_insert (signal_id, instance, handler);
1313           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1314             g_closure_set_marshal (closure, node->c_marshaller);
1315         }
1316     }
1317   else
1318     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1319   SIGNAL_UNLOCK ();
1320   
1321   return handler_seq_no;
1322 }
1323
1324 gulong
1325 g_signal_connect_closure (gpointer     instance,
1326                           const gchar *detailed_signal,
1327                           GClosure    *closure,
1328                           gboolean     after)
1329 {
1330   guint signal_id;
1331   gulong handler_seq_no = 0;
1332   GQuark detail = 0;
1333   GType itype;
1334
1335   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1336   g_return_val_if_fail (detailed_signal != NULL, 0);
1337   g_return_val_if_fail (closure != NULL, 0);
1338
1339   SIGNAL_LOCK ();
1340   itype = G_TYPE_FROM_INSTANCE (instance);
1341   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1342   if (signal_id)
1343     {
1344       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1345
1346       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1347         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1348       else if (!g_type_is_a (itype, node->itype))
1349         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1350       else
1351         {
1352           Handler *handler = handler_new (after);
1353
1354           handler_seq_no = handler->sequential_number;
1355           handler->detail = detail;
1356           handler->closure = g_closure_ref (closure);
1357           g_closure_sink (closure);
1358           handler_insert (signal_id, instance, handler);
1359           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1360             g_closure_set_marshal (handler->closure, node->c_marshaller);
1361         }
1362     }
1363   else
1364     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1365   SIGNAL_UNLOCK ();
1366
1367   return handler_seq_no;
1368 }
1369
1370 gulong
1371 g_signal_connect_data (gpointer       instance,
1372                        const gchar   *detailed_signal,
1373                        GCallback      c_handler,
1374                        gpointer       data,
1375                        GClosureNotify destroy_data,
1376                        GConnectFlags  connect_flags)
1377 {
1378   guint signal_id;
1379   gulong handler_seq_no = 0;
1380   GQuark detail = 0;
1381   GType itype;
1382   gboolean swapped, after;
1383   
1384   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1385   g_return_val_if_fail (detailed_signal != NULL, 0);
1386   g_return_val_if_fail (c_handler != NULL, 0);
1387
1388   swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1389   after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1390
1391   SIGNAL_LOCK ();
1392   itype = G_TYPE_FROM_INSTANCE (instance);
1393   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1394   if (signal_id)
1395     {
1396       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1397
1398       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1399         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1400       else if (!g_type_is_a (itype, node->itype))
1401         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1402       else
1403         {
1404           Handler *handler = handler_new (after);
1405
1406           handler_seq_no = handler->sequential_number;
1407           handler->detail = detail;
1408           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1409           g_closure_sink (handler->closure);
1410           handler_insert (signal_id, instance, handler);
1411           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1412             g_closure_set_marshal (handler->closure, node->c_marshaller);
1413         }
1414     }
1415   else
1416     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1417   SIGNAL_UNLOCK ();
1418
1419   return handler_seq_no;
1420 }
1421
1422 void
1423 g_signal_handler_block (gpointer instance,
1424                         gulong   handler_id)
1425 {
1426   Handler *handler;
1427   
1428   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1429   g_return_if_fail (handler_id > 0);
1430   
1431   SIGNAL_LOCK ();
1432   handler = handler_lookup (instance, handler_id, NULL);
1433   if (handler)
1434     {
1435 #ifndef G_DISABLE_CHECKS
1436       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1437         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1438 #endif
1439       handler->block_count += 1;
1440     }
1441   else
1442     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1443   SIGNAL_UNLOCK ();
1444 }
1445
1446 void
1447 g_signal_handler_unblock (gpointer instance,
1448                           gulong   handler_id)
1449 {
1450   Handler *handler;
1451   
1452   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1453   g_return_if_fail (handler_id > 0);
1454   
1455   SIGNAL_LOCK ();
1456   handler = handler_lookup (instance, handler_id, NULL);
1457   if (handler)
1458     {
1459       if (handler->block_count)
1460         handler->block_count -= 1;
1461       else
1462         g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1463     }
1464   else
1465     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1466   SIGNAL_UNLOCK ();
1467 }
1468
1469 void
1470 g_signal_handler_disconnect (gpointer instance,
1471                              gulong   handler_id)
1472 {
1473   Handler *handler;
1474   guint signal_id;
1475   
1476   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1477   g_return_if_fail (handler_id > 0);
1478   
1479   SIGNAL_LOCK ();
1480   handler = handler_lookup (instance, handler_id, &signal_id);
1481   if (handler)
1482     {
1483       handler->sequential_number = 0;
1484       handler->block_count = 1;
1485       handler_unref_R (signal_id, instance, handler);
1486     }
1487   else
1488     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1489   SIGNAL_UNLOCK ();
1490 }
1491
1492 gboolean
1493 g_signal_handler_is_connected (gpointer instance,
1494                                gulong   handler_id)
1495 {
1496   Handler *handler;
1497   gboolean connected;
1498
1499   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1500   g_return_val_if_fail (handler_id > 0, FALSE);
1501
1502   SIGNAL_LOCK ();
1503   handler = handler_lookup (instance, handler_id, NULL);
1504   connected = handler != NULL;
1505   SIGNAL_UNLOCK ();
1506
1507   return connected;
1508 }
1509
1510 void
1511 g_signal_handlers_destroy (gpointer instance)
1512 {
1513   GBSearchArray *hlbsa;
1514   
1515   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1516   
1517   SIGNAL_LOCK ();
1518   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1519   if (hlbsa)
1520     {
1521       guint i;
1522       
1523       /* reentrancy caution, delete instance trace first */
1524       g_hash_table_remove (g_handler_list_bsa_ht, instance);
1525       
1526       for (i = 0; i < hlbsa->n_nodes; i++)
1527         {
1528           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1529           Handler *handler = hlist->handlers;
1530           
1531           while (handler)
1532             {
1533               Handler *tmp = handler;
1534               
1535               handler = tmp->next;
1536               tmp->block_count = 1;
1537               /* cruel unlink, this works because _all_ handlers vanish */
1538               tmp->next = NULL;
1539               tmp->prev = tmp;
1540               if (tmp->sequential_number)
1541                 {
1542                   tmp->sequential_number = 0;
1543                   handler_unref_R (0, NULL, tmp);
1544                 }
1545             }
1546         }
1547       g_bsearch_array_destroy (hlbsa);
1548     }
1549   SIGNAL_UNLOCK ();
1550 }
1551
1552 gulong
1553 g_signal_handler_find (gpointer         instance,
1554                        GSignalMatchType mask,
1555                        guint            signal_id,
1556                        GQuark           detail,
1557                        GClosure        *closure,
1558                        gpointer         func,
1559                        gpointer         data)
1560 {
1561   gulong handler_seq_no = 0;
1562   
1563   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1564   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1565   
1566   if (mask & G_SIGNAL_MATCH_MASK)
1567     {
1568       HandlerMatch *mlist;
1569       
1570       SIGNAL_LOCK ();
1571       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1572       if (mlist)
1573         {
1574           handler_seq_no = mlist->handler->sequential_number;
1575           handler_match_free1_R (mlist, instance);
1576         }
1577       SIGNAL_UNLOCK ();
1578     }
1579   
1580   return handler_seq_no;
1581 }
1582
1583 static guint
1584 signal_handlers_foreach_matched_R (gpointer         instance,
1585                                    GSignalMatchType mask,
1586                                    guint            signal_id,
1587                                    GQuark           detail,
1588                                    GClosure        *closure,
1589                                    gpointer         func,
1590                                    gpointer         data,
1591                                    void           (*callback) (gpointer instance,
1592                                                                gulong   handler_seq_no))
1593 {
1594   HandlerMatch *mlist;
1595   guint n_handlers = 0;
1596   
1597   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1598   while (mlist)
1599     {
1600       n_handlers++;
1601       if (mlist->handler->sequential_number)
1602         {
1603           SIGNAL_UNLOCK ();
1604           callback (instance, mlist->handler->sequential_number);
1605           SIGNAL_LOCK ();
1606         }
1607       mlist = handler_match_free1_R (mlist, instance);
1608     }
1609   
1610   return n_handlers;
1611 }
1612
1613 guint
1614 g_signal_handlers_block_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_block);
1633       SIGNAL_UNLOCK ();
1634     }
1635   
1636   return n_handlers;
1637 }
1638
1639 guint
1640 g_signal_handlers_unblock_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_unblock);
1659       SIGNAL_UNLOCK ();
1660     }
1661   
1662   return n_handlers;
1663 }
1664
1665 guint
1666 g_signal_handlers_disconnect_matched (gpointer         instance,
1667                                       GSignalMatchType mask,
1668                                       guint            signal_id,
1669                                       GQuark           detail,
1670                                       GClosure        *closure,
1671                                       gpointer         func,
1672                                       gpointer         data)
1673 {
1674   guint n_handlers = 0;
1675   
1676   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1677   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1678   
1679   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1680     {
1681       SIGNAL_LOCK ();
1682       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1683                                                       closure, func, data,
1684                                                       g_signal_handler_disconnect);
1685       SIGNAL_UNLOCK ();
1686     }
1687   
1688   return n_handlers;
1689 }
1690
1691 gboolean
1692 g_signal_has_handler_pending (gpointer instance,
1693                               guint    signal_id,
1694                               GQuark   detail,
1695                               gboolean may_be_blocked)
1696 {
1697   HandlerMatch *mlist;
1698   gboolean has_pending;
1699   
1700   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1701   g_return_val_if_fail (signal_id > 0, FALSE);
1702   
1703   SIGNAL_LOCK ();
1704   if (detail)
1705     {
1706       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1707       
1708       if (!(node->flags & G_SIGNAL_DETAILED))
1709         {
1710           g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1711           SIGNAL_UNLOCK ();
1712           return FALSE;
1713         }
1714     }
1715   mlist = handlers_find (instance,
1716                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1717                          signal_id, detail, NULL, NULL, NULL, TRUE);
1718   if (mlist)
1719     {
1720       has_pending = TRUE;
1721       handler_match_free1_R (mlist, instance);
1722     }
1723   else
1724     has_pending = FALSE;
1725   SIGNAL_UNLOCK ();
1726   
1727   return has_pending;
1728 }
1729
1730 void
1731 g_signal_emitv (const GValue *instance_and_params,
1732                 guint         signal_id,
1733                 GQuark        detail,
1734                 GValue       *return_value)
1735 {
1736   const GValue *param_values;
1737   gpointer instance;
1738   SignalNode *node;
1739 #ifdef G_ENABLE_DEBUG
1740   guint i;
1741 #endif
1742   
1743   g_return_if_fail (instance_and_params != NULL);
1744   instance = g_value_peek_pointer (instance_and_params);
1745   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1746   g_return_if_fail (signal_id > 0);
1747
1748   param_values = instance_and_params + 1;
1749   
1750   SIGNAL_LOCK ();
1751   node = LOOKUP_SIGNAL_NODE (signal_id);
1752   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1753     {
1754       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1755       SIGNAL_UNLOCK ();
1756       return;
1757     }
1758 #ifdef G_ENABLE_DEBUG
1759   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1760     {
1761       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1762       SIGNAL_UNLOCK ();
1763       return;
1764     }
1765   for (i = 0; i < node->n_params; i++)
1766     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1767       {
1768         g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1769                     G_STRLOC,
1770                     g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1771                     i,
1772                     node->name,
1773                     G_VALUE_TYPE_NAME (param_values + i));
1774         SIGNAL_UNLOCK ();
1775         return;
1776       }
1777   if (node->return_type != G_TYPE_NONE)
1778     {
1779       if (!return_value)
1780         {
1781           g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1782                       G_STRLOC,
1783                       g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1784                       node->name);
1785           SIGNAL_UNLOCK ();
1786           return;
1787         }
1788       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1789         {
1790           g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1791                       G_STRLOC,
1792                       g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1793                       node->name,
1794                       G_VALUE_TYPE_NAME (return_value));
1795           SIGNAL_UNLOCK ();
1796           return;
1797         }
1798     }
1799   else
1800     return_value = NULL;
1801 #endif  /* G_ENABLE_DEBUG */
1802
1803   SIGNAL_UNLOCK ();
1804   signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
1805 }
1806
1807 void
1808 g_signal_emit_valist (gpointer instance,
1809                       guint    signal_id,
1810                       GQuark   detail,
1811                       va_list  var_args)
1812 {
1813   GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1814   GType signal_return_type;
1815   GValue *param_values;
1816   SignalNode *node;
1817   guint i, n_params;
1818   
1819   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1820   g_return_if_fail (signal_id > 0);
1821
1822   SIGNAL_LOCK ();
1823   node = LOOKUP_SIGNAL_NODE (signal_id);
1824   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1825     {
1826       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1827       SIGNAL_UNLOCK ();
1828       return;
1829     }
1830 #ifndef G_DISABLE_CHECKS
1831   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1832     {
1833       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1834       SIGNAL_UNLOCK ();
1835       return;
1836     }
1837 #endif  /* !G_DISABLE_CHECKS */
1838
1839   n_params = node->n_params;
1840   signal_return_type = node->return_type;
1841   if (node->n_params < MAX_STACK_VALUES)
1842     instance_and_params = stack_values;
1843   else
1844     {
1845       free_me = g_new (GValue, node->n_params + 1);
1846       instance_and_params = free_me;
1847     }
1848   param_values = instance_and_params + 1;
1849   for (i = 0; i < node->n_params; i++)
1850     {
1851       gchar *error;
1852       GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1853       gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1854       
1855       param_values[i].g_type = 0;
1856       SIGNAL_UNLOCK ();
1857       g_value_init (param_values + i, ptype);
1858       G_VALUE_COLLECT (param_values + i,
1859                        var_args,
1860                        static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1861                        &error);
1862       if (error)
1863         {
1864           g_warning ("%s: %s", G_STRLOC, error);
1865           g_free (error);
1866
1867           /* we purposely leak the value here, it might not be
1868            * in a sane state if an error condition occoured
1869            */
1870           while (i--)
1871             g_value_unset (param_values + i);
1872
1873           g_free (free_me);
1874           return;
1875         }
1876       SIGNAL_LOCK ();
1877     }
1878   SIGNAL_UNLOCK ();
1879   instance_and_params->g_type = 0;
1880   g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1881   g_value_set_instance (instance_and_params, instance);
1882   if (signal_return_type == G_TYPE_NONE)
1883     signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
1884   else
1885     {
1886       GValue return_value = { 0, };
1887       gchar *error = NULL;
1888       GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1889       gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
1890       
1891       g_value_init (&return_value, rtype);
1892
1893       signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
1894
1895       G_VALUE_LCOPY (&return_value,
1896                      var_args,
1897                      static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1898                      &error);
1899       if (!error)
1900         g_value_unset (&return_value);
1901       else
1902         {
1903           g_warning ("%s: %s", G_STRLOC, error);
1904           g_free (error);
1905           
1906           /* we purposely leak the value here, it might not be
1907            * in a sane state if an error condition occoured
1908            */
1909         }
1910     }
1911   for (i = 0; i < n_params; i++)
1912     g_value_unset (param_values + i);
1913   g_value_unset (instance_and_params);
1914   if (free_me)
1915     g_free (free_me);
1916 }
1917
1918 void
1919 g_signal_emit (gpointer instance,
1920                guint    signal_id,
1921                GQuark   detail,
1922                ...)
1923 {
1924   va_list var_args;
1925
1926   va_start (var_args, detail);
1927   g_signal_emit_valist (instance, signal_id, detail, var_args);
1928   va_end (var_args);
1929 }
1930
1931 void
1932 g_signal_emit_by_name (gpointer     instance,
1933                        const gchar *detailed_signal,
1934                        ...)
1935 {
1936   GQuark detail = 0;
1937   guint signal_id;
1938
1939   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1940   g_return_if_fail (detailed_signal != NULL);
1941
1942   SIGNAL_LOCK ();
1943   signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1944   SIGNAL_UNLOCK ();
1945
1946   if (signal_id)
1947     {
1948       va_list var_args;
1949
1950       va_start (var_args, detailed_signal);
1951       g_signal_emit_valist (instance, signal_id, detail, var_args);
1952       va_end (var_args);
1953     }
1954   else
1955     g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1956 }
1957
1958 static inline gboolean
1959 accumulate (GSignalInvocationHint *ihint,
1960             GValue                *return_accu,
1961             GValue                *handler_return,
1962             SignalAccumulator     *accumulator)
1963 {
1964   gboolean continue_emission;
1965
1966   if (!accumulator)
1967     return TRUE;
1968
1969   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1970   g_value_reset (handler_return);
1971
1972   return continue_emission;
1973 }
1974
1975 static gboolean
1976 signal_emit_unlocked_R (SignalNode   *node,
1977                         GQuark        detail,
1978                         gpointer      instance,
1979                         GValue       *emission_return,
1980                         const GValue *instance_and_params)
1981 {
1982   EmissionState emission_state = 0;
1983   SignalAccumulator *accumulator;
1984   GSignalInvocationHint ihint;
1985   GClosure *class_closure;
1986   HandlerList *hlist;
1987   Handler *handler_list = NULL;
1988   GValue *return_accu, accu = { 0, };
1989   guint signal_id;
1990   gulong max_sequential_handler_number;
1991   gboolean return_value_altered = FALSE;
1992   
1993 #ifdef  G_ENABLE_DEBUG
1994   IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
1995     {
1996       g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
1997                  g_type_name (G_TYPE_FROM_INSTANCE (instance)),
1998                  node->name, detail,
1999                  instance, node);
2000       if (g_trap_instance_signals == instance)
2001         G_BREAKPOINT ();
2002     }
2003 #endif  /* G_ENABLE_DEBUG */
2004   
2005   SIGNAL_LOCK ();
2006   signal_id = node->signal_id;
2007   if (node->flags & G_SIGNAL_NO_RECURSE)
2008     {
2009       Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
2010       
2011       if (emission)
2012         {
2013           *emission->state_p = EMISSION_RESTART;
2014           SIGNAL_UNLOCK ();
2015           return return_value_altered;
2016         }
2017     }
2018   ihint.signal_id = node->signal_id;
2019   ihint.detail = detail;
2020   accumulator = node->accumulator;
2021   if (accumulator)
2022     {
2023       SIGNAL_UNLOCK ();
2024       g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2025       return_accu = &accu;
2026       SIGNAL_LOCK ();
2027     }
2028   else
2029     return_accu = emission_return;
2030   emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
2031                  signal_id, detail, instance, &emission_state);
2032   class_closure = node->class_closure;
2033   
2034  EMIT_RESTART:
2035   
2036   if (handler_list)
2037     handler_unref_R (signal_id, instance, handler_list);
2038   max_sequential_handler_number = g_handler_sequential_number;
2039   hlist = handler_list_lookup (signal_id, instance);
2040   handler_list = hlist ? hlist->handlers : NULL;
2041   if (handler_list)
2042     handler_ref (handler_list);
2043   
2044   ihint.run_type = G_SIGNAL_RUN_FIRST;
2045   
2046   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2047     {
2048       emission_state = EMISSION_RUN;
2049       
2050       SIGNAL_UNLOCK ();
2051       g_closure_invoke (class_closure,
2052                         return_accu,
2053                         node->n_params + 1,
2054                         instance_and_params,
2055                         &ihint);
2056       if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2057           emission_state == EMISSION_RUN)
2058         emission_state = EMISSION_STOP;
2059       SIGNAL_LOCK ();
2060       return_value_altered = TRUE;
2061       
2062       if (emission_state == EMISSION_STOP)
2063         goto EMIT_CLEANUP;
2064       else if (emission_state == EMISSION_RESTART)
2065         goto EMIT_RESTART;
2066     }
2067   
2068   if (node->emission_hooks)
2069     {
2070       gboolean need_destroy, was_in_call, may_recurse = TRUE;
2071       GHook *hook;
2072
2073       emission_state = EMISSION_HOOK;
2074       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2075       while (hook)
2076         {
2077           SignalHook *signal_hook = SIGNAL_HOOK (hook);
2078           
2079           if (!signal_hook->detail || signal_hook->detail == detail)
2080             {
2081               GSignalEmissionHook hook_func = hook->func;
2082               
2083               was_in_call = G_HOOK_IN_CALL (hook);
2084               hook->flags |= G_HOOK_FLAG_IN_CALL;
2085               SIGNAL_UNLOCK ();
2086               need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
2087               SIGNAL_LOCK ();
2088               if (!was_in_call)
2089                 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2090               if (need_destroy)
2091                 g_hook_destroy_link (node->emission_hooks, hook);
2092             }
2093           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2094         }
2095       
2096       if (emission_state == EMISSION_RESTART)
2097         goto EMIT_RESTART;
2098     }
2099   
2100   if (handler_list)
2101     {
2102       Handler *handler = handler_list;
2103       
2104       emission_state = EMISSION_RUN;
2105       handler_ref (handler);
2106       do
2107         {
2108           Handler *tmp;
2109           
2110           if (handler->after)
2111             {
2112               handler_unref_R (signal_id, instance, handler_list);
2113               handler_list = handler;
2114               break;
2115             }
2116           else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2117                    handler->sequential_number < max_sequential_handler_number)
2118             {
2119               SIGNAL_UNLOCK ();
2120               g_closure_invoke (handler->closure,
2121                                 return_accu,
2122                                 node->n_params + 1,
2123                                 instance_and_params,
2124                                 &ihint);
2125               if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2126                   emission_state == EMISSION_RUN)
2127                 emission_state = EMISSION_STOP;
2128               SIGNAL_LOCK ();
2129               return_value_altered = TRUE;
2130               
2131               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2132             }
2133           else
2134             tmp = handler->next;
2135           
2136           if (tmp)
2137             handler_ref (tmp);
2138           handler_unref_R (signal_id, instance, handler_list);
2139           handler_list = handler;
2140           handler = tmp;
2141         }
2142       while (handler);
2143       
2144       if (emission_state == EMISSION_STOP)
2145         goto EMIT_CLEANUP;
2146       else if (emission_state == EMISSION_RESTART)
2147         goto EMIT_RESTART;
2148     }
2149   
2150   ihint.run_type = G_SIGNAL_RUN_LAST;
2151   
2152   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2153     {
2154       emission_state = EMISSION_RUN;
2155       
2156       SIGNAL_UNLOCK ();
2157       g_closure_invoke (class_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       SIGNAL_LOCK ();
2166       return_value_altered = TRUE;
2167       
2168       if (emission_state == EMISSION_STOP)
2169         goto EMIT_CLEANUP;
2170       else if (emission_state == EMISSION_RESTART)
2171         goto EMIT_RESTART;
2172     }
2173   
2174   if (handler_list)
2175     {
2176       Handler *handler = handler_list;
2177       
2178       emission_state = EMISSION_RUN;
2179       handler_ref (handler);
2180       do
2181         {
2182           Handler *tmp;
2183           
2184           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2185               handler->sequential_number < max_sequential_handler_number)
2186             {
2187               SIGNAL_UNLOCK ();
2188               g_closure_invoke (handler->closure,
2189                                 return_accu,
2190                                 node->n_params + 1,
2191                                 instance_and_params,
2192                                 &ihint);
2193               if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2194                   emission_state == EMISSION_RUN)
2195                 emission_state = EMISSION_STOP;
2196               SIGNAL_LOCK ();
2197               return_value_altered = TRUE;
2198               
2199               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2200             }
2201           else
2202             tmp = handler->next;
2203           
2204           if (tmp)
2205             handler_ref (tmp);
2206           handler_unref_R (signal_id, instance, handler);
2207           handler = tmp;
2208         }
2209       while (handler);
2210       
2211       if (emission_state == EMISSION_STOP)
2212         goto EMIT_CLEANUP;
2213       else if (emission_state == EMISSION_RESTART)
2214         goto EMIT_RESTART;
2215     }
2216   
2217  EMIT_CLEANUP:
2218   
2219   ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2220   
2221   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2222     {
2223       gboolean need_unset = FALSE;
2224       
2225       emission_state = EMISSION_STOP;
2226       
2227       SIGNAL_UNLOCK ();
2228       if (node->return_type != G_TYPE_NONE && !accumulator)
2229         {
2230           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2231           need_unset = TRUE;
2232         }
2233       g_closure_invoke (class_closure,
2234                         node->return_type != G_TYPE_NONE ? &accu : NULL,
2235                         node->n_params + 1,
2236                         instance_and_params,
2237                         &ihint);
2238       if (need_unset)
2239         g_value_unset (&accu);
2240       SIGNAL_LOCK ();
2241       
2242       if (emission_state == EMISSION_RESTART)
2243         goto EMIT_RESTART;
2244     }
2245   
2246   if (handler_list)
2247     handler_unref_R (signal_id, instance, handler_list);
2248   
2249   emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2250   SIGNAL_UNLOCK ();
2251   if (accumulator)
2252     g_value_unset (&accu);
2253   
2254   return return_value_altered;
2255 }
2256
2257
2258 /* --- compile standard marshallers --- */
2259 #include        "gobject.h"
2260 #include        "genums.h"
2261 #include        "gmarshal.c"