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