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;
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_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 gulong handler_seq_no = 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_seq_no = handler->sequential_number;
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);
1251 return handler_seq_no;
1255 g_signal_connect_closure (gpointer instance,
1256 const gchar *detailed_signal,
1261 gulong handler_seq_no = 0;
1265 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1266 g_return_val_if_fail (detailed_signal != NULL, 0);
1267 g_return_val_if_fail (closure != NULL, 0);
1269 G_LOCK (g_signal_mutex);
1270 itype = G_TYPE_FROM_INSTANCE (instance);
1271 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1274 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1276 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1277 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1278 else if (!g_type_is_a (itype, node->itype))
1279 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1282 Handler *handler = handler_new (after);
1284 handler_seq_no = handler->sequential_number;
1285 handler->detail = detail;
1286 handler->closure = g_closure_ref (closure);
1287 g_closure_sink (closure);
1288 handler_insert (signal_id, instance, handler);
1289 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1290 g_closure_set_marshal (handler->closure, node->c_marshaller);
1294 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1295 G_UNLOCK (g_signal_mutex);
1297 return handler_seq_no;
1301 g_signal_connect_data (gpointer instance,
1302 const gchar *detailed_signal,
1303 GCallback c_handler,
1305 GClosureNotify destroy_data,
1310 gulong handler_seq_no = 0;
1314 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1315 g_return_val_if_fail (detailed_signal != NULL, 0);
1316 g_return_val_if_fail (c_handler != NULL, 0);
1318 G_LOCK (g_signal_mutex);
1319 itype = G_TYPE_FROM_INSTANCE (instance);
1320 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1323 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1325 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1326 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1327 else if (!g_type_is_a (itype, node->itype))
1328 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1331 Handler *handler = handler_new (after);
1333 handler_seq_no = handler->sequential_number;
1334 handler->detail = detail;
1335 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1336 g_closure_sink (handler->closure);
1337 handler_insert (signal_id, instance, handler);
1338 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1339 g_closure_set_marshal (handler->closure, node->c_marshaller);
1343 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1344 G_UNLOCK (g_signal_mutex);
1346 return handler_seq_no;
1350 g_signal_handler_block (gpointer instance,
1355 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1356 g_return_if_fail (handler_id > 0);
1358 G_LOCK (g_signal_mutex);
1359 handler = handler_lookup (instance, handler_id, NULL);
1362 #ifndef G_DISABLE_CHECKS
1363 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1364 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1366 handler->block_count += 1;
1369 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1370 G_UNLOCK (g_signal_mutex);
1374 g_signal_handler_unblock (gpointer instance,
1379 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1380 g_return_if_fail (handler_id > 0);
1382 G_LOCK (g_signal_mutex);
1383 handler = handler_lookup (instance, handler_id, NULL);
1386 if (handler->block_count)
1387 handler->block_count -= 1;
1389 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1392 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1393 G_UNLOCK (g_signal_mutex);
1397 g_signal_handler_disconnect (gpointer instance,
1403 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1404 g_return_if_fail (handler_id > 0);
1406 G_LOCK (g_signal_mutex);
1407 handler = handler_lookup (instance, handler_id, &signal_id);
1410 handler->sequential_number = 0;
1411 handler->block_count = 1;
1412 handler_unref_R (signal_id, instance, handler);
1415 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1416 G_UNLOCK (g_signal_mutex);
1420 g_signal_handlers_destroy (gpointer instance)
1422 GBSearchArray *hlbsa;
1424 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1426 G_LOCK (g_signal_mutex);
1427 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1432 /* reentrancy caution, delete instance trace first */
1433 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1435 for (i = 0; i < hlbsa->n_nodes; i++)
1437 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1438 Handler *handler = hlist->handlers;
1442 Handler *tmp = handler;
1444 handler = tmp->next;
1445 tmp->block_count = 1;
1446 /* cruel unlink, this works because _all_ handlers vanish */
1449 if (tmp->sequential_number)
1451 tmp->sequential_number = 0;
1452 handler_unref_R (0, NULL, tmp);
1456 g_bsearch_array_destroy (hlbsa);
1458 G_UNLOCK (g_signal_mutex);
1462 g_signal_handler_find (gpointer instance,
1463 GSignalMatchType mask,
1470 gulong handler_seq_no = 0;
1472 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1473 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1475 if (mask & G_SIGNAL_MATCH_MASK)
1477 HandlerMatch *mlist;
1479 G_LOCK (g_signal_mutex);
1480 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1483 handler_seq_no = mlist->handler->sequential_number;
1484 handler_match_free1_R (mlist, instance);
1486 G_UNLOCK (g_signal_mutex);
1489 return handler_seq_no;
1493 signal_handlers_foreach_matched_R (gpointer instance,
1494 GSignalMatchType mask,
1500 void (*callback) (gpointer instance,
1501 gulong handler_seq_no))
1503 HandlerMatch *mlist;
1504 guint n_handlers = 0;
1506 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1510 if (mlist->handler->sequential_number)
1512 G_UNLOCK (g_signal_mutex);
1513 callback (instance, mlist->handler->sequential_number);
1514 G_LOCK (g_signal_mutex);
1516 mlist = handler_match_free1_R (mlist, instance);
1523 g_signal_handlers_block_matched (gpointer instance,
1524 GSignalMatchType mask,
1531 guint n_handlers = 0;
1533 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1534 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1536 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1538 G_LOCK (g_signal_mutex);
1539 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1540 closure, func, data,
1541 g_signal_handler_block);
1542 G_UNLOCK (g_signal_mutex);
1549 g_signal_handlers_unblock_matched (gpointer instance,
1550 GSignalMatchType mask,
1557 guint n_handlers = 0;
1559 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1560 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1562 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1564 G_LOCK (g_signal_mutex);
1565 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1566 closure, func, data,
1567 g_signal_handler_unblock);
1568 G_UNLOCK (g_signal_mutex);
1575 g_signal_handlers_disconnect_matched (gpointer instance,
1576 GSignalMatchType mask,
1583 guint n_handlers = 0;
1585 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1586 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1588 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1590 G_LOCK (g_signal_mutex);
1591 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1592 closure, func, data,
1593 g_signal_handler_disconnect);
1594 G_UNLOCK (g_signal_mutex);
1601 g_signal_has_handler_pending (gpointer instance,
1604 gboolean may_be_blocked)
1606 HandlerMatch *mlist;
1607 gboolean has_pending;
1609 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1610 g_return_val_if_fail (signal_id > 0, FALSE);
1612 G_LOCK (g_signal_mutex);
1615 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1617 if (!(node->flags & G_SIGNAL_DETAILED))
1619 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1620 G_UNLOCK (g_signal_mutex);
1624 mlist = handlers_find (instance,
1625 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1626 signal_id, detail, NULL, NULL, NULL, TRUE);
1630 handler_match_free1_R (mlist, instance);
1633 has_pending = FALSE;
1634 G_UNLOCK (g_signal_mutex);
1640 g_signal_emitv (const GValue *instance_and_params,
1643 GValue *return_value)
1645 const GValue *param_values;
1650 g_return_if_fail (instance_and_params != NULL);
1651 instance = g_value_peek_pointer (instance_and_params);
1652 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1653 g_return_if_fail (signal_id > 0);
1655 param_values = instance_and_params + 1;
1657 G_LOCK (g_signal_mutex);
1658 node = LOOKUP_SIGNAL_NODE (signal_id);
1659 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1661 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1662 G_UNLOCK (g_signal_mutex);
1665 #ifdef G_ENABLE_DEBUG
1666 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1668 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1669 G_UNLOCK (g_signal_mutex);
1672 for (i = 0; i < node->n_params; i++)
1673 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1675 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1677 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1680 G_VALUE_TYPE_NAME (param_values + i));
1681 G_UNLOCK (g_signal_mutex);
1684 if (node->return_type != G_TYPE_NONE)
1688 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1690 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1692 G_UNLOCK (g_signal_mutex);
1695 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1697 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1699 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1701 G_VALUE_TYPE_NAME (return_value));
1702 G_UNLOCK (g_signal_mutex);
1707 return_value = NULL;
1708 #endif /* G_ENABLE_DEBUG */
1710 signal_emit_R (node, detail, instance, return_value, instance_and_params);
1711 G_UNLOCK (g_signal_mutex);
1715 g_signal_emit_valist (gpointer instance,
1720 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1721 GValue *param_values;
1725 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1726 g_return_if_fail (signal_id > 0);
1728 G_LOCK (g_signal_mutex);
1729 node = LOOKUP_SIGNAL_NODE (signal_id);
1730 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1732 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1733 G_UNLOCK (g_signal_mutex);
1736 #ifndef G_DISABLE_CHECKS
1737 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1739 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1740 G_UNLOCK (g_signal_mutex);
1743 #endif /* !G_DISABLE_CHECKS */
1745 if (node->n_params < MAX_STACK_VALUES)
1746 instance_and_params = stack_values;
1749 free_me = g_new (GValue, node->n_params + 1);
1750 instance_and_params = free_me;
1752 param_values = instance_and_params + 1;
1753 for (i = 0; i < node->n_params; i++)
1757 param_values[i].g_type = 0;
1758 g_value_init (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1759 G_VALUE_COLLECT (param_values + i,
1761 node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1765 g_warning ("%s: %s", G_STRLOC, error);
1768 /* we purposely leak the value here, it might not be
1769 * in a sane state if an error condition occoured
1772 g_value_unset (param_values + i);
1774 G_UNLOCK (g_signal_mutex);
1779 instance_and_params->g_type = 0;
1780 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1781 g_value_set_instance (instance_and_params, instance);
1782 if (node->return_type == G_TYPE_NONE)
1783 signal_emit_R (node, detail, instance, NULL, instance_and_params);
1786 GValue return_value = { 0, };
1787 gchar *error = NULL;
1789 g_value_init (&return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1790 signal_emit_R (node, detail, instance, &return_value, instance_and_params);
1791 G_VALUE_LCOPY (&return_value,
1793 node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE ? G_VALUE_NOCOPY_CONTENTS : 0,
1796 g_value_unset (&return_value);
1799 g_warning ("%s: %s", G_STRLOC, error);
1802 /* we purposely leak the value here, it might not be
1803 * in a sane state if an error condition occoured
1807 for (i = 0; i < node->n_params; i++)
1808 g_value_unset (param_values + i);
1809 g_value_unset (instance_and_params);
1812 G_UNLOCK (g_signal_mutex);
1816 g_signal_emit (gpointer instance,
1823 va_start (var_args, detail);
1824 g_signal_emit_valist (instance, signal_id, detail, var_args);
1829 g_signal_emit_by_name (gpointer instance,
1830 const gchar *detailed_signal,
1836 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1837 g_return_if_fail (detailed_signal != NULL);
1839 G_LOCK (g_signal_mutex);
1840 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1841 G_UNLOCK (g_signal_mutex);
1847 va_start (var_args, detailed_signal);
1848 g_signal_emit_valist (instance, signal_id, detail, var_args);
1852 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1855 static inline gboolean
1856 accumulate (GSignalInvocationHint *ihint,
1857 GValue *return_accu,
1858 GValue *handler_return,
1859 SignalAccumulator *accumulator)
1861 gboolean continue_emission;
1866 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1867 g_value_reset (handler_return);
1869 return continue_emission;
1873 signal_emit_R (SignalNode *node,
1876 GValue *emission_return,
1877 const GValue *instance_and_params)
1879 EmissionState emission_state = 0;
1880 SignalAccumulator *accumulator;
1881 GSignalInvocationHint ihint;
1882 GClosure *class_closure;
1884 Handler *handler_list = NULL;
1885 GValue *return_accu, accu = { 0, };
1886 guint signal_id = node->signal_id;
1887 gulong max_sequential_handler_number;
1888 gboolean return_value_altered = FALSE;
1890 #ifdef G_ENABLE_DEBUG
1891 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
1893 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
1894 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
1897 if (g_trap_instance_signals == instance)
1900 #endif /* G_ENABLE_DEBUG */
1902 if (node->flags & G_SIGNAL_NO_RECURSE)
1904 Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
1908 *emission->state_p = EMISSION_RESTART;
1909 return return_value_altered;
1912 ihint.signal_id = node->signal_id;
1913 ihint.detail = detail;
1914 accumulator = node->accumulator;
1917 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
1918 return_accu = &accu;
1921 return_accu = emission_return;
1922 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
1923 signal_id, detail, instance, &emission_state);
1924 class_closure = node->class_closure;
1929 handler_unref_R (signal_id, instance, handler_list);
1930 max_sequential_handler_number = g_handler_sequential_number;
1931 hlist = handler_list_lookup (signal_id, instance);
1932 handler_list = hlist ? hlist->handlers : NULL;
1934 handler_ref (handler_list);
1936 ihint.run_type = G_SIGNAL_RUN_FIRST;
1938 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
1940 emission_state = EMISSION_RUN;
1942 G_UNLOCK (g_signal_mutex);
1943 g_closure_invoke (class_closure,
1946 instance_and_params,
1948 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
1949 emission_state == EMISSION_RUN)
1950 emission_state = EMISSION_STOP;
1951 G_LOCK (g_signal_mutex);
1952 return_value_altered = TRUE;
1954 if (emission_state == EMISSION_STOP)
1956 else if (emission_state == EMISSION_RESTART)
1960 if (node->emission_hooks)
1962 gboolean need_destroy, was_in_call, may_recurse = TRUE;
1965 emission_state = EMISSION_HOOK;
1966 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
1969 SignalHook *signal_hook = SIGNAL_HOOK (hook);
1971 if (!signal_hook->detail || signal_hook->detail == detail)
1973 GSignalEmissionHook hook_func = hook->func;
1975 was_in_call = G_HOOK_IN_CALL (hook);
1976 hook->flags |= G_HOOK_FLAG_IN_CALL;
1977 G_UNLOCK (g_signal_mutex);
1978 need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
1979 G_LOCK (g_signal_mutex);
1981 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
1983 g_hook_destroy_link (node->emission_hooks, hook);
1985 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
1988 if (emission_state == EMISSION_RESTART)
1994 Handler *handler = handler_list;
1996 emission_state = EMISSION_RUN;
1997 handler_ref (handler);
2004 handler_unref_R (signal_id, instance, handler_list);
2005 handler_list = handler;
2008 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2009 handler->sequential_number < max_sequential_handler_number)
2011 G_UNLOCK (g_signal_mutex);
2012 g_closure_invoke (handler->closure,
2015 instance_and_params,
2017 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2018 emission_state == EMISSION_RUN)
2019 emission_state = EMISSION_STOP;
2020 G_LOCK (g_signal_mutex);
2021 return_value_altered = TRUE;
2023 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2026 tmp = handler->next;
2030 handler_unref_R (signal_id, instance, handler_list);
2031 handler_list = handler;
2036 if (emission_state == EMISSION_STOP)
2038 else if (emission_state == EMISSION_RESTART)
2042 ihint.run_type = G_SIGNAL_RUN_LAST;
2044 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2046 emission_state = EMISSION_RUN;
2048 G_UNLOCK (g_signal_mutex);
2049 g_closure_invoke (class_closure,
2052 instance_and_params,
2054 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2055 emission_state == EMISSION_RUN)
2056 emission_state = EMISSION_STOP;
2057 G_LOCK (g_signal_mutex);
2058 return_value_altered = TRUE;
2060 if (emission_state == EMISSION_STOP)
2062 else if (emission_state == EMISSION_RESTART)
2068 Handler *handler = handler_list;
2070 emission_state = EMISSION_RUN;
2071 handler_ref (handler);
2076 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2077 handler->sequential_number < max_sequential_handler_number)
2079 G_UNLOCK (g_signal_mutex);
2080 g_closure_invoke (handler->closure,
2083 instance_and_params,
2085 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2086 emission_state == EMISSION_RUN)
2087 emission_state = EMISSION_STOP;
2088 G_LOCK (g_signal_mutex);
2089 return_value_altered = TRUE;
2091 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2094 tmp = handler->next;
2098 handler_unref_R (signal_id, instance, handler);
2103 if (emission_state == EMISSION_STOP)
2105 else if (emission_state == EMISSION_RESTART)
2111 ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2113 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2115 gboolean need_unset = FALSE;
2117 emission_state = EMISSION_STOP;
2119 G_UNLOCK (g_signal_mutex);
2120 if (node->return_type != G_TYPE_NONE && !accumulator)
2122 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2125 g_closure_invoke (class_closure,
2126 node->return_type != G_TYPE_NONE ? &accu : NULL,
2128 instance_and_params,
2131 g_value_unset (&accu);
2132 G_LOCK (g_signal_mutex);
2134 if (emission_state == EMISSION_RESTART)
2139 handler_unref_R (signal_id, instance, handler_list);
2141 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2143 g_value_unset (&accu);
2145 return return_value_altered;
2149 /* --- compile standard marshallers --- */
2150 #include "gobject.h"
2152 #include "gmarshal.c"