1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
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.
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.
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.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
28 #include "gbsearcharray.h"
29 #include "gvaluecollector.h"
30 #include "gvaluetypes.h"
35 /* pre allocation configurations
37 #define MAX_STACK_VALUES (16)
38 #define HANDLER_PRE_ALLOC (48)
39 #define EMISSION_PRE_ALLOC (16)
41 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
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 */
49 /* --- generic allocation --- */
50 /* we special case allocations generically by replacing
51 * these functions with more speed/memory aware variants
53 #ifndef DISABLE_MEM_POOLS
54 static inline gpointer
55 g_generic_node_alloc (GTrashStack **trash_stack_p,
57 guint nodes_pre_alloc)
59 gpointer node = g_trash_stack_pop (trash_stack_p);
65 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
66 block = g_malloc (sizeof_node * nodes_pre_alloc);
67 while (--nodes_pre_alloc)
69 g_trash_stack_push (trash_stack_p, block);
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 */
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;
100 /* --- prototypes --- */
101 static inline guint signal_id_lookup (GQuark quark,
103 static void signal_destroy_R (SignalNode *signal_node);
104 static inline HandlerList* handler_list_ensure (guint signal_id,
106 static inline HandlerList* handler_list_lookup (guint signal_id,
108 static inline Handler* handler_new (gboolean after);
109 static void handler_insert (guint signal_id,
112 static Handler* handler_lookup (gpointer instance,
115 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
118 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
120 static HandlerMatch* handlers_find (gpointer instance,
121 GSignalMatchType mask,
127 gboolean one_and_only);
128 static inline void handler_ref (Handler *handler);
129 static inline void handler_unref_R (guint signal_id,
132 static inline void emission_push (Emission **emission_list_p,
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,
143 static gint signal_key_cmp (gconstpointer node1,
144 gconstpointer node2);
145 static gboolean signal_emit_R (SignalNode *node,
148 GValue *return_value,
149 const GValue *instance_and_params);
152 /* --- structures --- */
155 GSignalAccumulator func;
163 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
167 /* permanent portion */
173 /* reinitializable portion */
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;
197 EmissionState *state_p;
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)
229 /* --- variables --- */
230 static GBSearchArray g_signal_key_bsa = G_STATIC_BSEARCH_ARRAY_INIT (sizeof (SignalKey),
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);
241 /* --- signal nodes --- */
242 static guint g_n_signal_nodes = 0;
243 static SignalNode **g_signal_nodes = NULL;
245 static inline SignalNode*
246 LOOKUP_SIGNAL_NODE (register guint signal_id)
248 if (signal_id < g_n_signal_nodes)
249 return g_signal_nodes[signal_id];
255 /* --- functions --- */
257 signal_id_lookup (GQuark quark,
260 GType *ifaces, type = itype;
266 /* try looking up signals for this type and its anchestors */
269 SignalKey *signal_key;
272 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
275 return signal_key->signal_id;
277 type = g_type_parent (type);
281 /* no luck, try interfaces it exports */
282 ifaces = g_type_interfaces (itype, &n_ifaces);
285 SignalKey *signal_key;
287 key.itype = ifaces[n_ifaces];
288 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
293 return signal_key->signal_id;
302 handler_lists_cmp (gconstpointer node1,
305 const HandlerList *hlist1 = node1, *hlist2 = node2;
307 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
310 static inline HandlerList*
311 handler_list_ensure (guint signal_id,
314 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
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);
322 key.signal_id = signal_id;
325 return g_bsearch_array_insert (hlbsa, &key, FALSE);
328 static inline HandlerList*
329 handler_list_lookup (guint signal_id,
332 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
335 key.signal_id = signal_id;
337 return hlbsa ? g_bsearch_array_lookup (hlbsa, &key) : NULL;
341 handler_lookup (gpointer instance,
345 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
351 for (i = 0; i < hlbsa->n_nodes; i++)
353 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
356 for (handler = hlist->handlers; handler; handler = handler->next)
357 if (handler->id == handler_id)
360 *signal_id_p = hlist->signal_id;
370 static inline HandlerMatch*
371 handler_match_prepend (HandlerMatch *list,
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.
382 node = (HandlerMatch*) g_list_alloc ();
383 node->handler = handler;
385 node->d.signal_id = signal_id;
386 handler_ref (handler);
390 static inline HandlerMatch*
391 handler_match_free1_R (HandlerMatch *node,
394 HandlerMatch *next = node->next;
396 handler_unref_R (node->d.signal_id, instance, node->handler);
397 g_list_free_1 ((GList*) node);
403 handlers_find (gpointer instance,
404 GSignalMatchType mask,
410 gboolean one_and_only)
412 HandlerMatch *mlist = NULL;
414 if (mask & G_SIGNAL_MATCH_ID)
416 HandlerList *hlist = handler_list_lookup (signal_id, instance);
418 SignalNode *node = NULL;
420 if (mask & G_SIGNAL_MATCH_FUNC)
422 node = LOOKUP_SIGNAL_NODE (signal_id);
423 if (!node || !node->c_marshaller)
428 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
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)))
438 mlist = handler_match_prepend (mlist, handler, signal_id);
445 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
452 for (i = 0; i < hlbsa->n_nodes; i++)
454 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
455 SignalNode *node = NULL;
458 if (!(mask & G_SIGNAL_MATCH_FUNC))
460 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
461 if (!node->c_marshaller)
465 for (handler = hlist->handlers; handler; handler = handler->next)
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)))
475 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
486 static inline Handler*
487 handler_new (gboolean after)
489 static guint handler_id = 1;
490 Handler *handler = g_generic_node_alloc (&g_handler_ts,
493 #ifndef G_DISABLE_CHECKS
495 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
498 handler->id = handler_id++;
499 handler->prev = NULL;
500 handler->next = NULL;
502 handler->ref_count = 1;
503 handler->block_count = 0;
504 handler->after = after != FALSE;
505 handler->closure = NULL;
511 handler_ref (Handler *handler)
513 g_return_if_fail (handler->ref_count > 0);
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);
520 handler->ref_count += 1;
524 handler_unref_R (guint signal_id,
528 g_return_if_fail (handler->ref_count > 0);
530 handler->ref_count -= 1;
531 if (!handler->ref_count)
534 handler->next->prev = handler->prev;
535 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
536 handler->prev->next = handler->next;
539 HandlerList *hlist = handler_list_lookup (signal_id, instance);
541 hlist->handlers = handler->next;
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);
551 handler_insert (guint signal_id,
557 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
559 hlist = handler_list_ensure (signal_id, instance);
560 if (!hlist->handlers)
561 hlist->handlers = handler;
562 else if (hlist->handlers->after && !handler->after)
564 handler->next = hlist->handlers;
565 hlist->handlers->prev = handler;
566 hlist->handlers = handler;
570 Handler *tmp = hlist->handlers;
576 while (tmp->next && !tmp->next->after)
579 tmp->next->prev = handler;
580 handler->next = tmp->next;
587 emission_push (Emission **emission_list_p,
591 EmissionState *state_p)
593 Emission *emission = g_generic_node_alloc (&g_emission_ts,
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;
605 emission_pop (Emission **emission_list_p,
606 EmissionState *state_p)
608 Emission **loc = emission_list_p, *emission = *loc;
610 while (emission->state_p != state_p)
612 loc = &emission->next;
615 *loc = emission->next;
616 g_generic_node_free (&g_emission_ts, emission);
619 static inline Emission*
620 emission_find (Emission *emission_list,
627 for (emission = emission_list; emission; emission = emission->next)
628 if (emission->instance == instance &&
629 emission->signal_id == signal_id &&
630 emission->detail == detail)
636 signal_key_cmp (gconstpointer node1,
639 const SignalKey *key1 = node1, *key2 = node2;
641 if (key1->itype == key2->itype)
642 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
644 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
648 g_signal_init (void) /* sync with gtype.c */
650 G_LOCK (g_signal_mutex);
651 if (!g_n_signal_nodes)
653 /* handler_id_node_prepend() requires this */
654 g_assert (sizeof (GList) == sizeof (HandlerMatch));
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);
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;
664 G_UNLOCK (g_signal_mutex);
668 _g_signals_destroy (GType itype)
672 G_LOCK (g_signal_mutex);
673 for (i = 1; i < g_n_signal_nodes; i++)
675 SignalNode *node = g_signal_nodes[i];
677 if (node->itype == itype)
680 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
682 g_type_name (node->itype));
684 signal_destroy_R (node);
687 G_UNLOCK (g_signal_mutex);
691 g_signal_stop_emission (gpointer instance,
697 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
698 g_return_if_fail (signal_id > 0);
700 G_LOCK (g_signal_mutex);
701 node = LOOKUP_SIGNAL_NODE (signal_id);
702 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
704 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
705 G_UNLOCK (g_signal_mutex);
708 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
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);
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;
722 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
723 node->name, instance);
726 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
727 G_UNLOCK (g_signal_mutex);
731 signal_finalize_hook (GHookList *hook_list,
734 GDestroyNotify destroy = hook->destroy;
738 hook->destroy = NULL;
739 G_UNLOCK (g_signal_mutex);
740 destroy (hook->data);
741 G_LOCK (g_signal_mutex);
746 g_signal_add_emission_hook (guint signal_id,
748 GSignalEmissionHook hook_func,
750 GDestroyNotify data_destroy)
752 static guint seq_hook_id = 1;
755 SignalHook *signal_hook;
757 g_return_val_if_fail (signal_id > 0, 0);
758 g_return_val_if_fail (hook_func != NULL, 0);
760 G_LOCK (g_signal_mutex);
761 node = LOOKUP_SIGNAL_NODE (signal_id);
762 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
764 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
765 G_UNLOCK (g_signal_mutex);
768 if (detail && !(node->flags & G_SIGNAL_DETAILED))
770 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
771 G_UNLOCK (g_signal_mutex);
774 if (!node->emission_hooks)
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;
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);
791 return hook->hook_id;
795 g_signal_remove_emission_hook (guint signal_id,
800 g_return_if_fail (signal_id > 0);
801 g_return_if_fail (hook_id > 0);
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);
813 signal_parse_name (const gchar *name,
816 gboolean force_quark)
818 const gchar *colon = strchr (name, ':');
823 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
824 if (signal_id && detail_p)
827 else if (colon[1] == ':')
830 guint l = colon - name;
834 memcpy (buffer, name, l);
836 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
840 gchar *signal = g_new (gchar, l + 1);
842 memcpy (signal, name, l);
844 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
848 if (signal_id && detail_p)
849 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
857 g_signal_parse_name (const gchar *detailed_signal,
861 gboolean force_detail_quark)
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);
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);
874 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
875 if (!node || node->destroyed ||
876 (detail && !(node->flags & G_SIGNAL_DETAILED)))
880 *signal_id_p = signal_id;
888 g_signal_lookup (const gchar *name,
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);
896 G_LOCK (g_signal_mutex);
897 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
898 G_UNLOCK (g_signal_mutex);
903 G_CONST_RETURN gchar*
904 g_signal_name (guint signal_id)
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);
918 g_signal_query (guint signal_id,
923 g_return_if_fail (query != NULL);
925 G_LOCK (g_signal_mutex);
926 node = LOOKUP_SIGNAL_NODE (signal_id);
927 if (!node || node->destroyed)
928 query->signal_id = 0;
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;
939 G_UNLOCK (g_signal_mutex);
943 g_signal_list_ids (GType itype,
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);
954 G_LOCK (g_signal_mutex);
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));
960 for (i = 0; i < n_nodes; i++)
961 if (keys[i].itype == itype)
963 const gchar *name = g_quark_to_string (keys[i].quark);
965 /* Signal names with "_" in them are aliases to the same
966 * name with "-" instead of "_".
968 if (!strchr (name, '_'))
969 g_array_append_val (result, keys[i].signal_id);
972 *n_ids = result->len;
974 G_UNLOCK (g_signal_mutex);
976 return (guint*) g_array_free (result, FALSE);
980 g_signal_new_valist (const gchar *signal_name,
982 GSignalFlags signal_flags,
983 GClosure *class_closure,
984 GSignalAccumulator accumulator,
986 GSignalCMarshaller c_marshaller,
997 param_types = g_new (GType, n_params);
999 for (i = 0; i < n_params; i++)
1000 param_types[i] = va_arg (args, GType);
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);
1014 g_signal_newc (const gchar *signal_name,
1016 GSignalFlags signal_flags,
1018 GSignalAccumulator accumulator,
1020 GSignalCMarshaller c_marshaller,
1028 g_return_val_if_fail (signal_name != NULL, 0);
1030 va_start (args, n_params);
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);
1043 g_signal_newv (const gchar *signal_name,
1045 GSignalFlags signal_flags,
1046 GClosure *class_closure,
1047 GSignalAccumulator accumulator,
1049 GSignalCMarshaller c_marshaller,
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);
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);
1066 g_return_val_if_fail (accu_data == NULL, 0);
1068 name = g_strdup (signal_name);
1069 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); // FIXME do character checks like for types
1071 G_LOCK (g_signal_mutex);
1073 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1074 node = LOOKUP_SIGNAL_NODE (signal_id);
1075 if (node && !node->destroyed)
1077 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1079 g_type_name (node->itype),
1080 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1082 G_UNLOCK (g_signal_mutex);
1085 if (node && node->itype != itype)
1087 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1089 g_type_name (itype),
1090 g_type_name (node->itype));
1092 G_UNLOCK (g_signal_mutex);
1095 for (i = 0; i < n_params; i++)
1096 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
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);
1101 G_UNLOCK (g_signal_mutex);
1104 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
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);
1109 G_UNLOCK (g_signal_mutex);
1113 /* setup permanent portion of signal node */
1118 signal_id = g_n_signal_nodes++;
1119 node = g_new (SignalNode, 1);
1120 node->signal_id = signal_id;
1121 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1122 g_signal_nodes[signal_id] = node;
1123 node->itype = itype;
1126 key.quark = g_quark_from_string (node->name);
1127 key.signal_id = signal_id;
1128 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1129 g_strdelimit (node->name, "_", '-');
1130 key.quark = g_quark_from_static_string (node->name);
1131 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1133 node->destroyed = FALSE;
1135 /* setup reinitializable portion */
1136 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1137 node->n_params = n_params;
1138 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1139 node->return_type = return_type;
1140 node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
1142 g_closure_sink (class_closure);
1145 node->accumulator = g_new (SignalAccumulator, 1);
1146 node->accumulator->func = accumulator;
1147 node->accumulator->data = accu_data;
1150 node->accumulator = NULL;
1151 node->c_marshaller = c_marshaller;
1152 node->emission_hooks = NULL;
1153 if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
1154 g_closure_set_marshal (class_closure, node->c_marshaller);
1155 G_UNLOCK (g_signal_mutex);
1160 signal_destroy_R (SignalNode *signal_node)
1162 SignalNode node = *signal_node;
1164 signal_node->destroyed = TRUE;
1166 /* reentrancy caution, zero out real contents first */
1167 signal_node->n_params = 0;
1168 signal_node->param_types = NULL;
1169 signal_node->return_type = 0;
1170 signal_node->class_closure = NULL;
1171 signal_node->accumulator = NULL;
1172 signal_node->c_marshaller = NULL;
1173 signal_node->emission_hooks = NULL;
1175 #ifdef G_ENABLE_DEBUG
1176 /* check current emissions */
1180 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1181 emission; emission = emission->next)
1182 if (emission->signal_id == node.signal_id)
1183 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1184 node.name, emission->instance);
1188 /* free contents that need to
1190 G_UNLOCK (g_signal_mutex);
1191 g_free (node.param_types);
1192 g_closure_unref (node.class_closure);
1193 g_free (node.accumulator);
1194 if (node.emission_hooks)
1196 g_hook_list_clear (node.emission_hooks);
1197 g_free (node.emission_hooks);
1199 G_LOCK (g_signal_mutex);
1203 g_signal_connect_closure_by_id (gpointer instance,
1210 guint handler_id = 0;
1212 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1213 g_return_val_if_fail (signal_id > 0, 0);
1214 g_return_val_if_fail (closure != NULL, 0);
1216 G_LOCK (g_signal_mutex);
1217 node = LOOKUP_SIGNAL_NODE (signal_id);
1220 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1221 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1222 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1223 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1226 Handler *handler = handler_new (after);
1228 handler_id = handler->id;
1229 handler->detail = detail;
1230 handler->closure = g_closure_ref (closure);
1231 g_closure_sink (closure);
1232 handler_insert (signal_id, instance, handler);
1233 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1234 g_closure_set_marshal (closure, node->c_marshaller);
1238 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1239 G_UNLOCK (g_signal_mutex);
1245 g_signal_connect_closure (gpointer instance,
1246 const gchar *detailed_signal,
1250 guint signal_id, handler_id = 0;
1254 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1255 g_return_val_if_fail (detailed_signal != NULL, 0);
1256 g_return_val_if_fail (closure != NULL, 0);
1258 G_LOCK (g_signal_mutex);
1259 itype = G_TYPE_FROM_INSTANCE (instance);
1260 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1263 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1265 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1266 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1267 else if (!g_type_is_a (itype, node->itype))
1268 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1271 Handler *handler = handler_new (after);
1273 handler_id = handler->id;
1274 handler->detail = detail;
1275 handler->closure = g_closure_ref (closure);
1276 g_closure_sink (closure);
1277 handler_insert (signal_id, instance, handler);
1278 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1279 g_closure_set_marshal (handler->closure, node->c_marshaller);
1283 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1284 G_UNLOCK (g_signal_mutex);
1290 g_signal_connect_data (gpointer instance,
1291 const gchar *detailed_signal,
1292 GCallback c_handler,
1294 GClosureNotify destroy_data,
1298 guint signal_id, handler_id = 0;
1302 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1303 g_return_val_if_fail (detailed_signal != NULL, 0);
1304 g_return_val_if_fail (c_handler != NULL, 0);
1306 G_LOCK (g_signal_mutex);
1307 itype = G_TYPE_FROM_INSTANCE (instance);
1308 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1311 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1313 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1314 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1315 else if (!g_type_is_a (itype, node->itype))
1316 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1319 Handler *handler = handler_new (after);
1321 handler_id = handler->id;
1322 handler->detail = detail;
1323 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1324 g_closure_sink (handler->closure);
1325 handler_insert (signal_id, instance, handler);
1326 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1327 g_closure_set_marshal (handler->closure, node->c_marshaller);
1331 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1332 G_UNLOCK (g_signal_mutex);
1338 g_signal_handler_block (gpointer instance,
1343 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1344 g_return_if_fail (handler_id > 0);
1346 G_LOCK (g_signal_mutex);
1347 handler = handler_lookup (instance, handler_id, NULL);
1350 #ifndef G_DISABLE_CHECKS
1351 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1352 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1355 handler->block_count += 1;
1358 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1359 G_UNLOCK (g_signal_mutex);
1363 g_signal_handler_unblock (gpointer instance,
1368 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1369 g_return_if_fail (handler_id > 0);
1371 G_LOCK (g_signal_mutex);
1372 handler = handler_lookup (instance, handler_id, NULL);
1375 if (handler->block_count)
1376 handler->block_count -= 1;
1378 g_warning (G_STRLOC ": handler `%u' of instance `%p' is not blocked", handler_id, instance);
1381 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1382 G_UNLOCK (g_signal_mutex);
1386 g_signal_handler_disconnect (gpointer instance,
1392 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1393 g_return_if_fail (handler_id > 0);
1395 G_LOCK (g_signal_mutex);
1396 handler = handler_lookup (instance, handler_id, &signal_id);
1400 handler->block_count = 1;
1401 handler_unref_R (signal_id, instance, handler);
1404 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1405 G_UNLOCK (g_signal_mutex);
1409 g_signal_handlers_destroy (gpointer instance)
1411 GBSearchArray *hlbsa;
1413 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1415 G_LOCK (g_signal_mutex);
1416 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1421 /* reentrancy caution, delete instance trace first */
1422 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1424 for (i = 0; i < hlbsa->n_nodes; i++)
1426 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1427 Handler *handler = hlist->handlers;
1431 Handler *tmp = handler;
1433 handler = tmp->next;
1434 tmp->block_count = 1;
1435 /* cruel unlink, this works because _all_ handlers vanish */
1441 handler_unref_R (0, NULL, tmp);
1445 g_bsearch_array_destroy (hlbsa);
1447 G_UNLOCK (g_signal_mutex);
1451 g_signal_handler_find (gpointer instance,
1452 GSignalMatchType mask,
1459 guint handler_id = 0;
1461 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1462 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1464 if (mask & G_SIGNAL_MATCH_MASK)
1466 HandlerMatch *mlist;
1468 G_LOCK (g_signal_mutex);
1469 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1472 handler_id = mlist->handler->id;
1473 handler_match_free1_R (mlist, instance);
1475 G_UNLOCK (g_signal_mutex);
1482 signal_handlers_foreach_matched_R (gpointer instance,
1483 GSignalMatchType mask,
1489 void (*callback) (gpointer instance,
1492 HandlerMatch *mlist;
1493 guint n_handlers = 0;
1495 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1499 G_UNLOCK (g_signal_mutex);
1500 callback (instance, mlist->handler->id);
1501 G_LOCK (g_signal_mutex);
1502 mlist = handler_match_free1_R (mlist, instance);
1509 g_signal_handlers_block_matched (gpointer instance,
1510 GSignalMatchType mask,
1517 guint n_handlers = 0;
1519 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1520 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1522 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1524 G_LOCK (g_signal_mutex);
1525 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1526 closure, func, data,
1527 g_signal_handler_block);
1528 G_UNLOCK (g_signal_mutex);
1535 g_signal_handlers_unblock_matched (gpointer instance,
1536 GSignalMatchType mask,
1543 guint n_handlers = 0;
1545 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1546 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1548 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1550 G_LOCK (g_signal_mutex);
1551 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1552 closure, func, data,
1553 g_signal_handler_unblock);
1554 G_UNLOCK (g_signal_mutex);
1561 g_signal_handlers_disconnect_matched (gpointer instance,
1562 GSignalMatchType mask,
1569 guint n_handlers = 0;
1571 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1572 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1574 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1576 G_LOCK (g_signal_mutex);
1577 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1578 closure, func, data,
1579 g_signal_handler_disconnect);
1580 G_UNLOCK (g_signal_mutex);
1587 g_signal_has_handler_pending (gpointer instance,
1590 gboolean may_be_blocked)
1592 HandlerMatch *mlist;
1593 gboolean has_pending;
1595 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1596 g_return_val_if_fail (signal_id > 0, FALSE);
1598 G_LOCK (g_signal_mutex);
1601 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1603 if (!(node->flags & G_SIGNAL_DETAILED))
1605 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1606 G_UNLOCK (g_signal_mutex);
1610 mlist = handlers_find (instance,
1611 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1612 signal_id, detail, NULL, NULL, NULL, TRUE);
1616 handler_match_free1_R (mlist, instance);
1619 has_pending = FALSE;
1620 G_UNLOCK (g_signal_mutex);
1626 g_signal_emitv (const GValue *instance_and_params,
1629 GValue *return_value)
1631 const GValue *param_values;
1636 g_return_if_fail (instance_and_params != NULL);
1637 instance = g_value_peek_pointer (instance_and_params);
1638 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1639 g_return_if_fail (signal_id > 0);
1641 param_values = instance_and_params + 1;
1643 G_LOCK (g_signal_mutex);
1644 node = LOOKUP_SIGNAL_NODE (signal_id);
1645 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1647 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1648 G_UNLOCK (g_signal_mutex);
1651 #ifdef G_ENABLE_DEBUG
1652 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1654 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1655 G_UNLOCK (g_signal_mutex);
1658 for (i = 0; i < node->n_params; i++)
1659 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1661 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1663 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1666 G_VALUE_TYPE_NAME (param_values + i));
1667 G_UNLOCK (g_signal_mutex);
1670 if (node->return_type != G_TYPE_NONE)
1674 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1676 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1678 G_UNLOCK (g_signal_mutex);
1681 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1683 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1685 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1687 G_VALUE_TYPE_NAME (return_value));
1688 G_UNLOCK (g_signal_mutex);
1693 return_value = NULL;
1694 #endif /* G_ENABLE_DEBUG */
1696 signal_emit_R (node, detail, instance, return_value, instance_and_params);
1697 G_UNLOCK (g_signal_mutex);
1701 g_signal_emit_valist (gpointer instance,
1706 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1707 GValue *param_values;
1711 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1712 g_return_if_fail (signal_id > 0);
1714 G_LOCK (g_signal_mutex);
1715 node = LOOKUP_SIGNAL_NODE (signal_id);
1716 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1718 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1719 G_UNLOCK (g_signal_mutex);
1722 #ifndef G_DISABLE_CHECKS
1723 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1725 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1726 G_UNLOCK (g_signal_mutex);
1729 #endif /* !G_DISABLE_CHECKS */
1731 if (node->n_params < MAX_STACK_VALUES)
1732 instance_and_params = stack_values;
1735 free_me = g_new (GValue, node->n_params + 1);
1736 instance_and_params = free_me;
1738 param_values = instance_and_params + 1;
1739 for (i = 0; i < node->n_params; i++)
1743 param_values[i].g_type = 0;
1744 g_value_init (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1745 G_VALUE_COLLECT (param_values + i,
1747 node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1751 g_warning ("%s: %s", G_STRLOC, error);
1754 /* we purposely leak the value here, it might not be
1755 * in a sane state if an error condition occoured
1758 g_value_unset (param_values + i);
1760 G_UNLOCK (g_signal_mutex);
1765 instance_and_params->g_type = 0;
1766 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1767 g_value_set_instance (instance_and_params, instance);
1768 if (node->return_type == G_TYPE_NONE)
1769 signal_emit_R (node, detail, instance, NULL, instance_and_params);
1772 GValue return_value = { 0, };
1773 gchar *error = NULL;
1775 g_value_init (&return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1776 if (signal_emit_R (node, detail, instance, &return_value, instance_and_params))
1777 G_VALUE_LCOPY (&return_value,
1779 node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1782 g_value_unset (&return_value);
1785 g_warning ("%s: %s", G_STRLOC, error);
1788 /* we purposely leak the value here, it might not be
1789 * in a sane state if an error condition occoured
1793 for (i = 0; i < node->n_params; i++)
1794 g_value_unset (param_values + i);
1795 g_value_unset (instance_and_params);
1798 G_UNLOCK (g_signal_mutex);
1802 g_signal_emit (gpointer instance,
1809 va_start (var_args, detail);
1810 g_signal_emit_valist (instance, signal_id, detail, var_args);
1815 g_signal_emit_by_name (gpointer instance,
1816 const gchar *detailed_signal,
1822 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1823 g_return_if_fail (detailed_signal != NULL);
1825 G_LOCK (g_signal_mutex);
1826 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1827 G_UNLOCK (g_signal_mutex);
1833 va_start (var_args, detailed_signal);
1834 g_signal_emit_valist (instance, signal_id, detail, var_args);
1838 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1841 static inline gboolean
1842 accumulate (GSignalInvocationHint *ihint,
1843 GValue *return_accu,
1844 GValue *handler_return,
1845 SignalAccumulator *accumulator)
1847 gboolean continue_emission;
1852 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1853 g_value_reset (handler_return);
1855 return continue_emission;
1859 signal_emit_R (SignalNode *node,
1862 GValue *emission_return,
1863 const GValue *instance_and_params)
1865 EmissionState emission_state = 0;
1866 SignalAccumulator *accumulator;
1867 GSignalInvocationHint ihint;
1868 GClosure *class_closure;
1870 Handler *handler_list = NULL;
1871 GValue *return_accu, accu = { 0, };
1872 guint signal_id = node->signal_id;
1873 gboolean return_value_altered = FALSE;
1875 #ifdef G_ENABLE_DEBUG
1876 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
1878 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
1879 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
1882 if (g_trap_instance_signals == instance)
1885 #endif /* G_ENABLE_DEBUG */
1887 if (node->flags & G_SIGNAL_NO_RECURSE)
1889 Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
1893 *emission->state_p = EMISSION_RESTART;
1894 return return_value_altered;
1897 ihint.signal_id = node->signal_id;
1898 ihint.detail = detail;
1899 accumulator = node->accumulator;
1902 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1903 return_accu = &accu;
1906 return_accu = emission_return;
1907 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
1908 signal_id, detail, instance, &emission_state);
1909 class_closure = node->class_closure;
1914 handler_unref_R (signal_id, instance, handler_list);
1915 hlist = handler_list_lookup (signal_id, instance);
1916 handler_list = hlist ? hlist->handlers : NULL;
1918 handler_ref (handler_list);
1920 ihint.run_type = G_SIGNAL_RUN_FIRST;
1922 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
1924 emission_state = EMISSION_RUN;
1926 G_UNLOCK (g_signal_mutex);
1927 g_closure_invoke (class_closure,
1930 instance_and_params,
1932 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
1933 emission_state == EMISSION_RUN)
1934 emission_state = EMISSION_STOP;
1935 G_LOCK (g_signal_mutex);
1936 return_value_altered = TRUE;
1938 if (emission_state == EMISSION_STOP)
1940 else if (emission_state == EMISSION_RESTART)
1944 if (node->emission_hooks)
1946 gboolean need_destroy, was_in_call, may_recurse = TRUE;
1949 emission_state = EMISSION_HOOK;
1950 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
1953 SignalHook *signal_hook = SIGNAL_HOOK (hook);
1955 if (!signal_hook->detail || signal_hook->detail == detail)
1957 GSignalEmissionHook hook_func = hook->func;
1959 was_in_call = G_HOOK_IN_CALL (hook);
1960 hook->flags |= G_HOOK_FLAG_IN_CALL;
1961 G_UNLOCK (g_signal_mutex);
1962 need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
1963 G_LOCK (g_signal_mutex);
1965 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
1967 g_hook_destroy_link (node->emission_hooks, hook);
1969 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
1972 if (emission_state == EMISSION_RESTART)
1978 Handler *handler = handler_list;
1980 emission_state = EMISSION_RUN;
1981 handler_ref (handler);
1988 handler_unref_R (signal_id, instance, handler_list);
1989 handler_list = handler;
1992 else if (!handler->block_count && (!handler->detail || handler->detail == detail))
1994 G_UNLOCK (g_signal_mutex);
1995 g_closure_invoke (handler->closure,
1998 instance_and_params,
2000 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2001 emission_state == EMISSION_RUN)
2002 emission_state = EMISSION_STOP;
2003 G_LOCK (g_signal_mutex);
2004 return_value_altered = TRUE;
2006 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2009 tmp = handler->next;
2013 handler_unref_R (signal_id, instance, handler_list);
2014 handler_list = handler;
2019 if (emission_state == EMISSION_STOP)
2021 else if (emission_state == EMISSION_RESTART)
2025 ihint.run_type = G_SIGNAL_RUN_LAST;
2027 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2029 emission_state = EMISSION_RUN;
2031 G_UNLOCK (g_signal_mutex);
2032 g_closure_invoke (class_closure,
2035 instance_and_params,
2037 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2038 emission_state == EMISSION_RUN)
2039 emission_state = EMISSION_STOP;
2040 G_LOCK (g_signal_mutex);
2041 return_value_altered = TRUE;
2043 if (emission_state == EMISSION_STOP)
2045 else if (emission_state == EMISSION_RESTART)
2051 Handler *handler = handler_list;
2053 emission_state = EMISSION_RUN;
2054 handler_ref (handler);
2059 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail))
2061 G_UNLOCK (g_signal_mutex);
2062 g_closure_invoke (handler->closure,
2065 instance_and_params,
2067 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2068 emission_state == EMISSION_RUN)
2069 emission_state = EMISSION_STOP;
2070 G_LOCK (g_signal_mutex);
2071 return_value_altered = TRUE;
2073 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2076 tmp = handler->next;
2080 handler_unref_R (signal_id, instance, handler);
2085 if (emission_state == EMISSION_STOP)
2087 else if (emission_state == EMISSION_RESTART)
2093 ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2095 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2097 gboolean need_unset = FALSE;
2099 emission_state = EMISSION_STOP;
2101 G_UNLOCK (g_signal_mutex);
2102 if (node->return_type != G_TYPE_NONE && !accumulator)
2104 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2107 g_closure_invoke (class_closure,
2108 node->return_type != G_TYPE_NONE ? &accu : NULL,
2110 instance_and_params,
2113 g_value_unset (&accu);
2114 G_LOCK (g_signal_mutex);
2116 if (emission_state == EMISSION_RESTART)
2121 handler_unref_R (signal_id, instance, handler_list);
2123 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2125 g_value_unset (&accu);
2127 return return_value_altered;
2131 /* --- compile standard marshallers --- */
2132 #include "gobject.h"
2134 #include "gmarshal.c"