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