typedef GType to gulong instead of gsize, if possible.
[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 `%lu'",
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 `%lu'",
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 void
1283 g_signal_override_class_closure (guint     signal_id,
1284                                  GType     instance_type,
1285                                  GClosure *class_closure)
1286 {
1287 }
1288
1289 void
1290 g_signal_chain_from_overridden (const GValue *instance_and_params,
1291                                 guint         signal_id,
1292                                 GValue       *return_value)
1293 {
1294 }
1295
1296 gulong
1297 g_signal_connect_closure_by_id (gpointer  instance,
1298                                 guint     signal_id,
1299                                 GQuark    detail,
1300                                 GClosure *closure,
1301                                 gboolean  after)
1302 {
1303   SignalNode *node;
1304   gulong handler_seq_no = 0;
1305   
1306   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1307   g_return_val_if_fail (signal_id > 0, 0);
1308   g_return_val_if_fail (closure != NULL, 0);
1309   
1310   SIGNAL_LOCK ();
1311   node = LOOKUP_SIGNAL_NODE (signal_id);
1312   if (node)
1313     {
1314       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1315         g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1316       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1317         g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1318       else
1319         {
1320           Handler *handler = handler_new (after);
1321           
1322           handler_seq_no = handler->sequential_number;
1323           handler->detail = detail;
1324           handler->closure = g_closure_ref (closure);
1325           g_closure_sink (closure);
1326           handler_insert (signal_id, instance, handler);
1327           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1328             g_closure_set_marshal (closure, node->c_marshaller);
1329         }
1330     }
1331   else
1332     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1333   SIGNAL_UNLOCK ();
1334   
1335   return handler_seq_no;
1336 }
1337
1338 gulong
1339 g_signal_connect_closure (gpointer     instance,
1340                           const gchar *detailed_signal,
1341                           GClosure    *closure,
1342                           gboolean     after)
1343 {
1344   guint signal_id;
1345   gulong handler_seq_no = 0;
1346   GQuark detail = 0;
1347   GType itype;
1348
1349   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1350   g_return_val_if_fail (detailed_signal != NULL, 0);
1351   g_return_val_if_fail (closure != NULL, 0);
1352
1353   SIGNAL_LOCK ();
1354   itype = G_TYPE_FROM_INSTANCE (instance);
1355   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1356   if (signal_id)
1357     {
1358       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1359
1360       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1361         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1362       else if (!g_type_is_a (itype, node->itype))
1363         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1364       else
1365         {
1366           Handler *handler = handler_new (after);
1367
1368           handler_seq_no = handler->sequential_number;
1369           handler->detail = detail;
1370           handler->closure = g_closure_ref (closure);
1371           g_closure_sink (closure);
1372           handler_insert (signal_id, instance, handler);
1373           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1374             g_closure_set_marshal (handler->closure, node->c_marshaller);
1375         }
1376     }
1377   else
1378     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1379   SIGNAL_UNLOCK ();
1380
1381   return handler_seq_no;
1382 }
1383
1384 gulong
1385 g_signal_connect_data (gpointer       instance,
1386                        const gchar   *detailed_signal,
1387                        GCallback      c_handler,
1388                        gpointer       data,
1389                        GClosureNotify destroy_data,
1390                        GConnectFlags  connect_flags)
1391 {
1392   guint signal_id;
1393   gulong handler_seq_no = 0;
1394   GQuark detail = 0;
1395   GType itype;
1396   gboolean swapped, after;
1397   
1398   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1399   g_return_val_if_fail (detailed_signal != NULL, 0);
1400   g_return_val_if_fail (c_handler != NULL, 0);
1401
1402   swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1403   after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1404
1405   SIGNAL_LOCK ();
1406   itype = G_TYPE_FROM_INSTANCE (instance);
1407   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1408   if (signal_id)
1409     {
1410       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1411
1412       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1413         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1414       else if (!g_type_is_a (itype, node->itype))
1415         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1416       else
1417         {
1418           Handler *handler = handler_new (after);
1419
1420           handler_seq_no = handler->sequential_number;
1421           handler->detail = detail;
1422           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1423           g_closure_sink (handler->closure);
1424           handler_insert (signal_id, instance, handler);
1425           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1426             g_closure_set_marshal (handler->closure, node->c_marshaller);
1427         }
1428     }
1429   else
1430     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1431   SIGNAL_UNLOCK ();
1432
1433   return handler_seq_no;
1434 }
1435
1436 void
1437 g_signal_handler_block (gpointer instance,
1438                         gulong   handler_id)
1439 {
1440   Handler *handler;
1441   
1442   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1443   g_return_if_fail (handler_id > 0);
1444   
1445   SIGNAL_LOCK ();
1446   handler = handler_lookup (instance, handler_id, NULL);
1447   if (handler)
1448     {
1449 #ifndef G_DISABLE_CHECKS
1450       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1451         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1452 #endif
1453       handler->block_count += 1;
1454     }
1455   else
1456     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1457   SIGNAL_UNLOCK ();
1458 }
1459
1460 void
1461 g_signal_handler_unblock (gpointer instance,
1462                           gulong   handler_id)
1463 {
1464   Handler *handler;
1465   
1466   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1467   g_return_if_fail (handler_id > 0);
1468   
1469   SIGNAL_LOCK ();
1470   handler = handler_lookup (instance, handler_id, NULL);
1471   if (handler)
1472     {
1473       if (handler->block_count)
1474         handler->block_count -= 1;
1475       else
1476         g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1477     }
1478   else
1479     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1480   SIGNAL_UNLOCK ();
1481 }
1482
1483 void
1484 g_signal_handler_disconnect (gpointer instance,
1485                              gulong   handler_id)
1486 {
1487   Handler *handler;
1488   guint signal_id;
1489   
1490   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1491   g_return_if_fail (handler_id > 0);
1492   
1493   SIGNAL_LOCK ();
1494   handler = handler_lookup (instance, handler_id, &signal_id);
1495   if (handler)
1496     {
1497       handler->sequential_number = 0;
1498       handler->block_count = 1;
1499       handler_unref_R (signal_id, instance, handler);
1500     }
1501   else
1502     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1503   SIGNAL_UNLOCK ();
1504 }
1505
1506 gboolean
1507 g_signal_handler_is_connected (gpointer instance,
1508                                gulong   handler_id)
1509 {
1510   Handler *handler;
1511   gboolean connected;
1512
1513   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1514   g_return_val_if_fail (handler_id > 0, FALSE);
1515
1516   SIGNAL_LOCK ();
1517   handler = handler_lookup (instance, handler_id, NULL);
1518   connected = handler != NULL;
1519   SIGNAL_UNLOCK ();
1520
1521   return connected;
1522 }
1523
1524 void
1525 g_signal_handlers_destroy (gpointer instance)
1526 {
1527   GBSearchArray *hlbsa;
1528   
1529   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1530   
1531   SIGNAL_LOCK ();
1532   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1533   if (hlbsa)
1534     {
1535       guint i;
1536       
1537       /* reentrancy caution, delete instance trace first */
1538       g_hash_table_remove (g_handler_list_bsa_ht, instance);
1539       
1540       for (i = 0; i < hlbsa->n_nodes; i++)
1541         {
1542           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1543           Handler *handler = hlist->handlers;
1544           
1545           while (handler)
1546             {
1547               Handler *tmp = handler;
1548               
1549               handler = tmp->next;
1550               tmp->block_count = 1;
1551               /* cruel unlink, this works because _all_ handlers vanish */
1552               tmp->next = NULL;
1553               tmp->prev = tmp;
1554               if (tmp->sequential_number)
1555                 {
1556                   tmp->sequential_number = 0;
1557                   handler_unref_R (0, NULL, tmp);
1558                 }
1559             }
1560         }
1561       g_bsearch_array_destroy (hlbsa);
1562     }
1563   SIGNAL_UNLOCK ();
1564 }
1565
1566 gulong
1567 g_signal_handler_find (gpointer         instance,
1568                        GSignalMatchType mask,
1569                        guint            signal_id,
1570                        GQuark           detail,
1571                        GClosure        *closure,
1572                        gpointer         func,
1573                        gpointer         data)
1574 {
1575   gulong handler_seq_no = 0;
1576   
1577   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1578   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1579   
1580   if (mask & G_SIGNAL_MATCH_MASK)
1581     {
1582       HandlerMatch *mlist;
1583       
1584       SIGNAL_LOCK ();
1585       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1586       if (mlist)
1587         {
1588           handler_seq_no = mlist->handler->sequential_number;
1589           handler_match_free1_R (mlist, instance);
1590         }
1591       SIGNAL_UNLOCK ();
1592     }
1593   
1594   return handler_seq_no;
1595 }
1596
1597 static guint
1598 signal_handlers_foreach_matched_R (gpointer         instance,
1599                                    GSignalMatchType mask,
1600                                    guint            signal_id,
1601                                    GQuark           detail,
1602                                    GClosure        *closure,
1603                                    gpointer         func,
1604                                    gpointer         data,
1605                                    void           (*callback) (gpointer instance,
1606                                                                gulong   handler_seq_no))
1607 {
1608   HandlerMatch *mlist;
1609   guint n_handlers = 0;
1610   
1611   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1612   while (mlist)
1613     {
1614       n_handlers++;
1615       if (mlist->handler->sequential_number)
1616         {
1617           SIGNAL_UNLOCK ();
1618           callback (instance, mlist->handler->sequential_number);
1619           SIGNAL_LOCK ();
1620         }
1621       mlist = handler_match_free1_R (mlist, instance);
1622     }
1623   
1624   return n_handlers;
1625 }
1626
1627 guint
1628 g_signal_handlers_block_matched (gpointer         instance,
1629                                  GSignalMatchType mask,
1630                                  guint            signal_id,
1631                                  GQuark           detail,
1632                                  GClosure        *closure,
1633                                  gpointer         func,
1634                                  gpointer         data)
1635 {
1636   guint n_handlers = 0;
1637   
1638   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1639   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1640   
1641   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1642     {
1643       SIGNAL_LOCK ();
1644       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1645                                                       closure, func, data,
1646                                                       g_signal_handler_block);
1647       SIGNAL_UNLOCK ();
1648     }
1649   
1650   return n_handlers;
1651 }
1652
1653 guint
1654 g_signal_handlers_unblock_matched (gpointer         instance,
1655                                    GSignalMatchType mask,
1656                                    guint            signal_id,
1657                                    GQuark           detail,
1658                                    GClosure        *closure,
1659                                    gpointer         func,
1660                                    gpointer         data)
1661 {
1662   guint n_handlers = 0;
1663   
1664   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1665   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1666   
1667   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1668     {
1669       SIGNAL_LOCK ();
1670       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1671                                                       closure, func, data,
1672                                                       g_signal_handler_unblock);
1673       SIGNAL_UNLOCK ();
1674     }
1675   
1676   return n_handlers;
1677 }
1678
1679 guint
1680 g_signal_handlers_disconnect_matched (gpointer         instance,
1681                                       GSignalMatchType mask,
1682                                       guint            signal_id,
1683                                       GQuark           detail,
1684                                       GClosure        *closure,
1685                                       gpointer         func,
1686                                       gpointer         data)
1687 {
1688   guint n_handlers = 0;
1689   
1690   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1691   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1692   
1693   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1694     {
1695       SIGNAL_LOCK ();
1696       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1697                                                       closure, func, data,
1698                                                       g_signal_handler_disconnect);
1699       SIGNAL_UNLOCK ();
1700     }
1701   
1702   return n_handlers;
1703 }
1704
1705 gboolean
1706 g_signal_has_handler_pending (gpointer instance,
1707                               guint    signal_id,
1708                               GQuark   detail,
1709                               gboolean may_be_blocked)
1710 {
1711   HandlerMatch *mlist;
1712   gboolean has_pending;
1713   
1714   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1715   g_return_val_if_fail (signal_id > 0, FALSE);
1716   
1717   SIGNAL_LOCK ();
1718   if (detail)
1719     {
1720       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1721       
1722       if (!(node->flags & G_SIGNAL_DETAILED))
1723         {
1724           g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1725           SIGNAL_UNLOCK ();
1726           return FALSE;
1727         }
1728     }
1729   mlist = handlers_find (instance,
1730                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1731                          signal_id, detail, NULL, NULL, NULL, TRUE);
1732   if (mlist)
1733     {
1734       has_pending = TRUE;
1735       handler_match_free1_R (mlist, instance);
1736     }
1737   else
1738     has_pending = FALSE;
1739   SIGNAL_UNLOCK ();
1740   
1741   return has_pending;
1742 }
1743
1744 void
1745 g_signal_emitv (const GValue *instance_and_params,
1746                 guint         signal_id,
1747                 GQuark        detail,
1748                 GValue       *return_value)
1749 {
1750   const GValue *param_values;
1751   gpointer instance;
1752   SignalNode *node;
1753 #ifdef G_ENABLE_DEBUG
1754   guint i;
1755 #endif
1756   
1757   g_return_if_fail (instance_and_params != NULL);
1758   instance = g_value_peek_pointer (instance_and_params);
1759   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1760   g_return_if_fail (signal_id > 0);
1761
1762   param_values = instance_and_params + 1;
1763   
1764   SIGNAL_LOCK ();
1765   node = LOOKUP_SIGNAL_NODE (signal_id);
1766   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1767     {
1768       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1769       SIGNAL_UNLOCK ();
1770       return;
1771     }
1772 #ifdef G_ENABLE_DEBUG
1773   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1774     {
1775       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1776       SIGNAL_UNLOCK ();
1777       return;
1778     }
1779   for (i = 0; i < node->n_params; i++)
1780     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1781       {
1782         g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1783                     G_STRLOC,
1784                     g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1785                     i,
1786                     node->name,
1787                     G_VALUE_TYPE_NAME (param_values + i));
1788         SIGNAL_UNLOCK ();
1789         return;
1790       }
1791   if (node->return_type != G_TYPE_NONE)
1792     {
1793       if (!return_value)
1794         {
1795           g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1796                       G_STRLOC,
1797                       g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1798                       node->name);
1799           SIGNAL_UNLOCK ();
1800           return;
1801         }
1802       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1803         {
1804           g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1805                       G_STRLOC,
1806                       g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1807                       node->name,
1808                       G_VALUE_TYPE_NAME (return_value));
1809           SIGNAL_UNLOCK ();
1810           return;
1811         }
1812     }
1813   else
1814     return_value = NULL;
1815 #endif  /* G_ENABLE_DEBUG */
1816
1817   SIGNAL_UNLOCK ();
1818   signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
1819 }
1820
1821 void
1822 g_signal_emit_valist (gpointer instance,
1823                       guint    signal_id,
1824                       GQuark   detail,
1825                       va_list  var_args)
1826 {
1827   GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1828   GType signal_return_type;
1829   GValue *param_values;
1830   SignalNode *node;
1831   guint i, n_params;
1832   
1833   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1834   g_return_if_fail (signal_id > 0);
1835
1836   SIGNAL_LOCK ();
1837   node = LOOKUP_SIGNAL_NODE (signal_id);
1838   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1839     {
1840       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1841       SIGNAL_UNLOCK ();
1842       return;
1843     }
1844 #ifndef G_DISABLE_CHECKS
1845   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1846     {
1847       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1848       SIGNAL_UNLOCK ();
1849       return;
1850     }
1851 #endif  /* !G_DISABLE_CHECKS */
1852
1853   n_params = node->n_params;
1854   signal_return_type = node->return_type;
1855   if (node->n_params < MAX_STACK_VALUES)
1856     instance_and_params = stack_values;
1857   else
1858     {
1859       free_me = g_new (GValue, node->n_params + 1);
1860       instance_and_params = free_me;
1861     }
1862   param_values = instance_and_params + 1;
1863   for (i = 0; i < node->n_params; i++)
1864     {
1865       gchar *error;
1866       GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1867       gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1868       
1869       param_values[i].g_type = 0;
1870       SIGNAL_UNLOCK ();
1871       g_value_init (param_values + i, ptype);
1872       G_VALUE_COLLECT (param_values + i,
1873                        var_args,
1874                        static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1875                        &error);
1876       if (error)
1877         {
1878           g_warning ("%s: %s", G_STRLOC, error);
1879           g_free (error);
1880
1881           /* we purposely leak the value here, it might not be
1882            * in a sane state if an error condition occoured
1883            */
1884           while (i--)
1885             g_value_unset (param_values + i);
1886
1887           g_free (free_me);
1888           return;
1889         }
1890       SIGNAL_LOCK ();
1891     }
1892   SIGNAL_UNLOCK ();
1893   instance_and_params->g_type = 0;
1894   g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1895   g_value_set_instance (instance_and_params, instance);
1896   if (signal_return_type == G_TYPE_NONE)
1897     signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
1898   else
1899     {
1900       GValue return_value = { 0, };
1901       gchar *error = NULL;
1902       GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1903       gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
1904       
1905       g_value_init (&return_value, rtype);
1906
1907       signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
1908
1909       G_VALUE_LCOPY (&return_value,
1910                      var_args,
1911                      static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1912                      &error);
1913       if (!error)
1914         g_value_unset (&return_value);
1915       else
1916         {
1917           g_warning ("%s: %s", G_STRLOC, error);
1918           g_free (error);
1919           
1920           /* we purposely leak the value here, it might not be
1921            * in a sane state if an error condition occoured
1922            */
1923         }
1924     }
1925   for (i = 0; i < n_params; i++)
1926     g_value_unset (param_values + i);
1927   g_value_unset (instance_and_params);
1928   if (free_me)
1929     g_free (free_me);
1930 }
1931
1932 void
1933 g_signal_emit (gpointer instance,
1934                guint    signal_id,
1935                GQuark   detail,
1936                ...)
1937 {
1938   va_list var_args;
1939
1940   va_start (var_args, detail);
1941   g_signal_emit_valist (instance, signal_id, detail, var_args);
1942   va_end (var_args);
1943 }
1944
1945 void
1946 g_signal_emit_by_name (gpointer     instance,
1947                        const gchar *detailed_signal,
1948                        ...)
1949 {
1950   GQuark detail = 0;
1951   guint signal_id;
1952
1953   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1954   g_return_if_fail (detailed_signal != NULL);
1955
1956   SIGNAL_LOCK ();
1957   signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1958   SIGNAL_UNLOCK ();
1959
1960   if (signal_id)
1961     {
1962       va_list var_args;
1963
1964       va_start (var_args, detailed_signal);
1965       g_signal_emit_valist (instance, signal_id, detail, var_args);
1966       va_end (var_args);
1967     }
1968   else
1969     g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1970 }
1971
1972 static inline gboolean
1973 accumulate (GSignalInvocationHint *ihint,
1974             GValue                *return_accu,
1975             GValue                *handler_return,
1976             SignalAccumulator     *accumulator)
1977 {
1978   gboolean continue_emission;
1979
1980   if (!accumulator)
1981     return TRUE;
1982
1983   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1984   g_value_reset (handler_return);
1985
1986   return continue_emission;
1987 }
1988
1989 static gboolean
1990 signal_emit_unlocked_R (SignalNode   *node,
1991                         GQuark        detail,
1992                         gpointer      instance,
1993                         GValue       *emission_return,
1994                         const GValue *instance_and_params)
1995 {
1996   EmissionState emission_state = 0;
1997   SignalAccumulator *accumulator;
1998   GSignalInvocationHint ihint;
1999   GClosure *class_closure;
2000   HandlerList *hlist;
2001   Handler *handler_list = NULL;
2002   GValue *return_accu, accu = { 0, };
2003   guint signal_id;
2004   gulong max_sequential_handler_number;
2005   gboolean return_value_altered = FALSE;
2006   
2007 #ifdef  G_ENABLE_DEBUG
2008   IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2009     {
2010       g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2011                  g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2012                  node->name, detail,
2013                  instance, node);
2014       if (g_trap_instance_signals == instance)
2015         G_BREAKPOINT ();
2016     }
2017 #endif  /* G_ENABLE_DEBUG */
2018   
2019   SIGNAL_LOCK ();
2020   signal_id = node->signal_id;
2021   if (node->flags & G_SIGNAL_NO_RECURSE)
2022     {
2023       Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
2024       
2025       if (emission)
2026         {
2027           *emission->state_p = EMISSION_RESTART;
2028           SIGNAL_UNLOCK ();
2029           return return_value_altered;
2030         }
2031     }
2032   ihint.signal_id = node->signal_id;
2033   ihint.detail = detail;
2034   accumulator = node->accumulator;
2035   if (accumulator)
2036     {
2037       SIGNAL_UNLOCK ();
2038       g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2039       return_accu = &accu;
2040       SIGNAL_LOCK ();
2041     }
2042   else
2043     return_accu = emission_return;
2044   emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
2045                  signal_id, detail, instance, &emission_state);
2046   class_closure = node->class_closure;
2047   
2048  EMIT_RESTART:
2049   
2050   if (handler_list)
2051     handler_unref_R (signal_id, instance, handler_list);
2052   max_sequential_handler_number = g_handler_sequential_number;
2053   hlist = handler_list_lookup (signal_id, instance);
2054   handler_list = hlist ? hlist->handlers : NULL;
2055   if (handler_list)
2056     handler_ref (handler_list);
2057   
2058   ihint.run_type = G_SIGNAL_RUN_FIRST;
2059   
2060   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2061     {
2062       emission_state = EMISSION_RUN;
2063       
2064       SIGNAL_UNLOCK ();
2065       g_closure_invoke (class_closure,
2066                         return_accu,
2067                         node->n_params + 1,
2068                         instance_and_params,
2069                         &ihint);
2070       if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2071           emission_state == EMISSION_RUN)
2072         emission_state = EMISSION_STOP;
2073       SIGNAL_LOCK ();
2074       return_value_altered = TRUE;
2075       
2076       if (emission_state == EMISSION_STOP)
2077         goto EMIT_CLEANUP;
2078       else if (emission_state == EMISSION_RESTART)
2079         goto EMIT_RESTART;
2080     }
2081   
2082   if (node->emission_hooks)
2083     {
2084       gboolean need_destroy, was_in_call, may_recurse = TRUE;
2085       GHook *hook;
2086
2087       emission_state = EMISSION_HOOK;
2088       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2089       while (hook)
2090         {
2091           SignalHook *signal_hook = SIGNAL_HOOK (hook);
2092           
2093           if (!signal_hook->detail || signal_hook->detail == detail)
2094             {
2095               GSignalEmissionHook hook_func = hook->func;
2096               
2097               was_in_call = G_HOOK_IN_CALL (hook);
2098               hook->flags |= G_HOOK_FLAG_IN_CALL;
2099               SIGNAL_UNLOCK ();
2100               need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
2101               SIGNAL_LOCK ();
2102               if (!was_in_call)
2103                 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2104               if (need_destroy)
2105                 g_hook_destroy_link (node->emission_hooks, hook);
2106             }
2107           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2108         }
2109       
2110       if (emission_state == EMISSION_RESTART)
2111         goto EMIT_RESTART;
2112     }
2113   
2114   if (handler_list)
2115     {
2116       Handler *handler = handler_list;
2117       
2118       emission_state = EMISSION_RUN;
2119       handler_ref (handler);
2120       do
2121         {
2122           Handler *tmp;
2123           
2124           if (handler->after)
2125             {
2126               handler_unref_R (signal_id, instance, handler_list);
2127               handler_list = handler;
2128               break;
2129             }
2130           else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2131                    handler->sequential_number < max_sequential_handler_number)
2132             {
2133               SIGNAL_UNLOCK ();
2134               g_closure_invoke (handler->closure,
2135                                 return_accu,
2136                                 node->n_params + 1,
2137                                 instance_and_params,
2138                                 &ihint);
2139               if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2140                   emission_state == EMISSION_RUN)
2141                 emission_state = EMISSION_STOP;
2142               SIGNAL_LOCK ();
2143               return_value_altered = TRUE;
2144               
2145               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2146             }
2147           else
2148             tmp = handler->next;
2149           
2150           if (tmp)
2151             handler_ref (tmp);
2152           handler_unref_R (signal_id, instance, handler_list);
2153           handler_list = handler;
2154           handler = tmp;
2155         }
2156       while (handler);
2157       
2158       if (emission_state == EMISSION_STOP)
2159         goto EMIT_CLEANUP;
2160       else if (emission_state == EMISSION_RESTART)
2161         goto EMIT_RESTART;
2162     }
2163   
2164   ihint.run_type = G_SIGNAL_RUN_LAST;
2165   
2166   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2167     {
2168       emission_state = EMISSION_RUN;
2169       
2170       SIGNAL_UNLOCK ();
2171       g_closure_invoke (class_closure,
2172                         return_accu,
2173                         node->n_params + 1,
2174                         instance_and_params,
2175                         &ihint);
2176       if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2177           emission_state == EMISSION_RUN)
2178         emission_state = EMISSION_STOP;
2179       SIGNAL_LOCK ();
2180       return_value_altered = TRUE;
2181       
2182       if (emission_state == EMISSION_STOP)
2183         goto EMIT_CLEANUP;
2184       else if (emission_state == EMISSION_RESTART)
2185         goto EMIT_RESTART;
2186     }
2187   
2188   if (handler_list)
2189     {
2190       Handler *handler = handler_list;
2191       
2192       emission_state = EMISSION_RUN;
2193       handler_ref (handler);
2194       do
2195         {
2196           Handler *tmp;
2197           
2198           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2199               handler->sequential_number < max_sequential_handler_number)
2200             {
2201               SIGNAL_UNLOCK ();
2202               g_closure_invoke (handler->closure,
2203                                 return_accu,
2204                                 node->n_params + 1,
2205                                 instance_and_params,
2206                                 &ihint);
2207               if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2208                   emission_state == EMISSION_RUN)
2209                 emission_state = EMISSION_STOP;
2210               SIGNAL_LOCK ();
2211               return_value_altered = TRUE;
2212               
2213               tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2214             }
2215           else
2216             tmp = handler->next;
2217           
2218           if (tmp)
2219             handler_ref (tmp);
2220           handler_unref_R (signal_id, instance, handler);
2221           handler = tmp;
2222         }
2223       while (handler);
2224       
2225       if (emission_state == EMISSION_STOP)
2226         goto EMIT_CLEANUP;
2227       else if (emission_state == EMISSION_RESTART)
2228         goto EMIT_RESTART;
2229     }
2230   
2231  EMIT_CLEANUP:
2232   
2233   ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2234   
2235   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2236     {
2237       gboolean need_unset = FALSE;
2238       
2239       emission_state = EMISSION_STOP;
2240       
2241       SIGNAL_UNLOCK ();
2242       if (node->return_type != G_TYPE_NONE && !accumulator)
2243         {
2244           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2245           need_unset = TRUE;
2246         }
2247       g_closure_invoke (class_closure,
2248                         node->return_type != G_TYPE_NONE ? &accu : NULL,
2249                         node->n_params + 1,
2250                         instance_and_params,
2251                         &ihint);
2252       if (need_unset)
2253         g_value_unset (&accu);
2254       SIGNAL_LOCK ();
2255       
2256       if (emission_state == EMISSION_RESTART)
2257         goto EMIT_RESTART;
2258     }
2259   
2260   if (handler_list)
2261     handler_unref_R (signal_id, instance, handler_list);
2262   
2263   emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2264   SIGNAL_UNLOCK ();
2265   if (accumulator)
2266     g_value_unset (&accu);
2267   
2268   return return_value_altered;
2269 }
2270
2271
2272 /* --- compile standard marshallers --- */
2273 #include        "gobject.h"
2274 #include        "genums.h"
2275 #include        "gmarshal.c"