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_unlocked_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;
207 gulong sequential_number;
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 static gulong g_handler_sequential_number = 1;
239 G_LOCK_DEFINE_STATIC (g_signal_mutex);
242 /* --- signal nodes --- */
243 static guint g_n_signal_nodes = 0;
244 static SignalNode **g_signal_nodes = NULL;
246 static inline SignalNode*
247 LOOKUP_SIGNAL_NODE (register guint signal_id)
249 if (signal_id < g_n_signal_nodes)
250 return g_signal_nodes[signal_id];
256 /* --- functions --- */
258 signal_id_lookup (GQuark quark,
261 GType *ifaces, type = itype;
267 /* try looking up signals for this type and its anchestors */
270 SignalKey *signal_key;
273 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
276 return signal_key->signal_id;
278 type = g_type_parent (type);
282 /* no luck, try interfaces it exports */
283 ifaces = g_type_interfaces (itype, &n_ifaces);
286 SignalKey *signal_key;
288 key.itype = ifaces[n_ifaces];
289 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
294 return signal_key->signal_id;
303 handler_lists_cmp (gconstpointer node1,
306 const HandlerList *hlist1 = node1, *hlist2 = node2;
308 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
311 static inline HandlerList*
312 handler_list_ensure (guint signal_id,
315 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
320 hlbsa = g_bsearch_array_new (sizeof (HandlerList), handler_lists_cmp, G_BSEARCH_ARRAY_DEFER_SHRINK);
321 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
323 key.signal_id = signal_id;
326 return g_bsearch_array_insert (hlbsa, &key, FALSE);
329 static inline HandlerList*
330 handler_list_lookup (guint signal_id,
333 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
336 key.signal_id = signal_id;
338 return hlbsa ? g_bsearch_array_lookup (hlbsa, &key) : NULL;
342 handler_lookup (gpointer instance,
346 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
352 for (i = 0; i < hlbsa->n_nodes; i++)
354 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
357 for (handler = hlist->handlers; handler; handler = handler->next)
358 if (handler->sequential_number == handler_id)
361 *signal_id_p = hlist->signal_id;
371 static inline HandlerMatch*
372 handler_match_prepend (HandlerMatch *list,
378 /* yeah, we could use our own memchunk here, introducing yet more
379 * rarely used cached nodes and extra allocation overhead.
380 * instead, we use GList* nodes, since they are exactly the size
381 * we need and are already cached. g_signal_init() asserts this.
383 node = (HandlerMatch*) g_list_alloc ();
384 node->handler = handler;
386 node->d.signal_id = signal_id;
387 handler_ref (handler);
391 static inline HandlerMatch*
392 handler_match_free1_R (HandlerMatch *node,
395 HandlerMatch *next = node->next;
397 handler_unref_R (node->d.signal_id, instance, node->handler);
398 g_list_free_1 ((GList*) node);
404 handlers_find (gpointer instance,
405 GSignalMatchType mask,
411 gboolean one_and_only)
413 HandlerMatch *mlist = NULL;
415 if (mask & G_SIGNAL_MATCH_ID)
417 HandlerList *hlist = handler_list_lookup (signal_id, instance);
419 SignalNode *node = NULL;
421 if (mask & G_SIGNAL_MATCH_FUNC)
423 node = LOOKUP_SIGNAL_NODE (signal_id);
424 if (!node || !node->c_marshaller)
429 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
430 if (handler->sequential_number &&
431 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
432 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
433 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
434 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
435 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
436 handler->closure->meta_marshal == 0 &&
437 ((GCClosure*) handler->closure)->callback == func)))
439 mlist = handler_match_prepend (mlist, handler, signal_id);
446 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
453 for (i = 0; i < hlbsa->n_nodes; i++)
455 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
456 SignalNode *node = NULL;
459 if (!(mask & G_SIGNAL_MATCH_FUNC))
461 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
462 if (!node->c_marshaller)
466 for (handler = hlist->handlers; handler; handler = handler->next)
467 if (handler->sequential_number &&
468 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
469 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
470 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
471 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
472 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
473 handler->closure->meta_marshal == 0 &&
474 ((GCClosure*) handler->closure)->callback == func)))
476 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
487 static inline Handler*
488 handler_new (gboolean after)
490 Handler *handler = g_generic_node_alloc (&g_handler_ts,
493 #ifndef G_DISABLE_CHECKS
494 if (g_handler_sequential_number < 1)
495 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
498 handler->sequential_number = g_handler_sequential_number++;
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 gulong 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 (%lu) 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_stop_emission_by_name (gpointer instance,
889 const gchar *detailed_signal)
895 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
896 g_return_if_fail (detailed_signal != NULL);
898 G_LOCK (g_signal_mutex);
899 itype = G_TYPE_FROM_INSTANCE (instance);
900 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
903 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
905 if (detail && !(node->flags & G_SIGNAL_DETAILED))
906 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
907 else if (!g_type_is_a (itype, node->itype))
908 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
911 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
912 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
916 if (*emission->state_p == EMISSION_HOOK)
917 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
918 node->name, instance);
919 else if (*emission->state_p == EMISSION_RUN)
920 *emission->state_p = EMISSION_STOP;
923 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
924 node->name, instance);
928 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
929 G_UNLOCK (g_signal_mutex);
933 g_signal_lookup (const gchar *name,
938 g_return_val_if_fail (name != NULL, 0);
939 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
941 G_LOCK (g_signal_mutex);
942 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
943 G_UNLOCK (g_signal_mutex);
948 G_CONST_RETURN gchar*
949 g_signal_name (guint signal_id)
954 G_LOCK (g_signal_mutex);
955 node = LOOKUP_SIGNAL_NODE (signal_id);
956 name = node ? node->name : NULL;
957 G_UNLOCK (g_signal_mutex);
963 g_signal_query (guint signal_id,
968 g_return_if_fail (query != NULL);
970 G_LOCK (g_signal_mutex);
971 node = LOOKUP_SIGNAL_NODE (signal_id);
972 if (!node || node->destroyed)
973 query->signal_id = 0;
976 query->signal_id = node->signal_id;
977 query->signal_name = node->name;
978 query->itype = node->itype;
979 query->signal_flags = node->flags;
980 query->return_type = node->return_type;
981 query->n_params = node->n_params;
982 query->param_types = node->param_types;
984 G_UNLOCK (g_signal_mutex);
988 g_signal_list_ids (GType itype,
996 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
997 g_return_val_if_fail (n_ids != NULL, NULL);
999 G_LOCK (g_signal_mutex);
1001 keys = g_signal_key_bsa.nodes;
1002 n_nodes = g_signal_key_bsa.n_nodes;
1003 result = g_array_new (FALSE, FALSE, sizeof (guint));
1005 for (i = 0; i < n_nodes; i++)
1006 if (keys[i].itype == itype)
1008 const gchar *name = g_quark_to_string (keys[i].quark);
1010 /* Signal names with "_" in them are aliases to the same
1011 * name with "-" instead of "_".
1013 if (!strchr (name, '_'))
1014 g_array_append_val (result, keys[i].signal_id);
1017 *n_ids = result->len;
1019 G_UNLOCK (g_signal_mutex);
1021 return (guint*) g_array_free (result, FALSE);
1025 g_signal_new_valist (const gchar *signal_name,
1027 GSignalFlags signal_flags,
1028 GClosure *class_closure,
1029 GSignalAccumulator accumulator,
1031 GSignalCMarshaller c_marshaller,
1042 param_types = g_new (GType, n_params);
1044 for (i = 0; i < n_params; i++)
1045 param_types[i] = va_arg (args, GType);
1050 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1051 class_closure, accumulator, accu_data, c_marshaller,
1052 return_type, n_params, param_types);
1053 g_free (param_types);
1059 g_signal_newc (const gchar *signal_name,
1061 GSignalFlags signal_flags,
1063 GSignalAccumulator accumulator,
1065 GSignalCMarshaller c_marshaller,
1073 g_return_val_if_fail (signal_name != NULL, 0);
1075 va_start (args, n_params);
1077 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1078 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1079 accumulator, accu_data, c_marshaller,
1080 return_type, n_params, args);
1088 g_signal_newv (const gchar *signal_name,
1090 GSignalFlags signal_flags,
1091 GClosure *class_closure,
1092 GSignalAccumulator accumulator,
1094 GSignalCMarshaller c_marshaller,
1103 g_return_val_if_fail (signal_name != NULL, 0);
1104 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1106 g_return_val_if_fail (param_types != NULL, 0);
1107 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1108 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1109 g_return_val_if_fail (accumulator == NULL, 0);
1111 g_return_val_if_fail (accu_data == NULL, 0);
1113 name = g_strdup (signal_name);
1114 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1116 G_LOCK (g_signal_mutex);
1118 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1119 node = LOOKUP_SIGNAL_NODE (signal_id);
1120 if (node && !node->destroyed)
1122 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1124 g_type_name (node->itype),
1125 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1127 G_UNLOCK (g_signal_mutex);
1130 if (node && node->itype != itype)
1132 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1134 g_type_name (itype),
1135 g_type_name (node->itype));
1137 G_UNLOCK (g_signal_mutex);
1140 for (i = 0; i < n_params; i++)
1141 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1143 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1144 i + 1, g_type_name (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1146 G_UNLOCK (g_signal_mutex);
1149 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1151 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1152 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1154 G_UNLOCK (g_signal_mutex);
1157 if (return_type != G_TYPE_NONE &&
1158 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1160 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1161 g_type_name (itype), name,
1162 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE));
1164 G_UNLOCK (g_signal_mutex);
1168 /* setup permanent portion of signal node */
1173 signal_id = g_n_signal_nodes++;
1174 node = g_new (SignalNode, 1);
1175 node->signal_id = signal_id;
1176 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1177 g_signal_nodes[signal_id] = node;
1178 node->itype = itype;
1181 key.quark = g_quark_from_string (node->name);
1182 key.signal_id = signal_id;
1183 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1184 g_strdelimit (node->name, "_", '-');
1185 key.quark = g_quark_from_static_string (node->name);
1186 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1188 node->destroyed = FALSE;
1190 /* setup reinitializable portion */
1191 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1192 node->n_params = n_params;
1193 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1194 node->return_type = return_type;
1195 node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
1197 g_closure_sink (class_closure);
1200 node->accumulator = g_new (SignalAccumulator, 1);
1201 node->accumulator->func = accumulator;
1202 node->accumulator->data = accu_data;
1205 node->accumulator = NULL;
1206 node->c_marshaller = c_marshaller;
1207 node->emission_hooks = NULL;
1208 if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
1209 g_closure_set_marshal (class_closure, node->c_marshaller);
1210 G_UNLOCK (g_signal_mutex);
1215 signal_destroy_R (SignalNode *signal_node)
1217 SignalNode node = *signal_node;
1219 signal_node->destroyed = TRUE;
1221 /* reentrancy caution, zero out real contents first */
1222 signal_node->n_params = 0;
1223 signal_node->param_types = NULL;
1224 signal_node->return_type = 0;
1225 signal_node->class_closure = NULL;
1226 signal_node->accumulator = NULL;
1227 signal_node->c_marshaller = NULL;
1228 signal_node->emission_hooks = NULL;
1230 #ifdef G_ENABLE_DEBUG
1231 /* check current emissions */
1235 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1236 emission; emission = emission->next)
1237 if (emission->signal_id == node.signal_id)
1238 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1239 node.name, emission->instance);
1243 /* free contents that need to
1245 G_UNLOCK (g_signal_mutex);
1246 g_free (node.param_types);
1247 g_closure_unref (node.class_closure);
1248 g_free (node.accumulator);
1249 if (node.emission_hooks)
1251 g_hook_list_clear (node.emission_hooks);
1252 g_free (node.emission_hooks);
1254 G_LOCK (g_signal_mutex);
1258 g_signal_connect_closure_by_id (gpointer instance,
1265 gulong handler_seq_no = 0;
1267 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1268 g_return_val_if_fail (signal_id > 0, 0);
1269 g_return_val_if_fail (closure != NULL, 0);
1271 G_LOCK (g_signal_mutex);
1272 node = LOOKUP_SIGNAL_NODE (signal_id);
1275 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1276 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1277 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1278 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1281 Handler *handler = handler_new (after);
1283 handler_seq_no = handler->sequential_number;
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 (closure))
1289 g_closure_set_marshal (closure, node->c_marshaller);
1293 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1294 G_UNLOCK (g_signal_mutex);
1296 return handler_seq_no;
1300 g_signal_connect_closure (gpointer instance,
1301 const gchar *detailed_signal,
1306 gulong handler_seq_no = 0;
1310 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1311 g_return_val_if_fail (detailed_signal != NULL, 0);
1312 g_return_val_if_fail (closure != NULL, 0);
1314 G_LOCK (g_signal_mutex);
1315 itype = G_TYPE_FROM_INSTANCE (instance);
1316 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1319 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1321 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1322 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1323 else if (!g_type_is_a (itype, node->itype))
1324 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1327 Handler *handler = handler_new (after);
1329 handler_seq_no = handler->sequential_number;
1330 handler->detail = detail;
1331 handler->closure = g_closure_ref (closure);
1332 g_closure_sink (closure);
1333 handler_insert (signal_id, instance, handler);
1334 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1335 g_closure_set_marshal (handler->closure, node->c_marshaller);
1339 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1340 G_UNLOCK (g_signal_mutex);
1342 return handler_seq_no;
1346 g_signal_connect_data (gpointer instance,
1347 const gchar *detailed_signal,
1348 GCallback c_handler,
1350 GClosureNotify destroy_data,
1355 gulong handler_seq_no = 0;
1359 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1360 g_return_val_if_fail (detailed_signal != NULL, 0);
1361 g_return_val_if_fail (c_handler != NULL, 0);
1363 G_LOCK (g_signal_mutex);
1364 itype = G_TYPE_FROM_INSTANCE (instance);
1365 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1368 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1370 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1371 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1372 else if (!g_type_is_a (itype, node->itype))
1373 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1376 Handler *handler = handler_new (after);
1378 handler_seq_no = handler->sequential_number;
1379 handler->detail = detail;
1380 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1381 g_closure_sink (handler->closure);
1382 handler_insert (signal_id, instance, handler);
1383 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1384 g_closure_set_marshal (handler->closure, node->c_marshaller);
1388 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1389 G_UNLOCK (g_signal_mutex);
1391 return handler_seq_no;
1395 g_signal_handler_block (gpointer instance,
1400 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1401 g_return_if_fail (handler_id > 0);
1403 G_LOCK (g_signal_mutex);
1404 handler = handler_lookup (instance, handler_id, NULL);
1407 #ifndef G_DISABLE_CHECKS
1408 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1409 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1411 handler->block_count += 1;
1414 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1415 G_UNLOCK (g_signal_mutex);
1419 g_signal_handler_unblock (gpointer instance,
1424 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1425 g_return_if_fail (handler_id > 0);
1427 G_LOCK (g_signal_mutex);
1428 handler = handler_lookup (instance, handler_id, NULL);
1431 if (handler->block_count)
1432 handler->block_count -= 1;
1434 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1437 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1438 G_UNLOCK (g_signal_mutex);
1442 g_signal_handler_disconnect (gpointer instance,
1448 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1449 g_return_if_fail (handler_id > 0);
1451 G_LOCK (g_signal_mutex);
1452 handler = handler_lookup (instance, handler_id, &signal_id);
1455 handler->sequential_number = 0;
1456 handler->block_count = 1;
1457 handler_unref_R (signal_id, instance, handler);
1460 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1461 G_UNLOCK (g_signal_mutex);
1465 g_signal_handler_is_connected (gpointer instance,
1471 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1472 g_return_val_if_fail (handler_id > 0, FALSE);
1474 G_LOCK (g_signal_mutex);
1475 handler = handler_lookup (instance, handler_id, NULL);
1476 connected = handler != NULL;
1477 G_UNLOCK (g_signal_mutex);
1483 g_signal_handlers_destroy (gpointer instance)
1485 GBSearchArray *hlbsa;
1487 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1489 G_LOCK (g_signal_mutex);
1490 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1495 /* reentrancy caution, delete instance trace first */
1496 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1498 for (i = 0; i < hlbsa->n_nodes; i++)
1500 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1501 Handler *handler = hlist->handlers;
1505 Handler *tmp = handler;
1507 handler = tmp->next;
1508 tmp->block_count = 1;
1509 /* cruel unlink, this works because _all_ handlers vanish */
1512 if (tmp->sequential_number)
1514 tmp->sequential_number = 0;
1515 handler_unref_R (0, NULL, tmp);
1519 g_bsearch_array_destroy (hlbsa);
1521 G_UNLOCK (g_signal_mutex);
1525 g_signal_handler_find (gpointer instance,
1526 GSignalMatchType mask,
1533 gulong handler_seq_no = 0;
1535 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1536 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1538 if (mask & G_SIGNAL_MATCH_MASK)
1540 HandlerMatch *mlist;
1542 G_LOCK (g_signal_mutex);
1543 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1546 handler_seq_no = mlist->handler->sequential_number;
1547 handler_match_free1_R (mlist, instance);
1549 G_UNLOCK (g_signal_mutex);
1552 return handler_seq_no;
1556 signal_handlers_foreach_matched_R (gpointer instance,
1557 GSignalMatchType mask,
1563 void (*callback) (gpointer instance,
1564 gulong handler_seq_no))
1566 HandlerMatch *mlist;
1567 guint n_handlers = 0;
1569 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1573 if (mlist->handler->sequential_number)
1575 G_UNLOCK (g_signal_mutex);
1576 callback (instance, mlist->handler->sequential_number);
1577 G_LOCK (g_signal_mutex);
1579 mlist = handler_match_free1_R (mlist, instance);
1586 g_signal_handlers_block_matched (gpointer instance,
1587 GSignalMatchType mask,
1594 guint n_handlers = 0;
1596 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1597 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1599 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1601 G_LOCK (g_signal_mutex);
1602 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1603 closure, func, data,
1604 g_signal_handler_block);
1605 G_UNLOCK (g_signal_mutex);
1612 g_signal_handlers_unblock_matched (gpointer instance,
1613 GSignalMatchType mask,
1620 guint n_handlers = 0;
1622 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1623 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1625 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1627 G_LOCK (g_signal_mutex);
1628 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1629 closure, func, data,
1630 g_signal_handler_unblock);
1631 G_UNLOCK (g_signal_mutex);
1638 g_signal_handlers_disconnect_matched (gpointer instance,
1639 GSignalMatchType mask,
1646 guint n_handlers = 0;
1648 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1649 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1651 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1653 G_LOCK (g_signal_mutex);
1654 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1655 closure, func, data,
1656 g_signal_handler_disconnect);
1657 G_UNLOCK (g_signal_mutex);
1664 g_signal_has_handler_pending (gpointer instance,
1667 gboolean may_be_blocked)
1669 HandlerMatch *mlist;
1670 gboolean has_pending;
1672 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1673 g_return_val_if_fail (signal_id > 0, FALSE);
1675 G_LOCK (g_signal_mutex);
1678 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1680 if (!(node->flags & G_SIGNAL_DETAILED))
1682 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1683 G_UNLOCK (g_signal_mutex);
1687 mlist = handlers_find (instance,
1688 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1689 signal_id, detail, NULL, NULL, NULL, TRUE);
1693 handler_match_free1_R (mlist, instance);
1696 has_pending = FALSE;
1697 G_UNLOCK (g_signal_mutex);
1703 g_signal_emitv (const GValue *instance_and_params,
1706 GValue *return_value)
1708 const GValue *param_values;
1713 g_return_if_fail (instance_and_params != NULL);
1714 instance = g_value_peek_pointer (instance_and_params);
1715 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1716 g_return_if_fail (signal_id > 0);
1718 param_values = instance_and_params + 1;
1720 G_LOCK (g_signal_mutex);
1721 node = LOOKUP_SIGNAL_NODE (signal_id);
1722 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1724 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1725 G_UNLOCK (g_signal_mutex);
1728 #ifdef G_ENABLE_DEBUG
1729 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1731 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1732 G_UNLOCK (g_signal_mutex);
1735 for (i = 0; i < node->n_params; i++)
1736 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1738 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1740 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1743 G_VALUE_TYPE_NAME (param_values + i));
1744 G_UNLOCK (g_signal_mutex);
1747 if (node->return_type != G_TYPE_NONE)
1751 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1753 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1755 G_UNLOCK (g_signal_mutex);
1758 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1760 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1762 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1764 G_VALUE_TYPE_NAME (return_value));
1765 G_UNLOCK (g_signal_mutex);
1770 return_value = NULL;
1771 #endif /* G_ENABLE_DEBUG */
1773 G_UNLOCK (g_signal_mutex);
1774 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
1778 g_signal_emit_valist (gpointer instance,
1783 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1784 GType signal_return_type;
1785 GValue *param_values;
1789 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1790 g_return_if_fail (signal_id > 0);
1792 G_LOCK (g_signal_mutex);
1793 node = LOOKUP_SIGNAL_NODE (signal_id);
1794 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1796 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1797 G_UNLOCK (g_signal_mutex);
1800 #ifndef G_DISABLE_CHECKS
1801 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1803 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1804 G_UNLOCK (g_signal_mutex);
1807 #endif /* !G_DISABLE_CHECKS */
1809 n_params = node->n_params;
1810 signal_return_type = node->return_type;
1811 if (node->n_params < MAX_STACK_VALUES)
1812 instance_and_params = stack_values;
1815 free_me = g_new (GValue, node->n_params + 1);
1816 instance_and_params = free_me;
1818 param_values = instance_and_params + 1;
1819 for (i = 0; i < node->n_params; i++)
1822 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1823 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1825 param_values[i].g_type = 0;
1826 G_UNLOCK (g_signal_mutex);
1827 g_value_init (param_values + i, ptype);
1828 G_VALUE_COLLECT (param_values + i,
1830 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1834 g_warning ("%s: %s", G_STRLOC, error);
1837 /* we purposely leak the value here, it might not be
1838 * in a sane state if an error condition occoured
1841 g_value_unset (param_values + i);
1846 G_LOCK (g_signal_mutex);
1848 G_UNLOCK (g_signal_mutex);
1849 instance_and_params->g_type = 0;
1850 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1851 g_value_set_instance (instance_and_params, instance);
1852 if (signal_return_type == G_TYPE_NONE)
1853 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
1856 GValue return_value = { 0, };
1857 gchar *error = NULL;
1858 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1859 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
1861 g_value_init (&return_value, rtype);
1863 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
1865 G_VALUE_LCOPY (&return_value,
1867 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1870 g_value_unset (&return_value);
1873 g_warning ("%s: %s", G_STRLOC, error);
1876 /* we purposely leak the value here, it might not be
1877 * in a sane state if an error condition occoured
1881 for (i = 0; i < n_params; i++)
1882 g_value_unset (param_values + i);
1883 g_value_unset (instance_and_params);
1889 g_signal_emit (gpointer instance,
1896 va_start (var_args, detail);
1897 g_signal_emit_valist (instance, signal_id, detail, var_args);
1902 g_signal_emit_by_name (gpointer instance,
1903 const gchar *detailed_signal,
1909 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1910 g_return_if_fail (detailed_signal != NULL);
1912 G_LOCK (g_signal_mutex);
1913 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1914 G_UNLOCK (g_signal_mutex);
1920 va_start (var_args, detailed_signal);
1921 g_signal_emit_valist (instance, signal_id, detail, var_args);
1925 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1928 static inline gboolean
1929 accumulate (GSignalInvocationHint *ihint,
1930 GValue *return_accu,
1931 GValue *handler_return,
1932 SignalAccumulator *accumulator)
1934 gboolean continue_emission;
1939 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1940 g_value_reset (handler_return);
1942 return continue_emission;
1946 signal_emit_unlocked_R (SignalNode *node,
1949 GValue *emission_return,
1950 const GValue *instance_and_params)
1952 EmissionState emission_state = 0;
1953 SignalAccumulator *accumulator;
1954 GSignalInvocationHint ihint;
1955 GClosure *class_closure;
1957 Handler *handler_list = NULL;
1958 GValue *return_accu, accu = { 0, };
1960 gulong max_sequential_handler_number;
1961 gboolean return_value_altered = FALSE;
1963 #ifdef G_ENABLE_DEBUG
1964 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
1966 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
1967 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
1970 if (g_trap_instance_signals == instance)
1973 #endif /* G_ENABLE_DEBUG */
1975 G_LOCK (g_signal_mutex);
1976 signal_id = node->signal_id;
1977 if (node->flags & G_SIGNAL_NO_RECURSE)
1979 Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
1983 *emission->state_p = EMISSION_RESTART;
1984 return return_value_altered;
1987 ihint.signal_id = node->signal_id;
1988 ihint.detail = detail;
1989 accumulator = node->accumulator;
1992 G_UNLOCK (g_signal_mutex);
1993 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1994 return_accu = &accu;
1995 G_LOCK (g_signal_mutex);
1998 return_accu = emission_return;
1999 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
2000 signal_id, detail, instance, &emission_state);
2001 class_closure = node->class_closure;
2006 handler_unref_R (signal_id, instance, handler_list);
2007 max_sequential_handler_number = g_handler_sequential_number;
2008 hlist = handler_list_lookup (signal_id, instance);
2009 handler_list = hlist ? hlist->handlers : NULL;
2011 handler_ref (handler_list);
2013 ihint.run_type = G_SIGNAL_RUN_FIRST;
2015 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2017 emission_state = EMISSION_RUN;
2019 G_UNLOCK (g_signal_mutex);
2020 g_closure_invoke (class_closure,
2023 instance_and_params,
2025 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2026 emission_state == EMISSION_RUN)
2027 emission_state = EMISSION_STOP;
2028 G_LOCK (g_signal_mutex);
2029 return_value_altered = TRUE;
2031 if (emission_state == EMISSION_STOP)
2033 else if (emission_state == EMISSION_RESTART)
2037 if (node->emission_hooks)
2039 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2042 emission_state = EMISSION_HOOK;
2043 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2046 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2048 if (!signal_hook->detail || signal_hook->detail == detail)
2050 GSignalEmissionHook hook_func = hook->func;
2052 was_in_call = G_HOOK_IN_CALL (hook);
2053 hook->flags |= G_HOOK_FLAG_IN_CALL;
2054 G_UNLOCK (g_signal_mutex);
2055 need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
2056 G_LOCK (g_signal_mutex);
2058 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2060 g_hook_destroy_link (node->emission_hooks, hook);
2062 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2065 if (emission_state == EMISSION_RESTART)
2071 Handler *handler = handler_list;
2073 emission_state = EMISSION_RUN;
2074 handler_ref (handler);
2081 handler_unref_R (signal_id, instance, handler_list);
2082 handler_list = handler;
2085 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2086 handler->sequential_number < max_sequential_handler_number)
2088 G_UNLOCK (g_signal_mutex);
2089 g_closure_invoke (handler->closure,
2092 instance_and_params,
2094 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2095 emission_state == EMISSION_RUN)
2096 emission_state = EMISSION_STOP;
2097 G_LOCK (g_signal_mutex);
2098 return_value_altered = TRUE;
2100 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2103 tmp = handler->next;
2107 handler_unref_R (signal_id, instance, handler_list);
2108 handler_list = handler;
2113 if (emission_state == EMISSION_STOP)
2115 else if (emission_state == EMISSION_RESTART)
2119 ihint.run_type = G_SIGNAL_RUN_LAST;
2121 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2123 emission_state = EMISSION_RUN;
2125 G_UNLOCK (g_signal_mutex);
2126 g_closure_invoke (class_closure,
2129 instance_and_params,
2131 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2132 emission_state == EMISSION_RUN)
2133 emission_state = EMISSION_STOP;
2134 G_LOCK (g_signal_mutex);
2135 return_value_altered = TRUE;
2137 if (emission_state == EMISSION_STOP)
2139 else if (emission_state == EMISSION_RESTART)
2145 Handler *handler = handler_list;
2147 emission_state = EMISSION_RUN;
2148 handler_ref (handler);
2153 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2154 handler->sequential_number < max_sequential_handler_number)
2156 G_UNLOCK (g_signal_mutex);
2157 g_closure_invoke (handler->closure,
2160 instance_and_params,
2162 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2163 emission_state == EMISSION_RUN)
2164 emission_state = EMISSION_STOP;
2165 G_LOCK (g_signal_mutex);
2166 return_value_altered = TRUE;
2168 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2171 tmp = handler->next;
2175 handler_unref_R (signal_id, instance, handler);
2180 if (emission_state == EMISSION_STOP)
2182 else if (emission_state == EMISSION_RESTART)
2188 ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2190 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2192 gboolean need_unset = FALSE;
2194 emission_state = EMISSION_STOP;
2196 G_UNLOCK (g_signal_mutex);
2197 if (node->return_type != G_TYPE_NONE && !accumulator)
2199 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2202 g_closure_invoke (class_closure,
2203 node->return_type != G_TYPE_NONE ? &accu : NULL,
2205 instance_and_params,
2208 g_value_unset (&accu);
2209 G_LOCK (g_signal_mutex);
2211 if (emission_state == EMISSION_RESTART)
2216 handler_unref_R (signal_id, instance, handler_list);
2218 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2219 G_UNLOCK (g_signal_mutex);
2221 g_value_unset (&accu);
2223 return return_value_altered;
2227 /* --- compile standard marshallers --- */
2228 #include "gobject.h"
2230 #include "gmarshal.c"