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);
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)
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));
1119 G_UNLOCK (g_signal_mutex);
1123 /* setup permanent portion of signal node */
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;
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);
1143 node->destroyed = FALSE;
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;
1152 g_closure_sink (class_closure);
1155 node->accumulator = g_new (SignalAccumulator, 1);
1156 node->accumulator->func = accumulator;
1157 node->accumulator->data = accu_data;
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);
1170 signal_destroy_R (SignalNode *signal_node)
1172 SignalNode node = *signal_node;
1174 signal_node->destroyed = TRUE;
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;
1185 #ifdef G_ENABLE_DEBUG
1186 /* check current emissions */
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);
1198 /* free contents that need to
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)
1206 g_hook_list_clear (node.emission_hooks);
1207 g_free (node.emission_hooks);
1209 G_LOCK (g_signal_mutex);
1213 g_signal_connect_closure_by_id (gpointer instance,
1220 guint handler_id = 0;
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);
1226 G_LOCK (g_signal_mutex);
1227 node = LOOKUP_SIGNAL_NODE (signal_id);
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);
1236 Handler *handler = handler_new (after);
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);
1248 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1249 G_UNLOCK (g_signal_mutex);
1255 g_signal_connect_closure (gpointer instance,
1256 const gchar *detailed_signal,
1260 guint signal_id, handler_id = 0;
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);
1268 G_LOCK (g_signal_mutex);
1269 itype = G_TYPE_FROM_INSTANCE (instance);
1270 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1273 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
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);
1281 Handler *handler = handler_new (after);
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);
1293 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1294 G_UNLOCK (g_signal_mutex);
1300 g_signal_connect_data (gpointer instance,
1301 const gchar *detailed_signal,
1302 GCallback c_handler,
1304 GClosureNotify destroy_data,
1308 guint signal_id, handler_id = 0;
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);
1316 G_LOCK (g_signal_mutex);
1317 itype = G_TYPE_FROM_INSTANCE (instance);
1318 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1321 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
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);
1329 Handler *handler = handler_new (after);
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);
1341 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1342 G_UNLOCK (g_signal_mutex);
1348 g_signal_handler_block (gpointer instance,
1353 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1354 g_return_if_fail (handler_id > 0);
1356 G_LOCK (g_signal_mutex);
1357 handler = handler_lookup (instance, handler_id, NULL);
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);
1365 handler->block_count += 1;
1368 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1369 G_UNLOCK (g_signal_mutex);
1373 g_signal_handler_unblock (gpointer instance,
1378 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1379 g_return_if_fail (handler_id > 0);
1381 G_LOCK (g_signal_mutex);
1382 handler = handler_lookup (instance, handler_id, NULL);
1385 if (handler->block_count)
1386 handler->block_count -= 1;
1388 g_warning (G_STRLOC ": handler `%u' of instance `%p' is not blocked", handler_id, instance);
1391 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1392 G_UNLOCK (g_signal_mutex);
1396 g_signal_handler_disconnect (gpointer instance,
1402 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1403 g_return_if_fail (handler_id > 0);
1405 G_LOCK (g_signal_mutex);
1406 handler = handler_lookup (instance, handler_id, &signal_id);
1410 handler->block_count = 1;
1411 handler_unref_R (signal_id, instance, handler);
1414 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1415 G_UNLOCK (g_signal_mutex);
1419 g_signal_handlers_destroy (gpointer instance)
1421 GBSearchArray *hlbsa;
1423 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1425 G_LOCK (g_signal_mutex);
1426 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1431 /* reentrancy caution, delete instance trace first */
1432 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1434 for (i = 0; i < hlbsa->n_nodes; i++)
1436 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1437 Handler *handler = hlist->handlers;
1441 Handler *tmp = handler;
1443 handler = tmp->next;
1444 tmp->block_count = 1;
1445 /* cruel unlink, this works because _all_ handlers vanish */
1451 handler_unref_R (0, NULL, tmp);
1455 g_bsearch_array_destroy (hlbsa);
1457 G_UNLOCK (g_signal_mutex);
1461 g_signal_handler_find (gpointer instance,
1462 GSignalMatchType mask,
1469 guint handler_id = 0;
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);
1474 if (mask & G_SIGNAL_MATCH_MASK)
1476 HandlerMatch *mlist;
1478 G_LOCK (g_signal_mutex);
1479 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1482 handler_id = mlist->handler->id;
1483 handler_match_free1_R (mlist, instance);
1485 G_UNLOCK (g_signal_mutex);
1492 signal_handlers_foreach_matched_R (gpointer instance,
1493 GSignalMatchType mask,
1499 void (*callback) (gpointer instance,
1502 HandlerMatch *mlist;
1503 guint n_handlers = 0;
1505 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
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);
1519 g_signal_handlers_block_matched (gpointer instance,
1520 GSignalMatchType mask,
1527 guint n_handlers = 0;
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);
1532 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
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);
1545 g_signal_handlers_unblock_matched (gpointer instance,
1546 GSignalMatchType mask,
1553 guint n_handlers = 0;
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);
1558 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
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);
1571 g_signal_handlers_disconnect_matched (gpointer instance,
1572 GSignalMatchType mask,
1579 guint n_handlers = 0;
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);
1584 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
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);
1597 g_signal_has_handler_pending (gpointer instance,
1600 gboolean may_be_blocked)
1602 HandlerMatch *mlist;
1603 gboolean has_pending;
1605 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1606 g_return_val_if_fail (signal_id > 0, FALSE);
1608 G_LOCK (g_signal_mutex);
1611 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1613 if (!(node->flags & G_SIGNAL_DETAILED))
1615 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1616 G_UNLOCK (g_signal_mutex);
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);
1626 handler_match_free1_R (mlist, instance);
1629 has_pending = FALSE;
1630 G_UNLOCK (g_signal_mutex);
1636 g_signal_emitv (const GValue *instance_and_params,
1639 GValue *return_value)
1641 const GValue *param_values;
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);
1651 param_values = instance_and_params + 1;
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))
1657 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1658 G_UNLOCK (g_signal_mutex);
1661 #ifdef G_ENABLE_DEBUG
1662 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1664 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1665 G_UNLOCK (g_signal_mutex);
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))
1671 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1673 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1676 G_VALUE_TYPE_NAME (param_values + i));
1677 G_UNLOCK (g_signal_mutex);
1680 if (node->return_type != G_TYPE_NONE)
1684 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1686 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1688 G_UNLOCK (g_signal_mutex);
1691 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1693 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1695 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1697 G_VALUE_TYPE_NAME (return_value));
1698 G_UNLOCK (g_signal_mutex);
1703 return_value = NULL;
1704 #endif /* G_ENABLE_DEBUG */
1706 signal_emit_R (node, detail, instance, return_value, instance_and_params);
1707 G_UNLOCK (g_signal_mutex);
1711 g_signal_emit_valist (gpointer instance,
1716 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1717 GValue *param_values;
1721 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1722 g_return_if_fail (signal_id > 0);
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))
1728 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1729 G_UNLOCK (g_signal_mutex);
1732 #ifndef G_DISABLE_CHECKS
1733 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1735 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1736 G_UNLOCK (g_signal_mutex);
1739 #endif /* !G_DISABLE_CHECKS */
1741 if (node->n_params < MAX_STACK_VALUES)
1742 instance_and_params = stack_values;
1745 free_me = g_new (GValue, node->n_params + 1);
1746 instance_and_params = free_me;
1748 param_values = instance_and_params + 1;
1749 for (i = 0; i < node->n_params; i++)
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,
1757 node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1761 g_warning ("%s: %s", G_STRLOC, error);
1764 /* we purposely leak the value here, it might not be
1765 * in a sane state if an error condition occoured
1768 g_value_unset (param_values + i);
1770 G_UNLOCK (g_signal_mutex);
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);
1782 GValue return_value = { 0, };
1783 gchar *error = NULL;
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,
1789 node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1792 g_value_unset (&return_value);
1795 g_warning ("%s: %s", G_STRLOC, error);
1798 /* we purposely leak the value here, it might not be
1799 * in a sane state if an error condition occoured
1803 for (i = 0; i < node->n_params; i++)
1804 g_value_unset (param_values + i);
1805 g_value_unset (instance_and_params);
1808 G_UNLOCK (g_signal_mutex);
1812 g_signal_emit (gpointer instance,
1819 va_start (var_args, detail);
1820 g_signal_emit_valist (instance, signal_id, detail, var_args);
1825 g_signal_emit_by_name (gpointer instance,
1826 const gchar *detailed_signal,
1832 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1833 g_return_if_fail (detailed_signal != NULL);
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);
1843 va_start (var_args, detailed_signal);
1844 g_signal_emit_valist (instance, signal_id, detail, var_args);
1848 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1851 static inline gboolean
1852 accumulate (GSignalInvocationHint *ihint,
1853 GValue *return_accu,
1854 GValue *handler_return,
1855 SignalAccumulator *accumulator)
1857 gboolean continue_emission;
1862 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1863 g_value_reset (handler_return);
1865 return continue_emission;
1869 signal_emit_R (SignalNode *node,
1872 GValue *emission_return,
1873 const GValue *instance_and_params)
1875 EmissionState emission_state = 0;
1876 SignalAccumulator *accumulator;
1877 GSignalInvocationHint ihint;
1878 GClosure *class_closure;
1880 Handler *handler_list = NULL;
1881 GValue *return_accu, accu = { 0, };
1882 guint signal_id = node->signal_id;
1883 gboolean return_value_altered = FALSE;
1885 #ifdef G_ENABLE_DEBUG
1886 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
1888 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
1889 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
1892 if (g_trap_instance_signals == instance)
1895 #endif /* G_ENABLE_DEBUG */
1897 if (node->flags & G_SIGNAL_NO_RECURSE)
1899 Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
1903 *emission->state_p = EMISSION_RESTART;
1904 return return_value_altered;
1907 ihint.signal_id = node->signal_id;
1908 ihint.detail = detail;
1909 accumulator = node->accumulator;
1912 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1913 return_accu = &accu;
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;
1924 handler_unref_R (signal_id, instance, handler_list);
1925 hlist = handler_list_lookup (signal_id, instance);
1926 handler_list = hlist ? hlist->handlers : NULL;
1928 handler_ref (handler_list);
1930 ihint.run_type = G_SIGNAL_RUN_FIRST;
1932 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
1934 emission_state = EMISSION_RUN;
1936 G_UNLOCK (g_signal_mutex);
1937 g_closure_invoke (class_closure,
1940 instance_and_params,
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;
1948 if (emission_state == EMISSION_STOP)
1950 else if (emission_state == EMISSION_RESTART)
1954 if (node->emission_hooks)
1956 gboolean need_destroy, was_in_call, may_recurse = TRUE;
1959 emission_state = EMISSION_HOOK;
1960 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
1963 SignalHook *signal_hook = SIGNAL_HOOK (hook);
1965 if (!signal_hook->detail || signal_hook->detail == detail)
1967 GSignalEmissionHook hook_func = hook->func;
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);
1975 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
1977 g_hook_destroy_link (node->emission_hooks, hook);
1979 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
1982 if (emission_state == EMISSION_RESTART)
1988 Handler *handler = handler_list;
1990 emission_state = EMISSION_RUN;
1991 handler_ref (handler);
1998 handler_unref_R (signal_id, instance, handler_list);
1999 handler_list = handler;
2002 else if (!handler->block_count && (!handler->detail || handler->detail == detail))
2004 G_UNLOCK (g_signal_mutex);
2005 g_closure_invoke (handler->closure,
2008 instance_and_params,
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;
2016 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2019 tmp = handler->next;
2023 handler_unref_R (signal_id, instance, handler_list);
2024 handler_list = handler;
2029 if (emission_state == EMISSION_STOP)
2031 else if (emission_state == EMISSION_RESTART)
2035 ihint.run_type = G_SIGNAL_RUN_LAST;
2037 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2039 emission_state = EMISSION_RUN;
2041 G_UNLOCK (g_signal_mutex);
2042 g_closure_invoke (class_closure,
2045 instance_and_params,
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;
2053 if (emission_state == EMISSION_STOP)
2055 else if (emission_state == EMISSION_RESTART)
2061 Handler *handler = handler_list;
2063 emission_state = EMISSION_RUN;
2064 handler_ref (handler);
2069 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail))
2071 G_UNLOCK (g_signal_mutex);
2072 g_closure_invoke (handler->closure,
2075 instance_and_params,
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;
2083 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2086 tmp = handler->next;
2090 handler_unref_R (signal_id, instance, handler);
2095 if (emission_state == EMISSION_STOP)
2097 else if (emission_state == EMISSION_RESTART)
2103 ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2105 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2107 gboolean need_unset = FALSE;
2109 emission_state = EMISSION_STOP;
2111 G_UNLOCK (g_signal_mutex);
2112 if (node->return_type != G_TYPE_NONE && !accumulator)
2114 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2117 g_closure_invoke (class_closure,
2118 node->return_type != G_TYPE_NONE ? &accu : NULL,
2120 instance_and_params,
2123 g_value_unset (&accu);
2124 G_LOCK (g_signal_mutex);
2126 if (emission_state == EMISSION_RESTART)
2131 handler_unref_R (signal_id, instance, handler_list);
2133 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2135 g_value_unset (&accu);
2137 return return_value_altered;
2141 /* --- compile standard marshallers --- */
2142 #include "gobject.h"
2144 #include "gmarshal.c"