1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 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>
24 #include "gbsearcharray.h"
27 /* pre allocation configurations
29 #define BSA_PRE_ALLOC (20)
30 #define HANDLER_PRE_ALLOC (48)
31 #define EMISSION_PRE_ALLOC (16)
33 #define TIGHT_MEMORY (1)
35 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
38 /* --- generic allocation --- */
39 /* we can special case allocations generically by replacing
40 * these functions with more speed/memory aware variants
42 static inline gpointer
43 g_generic_node_alloc (GTrashStack **trash_stack_p,
45 guint nodes_pre_alloc)
47 gpointer node = g_trash_stack_pop (trash_stack_p);
53 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
54 block = g_malloc (sizeof_node * nodes_pre_alloc);
55 while (--nodes_pre_alloc)
57 g_trash_stack_push (trash_stack_p, block);
66 g_generic_node_free (GTrashStack **trash_stack_p,
69 g_trash_stack_push (trash_stack_p, node);
73 /* --- typedefs --- */
74 typedef struct _SignalNode SignalNode;
75 typedef struct _SignalKey SignalKey;
76 typedef struct _Emission Emission;
77 typedef struct _Handler Handler;
78 typedef struct _HandlerList HandlerList;
88 /* --- prototypes --- */
89 static inline guint signal_id_lookup (GQuark quark,
91 static void signal_destroy_R (SignalNode *signal_node);
92 static inline HandlerList* handler_list_ensure (guint signal_id,
94 static inline HandlerList* handler_list_lookup (guint signal_id,
96 static inline Handler* handler_new (gboolean after);
97 static void handler_insert (guint signal_id,
100 static Handler* handler_lookup (gpointer instance,
103 static Handler* handler_find (gpointer instance,
104 GSignalMatchType mask,
109 static inline void handler_ref (Handler *handler);
110 static inline void handler_unref_R (guint signal_id,
113 static inline void emission_push (Emission **emission_list_p,
116 EmissionState *state_p);
117 static inline void emission_pop (Emission **emission_list_p);
118 static inline Emission* emission_find (Emission *emission_list,
121 static void signal_emit_R (SignalNode *node,
123 GValue *return_value,
124 const GValue *instance_and_params);
127 /* --- structures --- */
130 /* permanent portion */
136 /* reinitializable portion */
141 GClosure *class_closure;
142 GSignalAccumulator accumulator;
143 GSignalCMarshaller c_marshaller;
144 GHookList *emission_hooks;
159 EmissionState *state_p;
173 guint ref_count : 16;
174 #define HANDLER_MAX_REF_COUNT (1 << 16)
175 guint block_count : 12;
176 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
182 /* --- variables --- */
183 static GBSearchArray g_signal_key_bsa = { NULL, 0, 0, 0, NULL };
184 static GHashTable *g_handler_list_bsa_ht = NULL;
185 static Emission *g_recursive_emissions = NULL;
186 static Emission *g_restart_emissions = NULL;
187 static GTrashStack *g_bsa_ts = NULL;
188 static GTrashStack *g_handler_ts = NULL;
189 static GTrashStack *g_emission_ts = NULL;
190 G_LOCK_DEFINE_STATIC (g_signal_mutex);
193 /* --- signal nodes --- */
194 static guint g_n_signal_nodes = 0;
195 static SignalNode **g_signal_nodes = NULL;
197 static inline SignalNode*
198 LOOKUP_SIGNAL_NODE (register guint signal_id)
200 if (signal_id < g_n_signal_nodes)
201 return g_signal_nodes[signal_id];
207 /* --- functions --- */
209 signal_id_lookup (GQuark quark,
212 SignalKey key, *signal_key;
217 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
219 return signal_key ? signal_key->signal_id : 0;
223 handler_lists_cmp (gconstpointer node1,
226 const HandlerList *hlist1 = node1, *hlist2 = node2;
228 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
231 static inline HandlerList*
232 handler_list_ensure (guint signal_id,
235 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
240 hlbsa = g_generic_node_alloc (&g_bsa_ts,
241 sizeof (GBSearchArray),
243 hlbsa->cmp_func = handler_lists_cmp;
244 hlbsa->sizeof_node = sizeof (HandlerList);
245 hlbsa->flags = G_BSEARCH_DEFER_SHRINK;
248 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
250 key.signal_id = signal_id;
253 return g_bsearch_array_insert (hlbsa, &key, FALSE);
256 static inline HandlerList*
257 handler_list_lookup (guint signal_id,
260 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
263 key.signal_id = signal_id;
265 return hlbsa ? g_bsearch_array_lookup (hlbsa, &key) : NULL;
269 handler_lookup (gpointer instance,
273 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
279 for (i = 0; i < hlbsa->n_nodes; i++)
281 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
284 for (handler = hlist->handlers; handler; handler = handler->next)
285 if (handler->id == handler_id)
288 *signal_id_p = hlist->signal_id;
299 handler_find (gpointer instance,
300 GSignalMatchType mask,
306 if (mask & G_SIGNAL_MATCH_ID)
308 HandlerList *hlist = handler_list_lookup (signal_id, instance);
312 if (mask & G_SIGNAL_MATCH_FUNC)
314 node = LOOKUP_SIGNAL_NODE (signal_id);
315 if (!node || !node->c_marshaller)
320 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
321 if (((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
322 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
323 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
324 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
325 handler->closure->meta_marshal == 0 &&
326 ((GCClosure*) handler->closure)->callback == func)))
331 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
338 for (i = 0; i < hlbsa->n_nodes; i++)
340 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
344 if (!(mask & G_SIGNAL_MATCH_FUNC))
346 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
347 if (!node->c_marshaller)
351 for (handler = hlist->handlers; handler; handler = handler->next)
352 if (((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
353 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
354 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
355 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
356 handler->closure->meta_marshal == 0 &&
357 ((GCClosure*) handler->closure)->callback == func)))
366 static inline Handler*
367 handler_new (gboolean after)
369 static guint handler_id = 1;
370 Handler *handler = g_generic_node_alloc (&g_handler_ts,
373 #ifndef G_DISABLE_CHECKS
375 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
378 handler->id = handler_id++;
379 handler->prev = NULL;
380 handler->next = NULL;
381 handler->ref_count = 1;
382 handler->block_count = 0;
383 handler->after = after != FALSE;
384 handler->closure = NULL;
390 handler_ref (Handler *handler)
392 g_return_if_fail (handler->ref_count > 0);
394 #ifndef G_DISABLE_CHECKS
395 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
396 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
399 handler->ref_count += 1;
403 handler_unref_R (guint signal_id,
407 g_return_if_fail (handler->ref_count > 0);
409 handler->ref_count -= 1;
410 if (!handler->ref_count)
413 handler->next->prev = handler->prev;
414 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
415 handler->prev->next = handler->next;
418 HandlerList *hlist = handler_list_lookup (signal_id, instance);
420 hlist->handlers = handler->next;
422 G_UNLOCK (g_signal_mutex);
423 g_closure_unref (handler->closure);
424 G_LOCK (g_signal_mutex);
425 g_generic_node_free (&g_handler_ts, handler);
430 handler_insert (guint signal_id,
436 g_assert (handler->prev == NULL && handler->next == NULL); // FIXME: paranoid
438 hlist = handler_list_ensure (signal_id, instance);
439 if (!hlist->handlers)
440 hlist->handlers = handler;
441 else if (hlist->handlers->after && !handler->after)
443 handler->next = hlist->handlers;
444 hlist->handlers->prev = handler;
445 hlist->handlers = handler;
449 Handler *tmp = hlist->handlers;
455 while (tmp->next && !tmp->next->after)
458 tmp->next->prev = handler;
459 handler->next = tmp->next;
466 emission_push (Emission **emission_list_p,
469 EmissionState *state_p)
471 Emission *emission = g_generic_node_alloc (&g_emission_ts,
474 emission->next = *emission_list_p;
475 emission->signal_id = signal_id;
476 emission->instance = instance;
477 emission->state_p = state_p;
478 *emission_list_p = emission;
482 emission_pop (Emission **emission_list_p)
484 Emission *emission = *emission_list_p;
486 *emission_list_p = emission->next;
487 g_generic_node_free (&g_emission_ts, emission);
490 static inline Emission*
491 emission_find (Emission *emission_list,
497 for (emission = emission_list; emission; emission = emission->next)
498 if (emission->instance == instance && emission->signal_id == signal_id)
504 signal_key_cmp (gconstpointer node1,
507 const SignalKey *key1 = node1, *key2 = node2;
509 if (key1->itype == key2->itype)
510 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
512 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
516 g_signal_init (void) /* sync with gtype.c */
518 G_LOCK (g_signal_mutex);
519 if (!g_n_signal_nodes)
521 /* setup signal key array */
522 g_signal_key_bsa.cmp_func = signal_key_cmp;
523 g_signal_key_bsa.sizeof_node = sizeof (SignalKey);
524 g_signal_key_bsa.flags = 0; /* alloc-only */
526 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
527 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
529 /* invalid (0) signal_id */
530 g_n_signal_nodes = 1;
531 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
532 g_signal_nodes[0] = NULL;
534 G_UNLOCK (g_signal_mutex);
538 g_signals_destroy (GType itype)
541 gboolean found_one = FALSE;
543 G_LOCK (g_signal_mutex);
544 for (i = 0; i < g_n_signal_nodes; i++)
546 SignalNode *node = g_signal_nodes[i];
548 if (node->itype == itype)
551 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
553 g_type_name (node->itype));
557 signal_destroy_R (node);
562 g_warning (G_STRLOC ": type `%s' has no signals that could be destroyed",
563 g_type_name (itype));
564 G_UNLOCK (g_signal_mutex);
568 g_signal_stop_emission (gpointer instance,
573 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
574 g_return_if_fail (signal_id > 0);
576 G_LOCK (g_signal_mutex);
577 node = LOOKUP_SIGNAL_NODE (signal_id);
578 if (node && g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
580 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
581 Emission *emission = emission_find (emission_list, signal_id, instance);
585 if (*emission->state_p == EMISSION_HOOK)
586 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
587 node->name, instance);
588 else if (*emission->state_p == EMISSION_RUN)
589 *emission->state_p = EMISSION_STOP;
592 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
593 node->name, instance);
596 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
597 G_UNLOCK (g_signal_mutex);
601 g_signal_lookup (const gchar *name,
606 g_return_val_if_fail (name != NULL, 0);
607 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
609 G_LOCK (g_signal_mutex);
610 quark = g_quark_try_string (name);
614 guint signal_id = signal_id_lookup (quark, itype);
619 itype = g_type_parent (itype);
622 G_UNLOCK (g_signal_mutex);
628 g_signal_name (guint signal_id)
633 G_LOCK (g_signal_mutex);
634 node = LOOKUP_SIGNAL_NODE (signal_id);
635 name = node ? node->name : NULL;
636 G_UNLOCK (g_signal_mutex);
642 g_signal_query (guint signal_id,
647 g_return_if_fail (query != NULL);
649 G_LOCK (g_signal_mutex);
650 node = LOOKUP_SIGNAL_NODE (signal_id);
651 if (!node || node->destroyed)
652 query->signal_id = 0;
655 query->signal_id = node->signal_id;
656 query->signal_name = node->name;
657 query->itype = node->itype;
658 query->signal_flags = node->flags;
659 query->return_type = node->return_type;
660 query->n_params = node->n_params;
661 query->param_types = node->param_types;
663 G_UNLOCK (g_signal_mutex);
667 g_signal_newv (const gchar *signal_name,
669 GSignalType signal_flags,
670 GClosure *class_closure,
671 GSignalAccumulator accumulator,
672 GSignalCMarshaller c_marshaller,
681 g_return_val_if_fail (signal_name != NULL, 0);
682 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
684 g_return_val_if_fail (param_types != NULL, 0);
685 if (return_type != G_TYPE_NONE)
686 g_return_val_if_fail (accumulator == NULL, 0);
688 name = g_strdup (signal_name);
689 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); // FIXME do character checks like for types
691 G_LOCK (g_signal_mutex);
693 signal_id = g_signal_lookup (name, itype);
694 node = LOOKUP_SIGNAL_NODE (signal_id);
695 if (node && !node->destroyed)
697 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
699 g_type_name (node->itype),
700 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
702 G_UNLOCK (g_signal_mutex);
705 if (node && node->itype != itype)
707 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
710 g_type_name (node->itype));
712 G_UNLOCK (g_signal_mutex);
715 for (i = 0; i < n_params; i++)
716 if (!G_TYPE_IS_VALUE (param_types[i]) ||
717 param_types[i] == G_TYPE_ENUM || param_types[i] == G_TYPE_FLAGS) /* FIXME: kludge */
719 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
720 i + 1, g_type_name (param_types[i]), g_type_name (itype), name);
722 G_UNLOCK (g_signal_mutex);
725 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type))
727 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
728 g_type_name (param_types[i]), g_type_name (itype), name);
730 G_UNLOCK (g_signal_mutex);
734 /* setup permanent portion of signal node */
739 signal_id = g_n_signal_nodes++;
740 node = g_new (SignalNode, 1);
741 node->signal_id = signal_id;
742 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
743 g_signal_nodes[signal_id] = node;
747 key.quark = g_quark_from_string (node->name);
748 key.signal_id = signal_id;
749 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
750 g_strdelimit (node->name, "_", '-');
751 key.quark = g_quark_from_static_string (node->name);
752 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
754 node->destroyed = FALSE;
756 /* setup reinitializable portion */
757 node->flags = signal_flags & (G_SIGNAL_RUN_FIRST |
759 G_SIGNAL_RUN_CLEANUP |
760 G_SIGNAL_NO_RECURSE |
763 node->n_params = n_params;
764 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
765 node->return_type = return_type;
766 node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
767 node->accumulator = accumulator;
768 node->c_marshaller = c_marshaller;
769 node->emission_hooks = NULL;
770 if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
771 g_closure_set_marshal (class_closure, node->c_marshaller);
773 G_UNLOCK (g_signal_mutex);
778 signal_destroy_R (SignalNode *signal_node)
780 SignalNode node = *signal_node;
782 signal_node->destroyed = TRUE;
784 /* reentrancy caution, zero out real contents first */
785 signal_node->n_params = 0;
786 signal_node->param_types = NULL;
787 signal_node->return_type = 0;
788 signal_node->class_closure = NULL;
789 signal_node->accumulator = NULL;
790 signal_node->c_marshaller = NULL;
791 signal_node->emission_hooks = NULL;
793 #ifndef G_DISABLE_CHECKS
794 /* check current emissions */
798 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
799 emission; emission = emission->next)
800 if (emission->signal_id == node.signal_id)
801 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
802 node.name, emission->instance);
806 /* free contents that need to
808 G_UNLOCK (g_signal_mutex);
809 g_free (node.param_types);
810 g_closure_unref (node.class_closure);
811 if (node.emission_hooks)
813 g_hook_list_clear (node.emission_hooks);
814 g_free (node.emission_hooks);
816 G_LOCK (g_signal_mutex);
820 g_signal_connect_closure (gpointer instance,
826 guint handler_id = 0;
828 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
829 g_return_val_if_fail (signal_id > 0, 0);
830 g_return_val_if_fail (closure != NULL, 0);
832 G_LOCK (g_signal_mutex);
833 node = LOOKUP_SIGNAL_NODE (signal_id);
834 if (node && g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
836 Handler *handler = handler_new (after);
838 handler_id = handler->id;
839 handler->closure = g_closure_ref (closure);
840 handler_insert (signal_id, instance, handler);
841 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
842 g_closure_set_marshal (closure, node->c_marshaller);
845 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
846 G_UNLOCK (g_signal_mutex);
852 g_signal_handler_disconnect (gpointer instance,
858 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
859 g_return_if_fail (handler_id > 0);
861 G_LOCK (g_signal_mutex);
862 handler = handler_lookup (instance, handler_id, &signal_id);
866 handler->block_count = 1;
867 handler_unref_R (signal_id, instance, handler);
870 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
871 G_UNLOCK (g_signal_mutex);
875 g_signal_handlers_destroy (gpointer instance)
877 GBSearchArray *hlbsa;
879 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
881 G_LOCK (g_signal_mutex);
882 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
887 /* reentrancy caution, delete instance trace first */
888 g_hash_table_remove (g_handler_list_bsa_ht, instance);
890 for (i = 0; i < hlbsa->n_nodes; i++)
892 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
893 Handler *handler = hlist->handlers;
897 Handler *tmp = handler;
900 tmp->block_count = 1;
901 /* cruel unlink, this works because _all_ handlers vanish */
907 handler_unref_R (0, NULL, tmp);
911 g_free (hlbsa->nodes);
912 g_generic_node_free (&g_bsa_ts, hlbsa);
914 G_UNLOCK (g_signal_mutex);
918 g_signal_handler_block (gpointer instance,
923 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
924 g_return_if_fail (handler_id > 0);
926 G_LOCK (g_signal_mutex);
927 handler = handler_lookup (instance, handler_id, NULL);
930 #ifndef G_DISABLE_CHECKS
931 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
932 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
935 handler->block_count += 1;
938 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
939 G_UNLOCK (g_signal_mutex);
943 g_signal_handler_unblock (gpointer instance,
948 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
949 g_return_if_fail (handler_id > 0);
951 G_LOCK (g_signal_mutex);
952 handler = handler_lookup (instance, handler_id, NULL);
955 if (handler->block_count)
956 handler->block_count -= 1;
958 g_warning (G_STRLOC ": handler `%u' of instance `%p' is not blocked", handler_id, instance);
961 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
962 G_UNLOCK (g_signal_mutex);
966 g_signal_handler_find (gpointer instance,
967 GSignalMatchType mask,
973 Handler *handler = NULL;
976 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
978 G_LOCK (g_signal_mutex);
979 handler = handler_find (instance, mask, signal_id, closure, func, data);
980 handler_id = handler ? handler->id : 0;
981 G_UNLOCK (g_signal_mutex);
987 g_signal_handler_pending (gpointer instance,
989 gboolean may_be_blocked)
991 Handler *handler = NULL;
993 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
994 g_return_val_if_fail (signal_id > 0, FALSE);
996 G_LOCK (g_signal_mutex);
997 handler = handler_find (instance, G_SIGNAL_MATCH_ID, signal_id, NULL, NULL, NULL);
999 for (; handler; handler = handler->next)
1000 if (!handler->block_count)
1002 G_UNLOCK (g_signal_mutex);
1004 return handler != NULL;
1008 g_signal_emitv (const GValue *instance_and_params,
1010 GValue *return_value)
1014 const GValue *param_values;
1017 g_return_if_fail (instance_and_params != NULL);
1018 instance = g_value_get_as_pointer (instance_and_params);
1019 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1020 g_return_if_fail (signal_id > 0);
1022 param_values = instance_and_params + 1;
1024 G_LOCK (g_signal_mutex);
1025 node = LOOKUP_SIGNAL_NODE (signal_id);
1026 #ifndef G_DISABLE_CHECKS
1027 if (!node || !g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
1028 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1029 for (i = 0; i < node->n_params; i++)
1030 if (!G_VALUE_HOLDS (param_values + i, node->param_types[i]))
1032 g_critical (G_STRLOC ": value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1033 g_type_name (node->param_types[i]),
1036 G_VALUE_TYPE_NAME (param_values + i));
1037 G_UNLOCK (g_signal_mutex);
1040 if (node->return_type != G_TYPE_NONE)
1044 g_critical (G_STRLOC ": return value `%s' for signal \"%s\" is (NULL)",
1045 g_type_name (node->return_type),
1047 G_UNLOCK (g_signal_mutex);
1050 else if (!node->accumulator && !G_VALUE_HOLDS (return_value, node->return_type))
1052 g_critical (G_STRLOC ": return value `%s' for signal \"%s\" is of type `%s'",
1053 g_type_name (node->return_type),
1055 G_VALUE_TYPE_NAME (return_value));
1056 G_UNLOCK (g_signal_mutex);
1061 return_value = NULL;
1062 #endif /* !G_DISABLE_CHECKS */
1064 signal_emit_R (node, instance, return_value, instance_and_params);
1066 G_UNLOCK (g_signal_mutex);
1070 signal_emit_R (SignalNode *node,
1072 GValue *return_value,
1073 const GValue *instance_and_params)
1075 EmissionState emission_state = 0;
1076 GSignalAccumulator accumulator;
1077 GClosure *class_closure;
1081 gboolean accu_used = FALSE;
1082 guint signal_id = node->signal_id;
1084 if (node->flags & G_SIGNAL_NO_RECURSE)
1086 Emission *emission = emission_find (g_restart_emissions, signal_id, instance);
1090 *emission->state_p = EMISSION_RESTART;
1094 accumulator = node->accumulator;
1097 G_UNLOCK (g_signal_mutex);
1098 g_value_init (&accu, node->return_type);
1099 G_LOCK (g_signal_mutex);
1101 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
1102 signal_id, instance, &emission_state);
1103 class_closure = node->class_closure;
1104 hlist = handler_list_lookup (signal_id, instance);
1105 handlers = hlist ? hlist->handlers : NULL;
1107 handler_ref (handlers);
1111 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
1113 emission_state = EMISSION_RUN;
1115 G_UNLOCK (g_signal_mutex);
1119 g_value_reset (&accu);
1120 g_closure_invoke (class_closure,
1121 (signal_id << 8) | G_SIGNAL_RUN_FIRST,
1124 instance_and_params);
1125 if (!accumulator (signal_id, return_value, &accu) &&
1126 emission_state == EMISSION_RUN)
1127 emission_state = EMISSION_STOP;
1131 g_closure_invoke (class_closure,
1132 (signal_id << 8) | G_SIGNAL_RUN_FIRST,
1135 instance_and_params);
1136 G_LOCK (g_signal_mutex);
1138 if (emission_state == EMISSION_STOP)
1140 else if (emission_state == EMISSION_RESTART)
1144 if (node->emission_hooks)
1146 emission_state = EMISSION_HOOK;
1148 G_UNLOCK (g_signal_mutex);
1149 g_print ("emission_hooks()\n");
1150 G_LOCK (g_signal_mutex);
1152 if (emission_state == EMISSION_RESTART)
1158 Handler *handler = handlers;
1160 emission_state = EMISSION_RUN;
1162 handler_ref (handler);
1167 if (!handler->after && !handler->block_count)
1169 G_UNLOCK (g_signal_mutex);
1173 g_value_reset (&accu);
1174 g_closure_invoke (handler->closure,
1175 (signal_id << 8) | G_SIGNAL_RUN_FIRST,
1178 instance_and_params);
1179 if (!accumulator (signal_id, return_value, &accu) &&
1180 emission_state == EMISSION_RUN)
1181 emission_state = EMISSION_STOP;
1185 g_closure_invoke (handler->closure,
1186 (signal_id << 8) | G_SIGNAL_RUN_FIRST,
1189 instance_and_params);
1190 G_LOCK (g_signal_mutex);
1192 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
1195 tmp = handler->next;
1199 handler_unref_R (signal_id, instance, handler);
1204 if (emission_state == EMISSION_STOP)
1206 else if (emission_state == EMISSION_RESTART)
1210 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
1212 emission_state = EMISSION_RUN;
1214 G_UNLOCK (g_signal_mutex);
1218 g_value_reset (&accu);
1219 g_closure_invoke (class_closure,
1220 (signal_id << 8) | G_SIGNAL_RUN_LAST,
1223 instance_and_params);
1224 if (!accumulator (signal_id, return_value, &accu) &&
1225 emission_state == EMISSION_RUN)
1226 emission_state = EMISSION_STOP;
1230 g_closure_invoke (class_closure,
1231 (signal_id << 8) | G_SIGNAL_RUN_LAST,
1234 instance_and_params);
1235 G_LOCK (g_signal_mutex);
1237 if (emission_state == EMISSION_STOP)
1239 else if (emission_state == EMISSION_RESTART)
1245 Handler *handler = handlers;
1247 emission_state = EMISSION_RUN;
1249 handler_ref (handler);
1254 if (handler->after && !handler->block_count)
1256 G_UNLOCK (g_signal_mutex);
1260 g_value_reset (&accu);
1261 g_closure_invoke (handler->closure,
1262 (signal_id << 8) | G_SIGNAL_RUN_LAST,
1265 instance_and_params);
1266 if (!accumulator (signal_id, return_value, &accu) &&
1267 emission_state == EMISSION_RUN)
1268 emission_state = EMISSION_STOP;
1272 g_closure_invoke (handler->closure,
1273 (signal_id << 8) | G_SIGNAL_RUN_LAST,
1276 instance_and_params);
1277 G_LOCK (g_signal_mutex);
1279 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
1282 tmp = handler->next;
1286 handler_unref_R (signal_id, instance, handler);
1291 if (emission_state == EMISSION_STOP)
1293 else if (emission_state == EMISSION_RESTART)
1299 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
1301 emission_state = EMISSION_STOP;
1303 G_UNLOCK (g_signal_mutex);
1304 if (node->return_type != G_TYPE_NONE)
1307 g_value_init (&accu, node->return_type);
1309 g_value_reset (&accu);
1312 g_closure_invoke (class_closure,
1313 (signal_id << 8) | G_SIGNAL_RUN_CLEANUP,
1314 node->return_type != G_TYPE_NONE ? &accu : NULL,
1316 instance_and_params);
1317 if (node->return_type != G_TYPE_NONE && !accumulator)
1318 g_value_unset (&accu);
1319 G_LOCK (g_signal_mutex);
1321 if (emission_state == EMISSION_RESTART)
1326 handler_unref_R (signal_id, instance, handlers);
1328 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions);
1331 G_UNLOCK (g_signal_mutex);
1332 g_value_unset (&accu);
1333 G_LOCK (g_signal_mutex);