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>
26 #include "gbsearcharray.h"
29 /* pre allocation configurations
31 #define BSA_PRE_ALLOC (20)
32 #define HANDLER_PRE_ALLOC (48)
33 #define EMISSION_PRE_ALLOC (16)
35 #define TIGHT_MEMORY (1)
37 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
40 /* --- generic allocation --- */
41 /* we can special case allocations generically by replacing
42 * these functions with more speed/memory aware variants
44 static inline gpointer
45 g_generic_node_alloc (GTrashStack **trash_stack_p,
47 guint nodes_pre_alloc)
49 gpointer node = g_trash_stack_pop (trash_stack_p);
55 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
56 block = g_malloc (sizeof_node * nodes_pre_alloc);
57 while (--nodes_pre_alloc)
59 g_trash_stack_push (trash_stack_p, block);
68 g_generic_node_free (GTrashStack **trash_stack_p,
71 g_trash_stack_push (trash_stack_p, node);
75 /* --- typedefs --- */
76 typedef struct _SignalNode SignalNode;
77 typedef struct _SignalKey SignalKey;
78 typedef struct _Emission Emission;
79 typedef struct _Handler Handler;
80 typedef struct _HandlerList HandlerList;
90 /* --- prototypes --- */
91 static inline guint signal_id_lookup (GQuark quark,
93 static void signal_destroy_R (SignalNode *signal_node);
94 static inline HandlerList* handler_list_ensure (guint signal_id,
96 static inline HandlerList* handler_list_lookup (guint signal_id,
98 static inline Handler* handler_new (gboolean after);
99 static void handler_insert (guint signal_id,
102 static Handler* handler_lookup (gpointer instance,
105 static Handler* handler_find (gpointer instance,
106 GSignalMatchType mask,
112 static inline void handler_ref (Handler *handler);
113 static inline void handler_unref_R (guint signal_id,
116 static inline void emission_push (Emission **emission_list_p,
120 EmissionState *state_p);
121 static inline void emission_pop (Emission **emission_list_p,
122 EmissionState *state_p);
123 static inline Emission* emission_find (Emission *emission_list,
127 static void signal_emit_R (SignalNode *node,
130 GValue *return_value,
131 const GValue *instance_and_params);
134 /* --- structures --- */
137 /* permanent portion */
143 /* reinitializable portion */
148 GClosure *class_closure;
149 GSignalAccumulator accumulator;
150 GSignalCMarshaller c_marshaller;
151 GHookList *emission_hooks;
167 EmissionState *state_p;
182 guint ref_count : 16;
183 #define HANDLER_MAX_REF_COUNT (1 << 16)
184 guint block_count : 12;
185 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
191 /* --- variables --- */
192 static GBSearchArray g_signal_key_bsa = { NULL, 0, 0, 0, NULL };
193 static GHashTable *g_handler_list_bsa_ht = NULL;
194 static Emission *g_recursive_emissions = NULL;
195 static Emission *g_restart_emissions = NULL;
196 static GTrashStack *g_bsa_ts = NULL;
197 static GTrashStack *g_handler_ts = NULL;
198 static GTrashStack *g_emission_ts = NULL;
199 G_LOCK_DEFINE_STATIC (g_signal_mutex);
202 /* --- signal nodes --- */
203 static guint g_n_signal_nodes = 0;
204 static SignalNode **g_signal_nodes = NULL;
206 static inline SignalNode*
207 LOOKUP_SIGNAL_NODE (register guint signal_id)
209 if (signal_id < g_n_signal_nodes)
210 return g_signal_nodes[signal_id];
216 /* --- functions --- */
218 signal_id_lookup (GQuark quark,
223 SignalKey key, *signal_key;
228 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
231 return signal_key->signal_id;
233 itype = g_type_parent (itype);
241 handler_lists_cmp (gconstpointer node1,
244 const HandlerList *hlist1 = node1, *hlist2 = node2;
246 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
249 static inline HandlerList*
250 handler_list_ensure (guint signal_id,
253 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
258 hlbsa = g_generic_node_alloc (&g_bsa_ts,
259 sizeof (GBSearchArray),
261 hlbsa->cmp_func = handler_lists_cmp;
262 hlbsa->sizeof_node = sizeof (HandlerList);
263 hlbsa->flags = G_BSEARCH_DEFER_SHRINK;
266 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
268 key.signal_id = signal_id;
271 return g_bsearch_array_insert (hlbsa, &key, FALSE);
274 static inline HandlerList*
275 handler_list_lookup (guint signal_id,
278 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
281 key.signal_id = signal_id;
283 return hlbsa ? g_bsearch_array_lookup (hlbsa, &key) : NULL;
287 handler_lookup (gpointer instance,
291 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
297 for (i = 0; i < hlbsa->n_nodes; i++)
299 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
302 for (handler = hlist->handlers; handler; handler = handler->next)
303 if (handler->id == handler_id)
306 *signal_id_p = hlist->signal_id;
317 handler_find (gpointer instance,
318 GSignalMatchType mask,
325 if (mask & G_SIGNAL_MATCH_ID)
327 HandlerList *hlist = handler_list_lookup (signal_id, instance);
331 if (mask & G_SIGNAL_MATCH_FUNC)
333 node = LOOKUP_SIGNAL_NODE (signal_id);
334 if (!node || !node->c_marshaller)
339 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
340 if (((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
341 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
342 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
343 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
344 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
345 handler->closure->meta_marshal == 0 &&
346 ((GCClosure*) handler->closure)->callback == func)))
351 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
358 for (i = 0; i < hlbsa->n_nodes; i++)
360 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
364 if (!(mask & G_SIGNAL_MATCH_FUNC))
366 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
367 if (!node->c_marshaller)
371 for (handler = hlist->handlers; handler; handler = handler->next)
372 if (((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
373 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
374 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
375 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
376 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
377 handler->closure->meta_marshal == 0 &&
378 ((GCClosure*) handler->closure)->callback == func)))
387 static inline Handler*
388 handler_new (gboolean after)
390 static guint handler_id = 1;
391 Handler *handler = g_generic_node_alloc (&g_handler_ts,
394 #ifndef G_DISABLE_CHECKS
396 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
399 handler->id = handler_id++;
400 handler->prev = NULL;
401 handler->next = NULL;
403 handler->ref_count = 1;
404 handler->block_count = 0;
405 handler->after = after != FALSE;
406 handler->closure = NULL;
412 handler_ref (Handler *handler)
414 g_return_if_fail (handler->ref_count > 0);
416 #ifndef G_DISABLE_CHECKS
417 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
418 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
421 handler->ref_count += 1;
425 handler_unref_R (guint signal_id,
429 g_return_if_fail (handler->ref_count > 0);
431 handler->ref_count -= 1;
432 if (!handler->ref_count)
435 handler->next->prev = handler->prev;
436 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
437 handler->prev->next = handler->next;
440 HandlerList *hlist = handler_list_lookup (signal_id, instance);
442 hlist->handlers = handler->next;
444 G_UNLOCK (g_signal_mutex);
445 g_closure_unref (handler->closure);
446 G_LOCK (g_signal_mutex);
447 g_generic_node_free (&g_handler_ts, handler);
452 handler_insert (guint signal_id,
458 g_assert (handler->prev == NULL && handler->next == NULL); // FIXME: paranoid
460 hlist = handler_list_ensure (signal_id, instance);
461 if (!hlist->handlers)
462 hlist->handlers = handler;
463 else if (hlist->handlers->after && !handler->after)
465 handler->next = hlist->handlers;
466 hlist->handlers->prev = handler;
467 hlist->handlers = handler;
471 Handler *tmp = hlist->handlers;
477 while (tmp->next && !tmp->next->after)
480 tmp->next->prev = handler;
481 handler->next = tmp->next;
488 emission_push (Emission **emission_list_p,
492 EmissionState *state_p)
494 Emission *emission = g_generic_node_alloc (&g_emission_ts,
497 emission->next = *emission_list_p;
498 emission->signal_id = signal_id;
499 emission->detail = detail;
500 emission->instance = instance;
501 emission->state_p = state_p;
502 *emission_list_p = emission;
506 emission_pop (Emission **emission_list_p,
507 EmissionState *state_p)
509 Emission **loc = emission_list_p, *emission = *loc;
511 while (emission->state_p != state_p)
513 loc = &emission->next;
516 *loc = emission->next;
517 g_generic_node_free (&g_emission_ts, emission);
520 static inline Emission*
521 emission_find (Emission *emission_list,
528 for (emission = emission_list; emission; emission = emission->next)
529 if (emission->instance == instance &&
530 emission->signal_id == signal_id &&
531 emission->detail == detail)
537 signal_key_cmp (gconstpointer node1,
540 const SignalKey *key1 = node1, *key2 = node2;
542 if (key1->itype == key2->itype)
543 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
545 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
549 g_signal_init (void) /* sync with gtype.c */
551 G_LOCK (g_signal_mutex);
552 if (!g_n_signal_nodes)
554 /* setup signal key array */
555 g_signal_key_bsa.cmp_func = signal_key_cmp;
556 g_signal_key_bsa.sizeof_node = sizeof (SignalKey);
557 g_signal_key_bsa.flags = 0; /* alloc-only */
559 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
560 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
562 /* invalid (0) signal_id */
563 g_n_signal_nodes = 1;
564 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
565 g_signal_nodes[0] = NULL;
567 G_UNLOCK (g_signal_mutex);
571 g_signals_destroy (GType itype)
574 gboolean found_one = FALSE;
576 G_LOCK (g_signal_mutex);
577 for (i = 0; i < g_n_signal_nodes; i++)
579 SignalNode *node = g_signal_nodes[i];
581 if (node->itype == itype)
584 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
586 g_type_name (node->itype));
590 signal_destroy_R (node);
595 g_warning (G_STRLOC ": type `%s' has no signals that could be destroyed",
596 g_type_name (itype));
597 G_UNLOCK (g_signal_mutex);
601 g_signal_stop_emission (gpointer instance,
607 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
608 g_return_if_fail (signal_id > 0);
610 G_LOCK (g_signal_mutex);
611 node = LOOKUP_SIGNAL_NODE (signal_id);
612 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
614 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
615 G_UNLOCK (g_signal_mutex);
618 if (node && g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
620 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
621 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
625 if (*emission->state_p == EMISSION_HOOK)
626 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
627 node->name, instance);
628 else if (*emission->state_p == EMISSION_RUN)
629 *emission->state_p = EMISSION_STOP;
632 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
633 node->name, instance);
636 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
637 G_UNLOCK (g_signal_mutex);
641 g_signal_lookup (const gchar *name,
646 g_return_val_if_fail (name != NULL, 0);
647 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
649 G_LOCK (g_signal_mutex);
650 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
651 G_UNLOCK (g_signal_mutex);
657 g_signal_name (guint signal_id)
662 G_LOCK (g_signal_mutex);
663 node = LOOKUP_SIGNAL_NODE (signal_id);
664 name = node ? node->name : NULL;
665 G_UNLOCK (g_signal_mutex);
671 g_signal_query (guint signal_id,
676 g_return_if_fail (query != NULL);
678 G_LOCK (g_signal_mutex);
679 node = LOOKUP_SIGNAL_NODE (signal_id);
680 if (!node || node->destroyed)
681 query->signal_id = 0;
684 query->signal_id = node->signal_id;
685 query->signal_name = node->name;
686 query->itype = node->itype;
687 query->signal_flags = node->flags;
688 query->return_type = node->return_type;
689 query->n_params = node->n_params;
690 query->param_types = node->param_types;
692 G_UNLOCK (g_signal_mutex);
698 * @n_ids: location to store number of ids in @itype
700 * List all signals for a given type.
702 * Return value: Array
705 g_type_signals (GType itype,
713 g_return_val_if_fail (n_ids != NULL, NULL);
714 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
716 keys = g_signal_key_bsa.nodes;
717 n_nodes = g_signal_key_bsa.n_nodes;
718 result = g_array_new (FALSE, FALSE, sizeof (guint));
720 for (i = 0; i < n_nodes; i++)
722 if (keys[i].itype == itype)
724 gchar *name = g_quark_to_string (keys[i].quark);
725 /* Signal names with "_" in them are aliases to the same
726 * name with "-" instead of "_".
728 if (!strchr (name, '_'))
729 g_array_append_val (result, keys[i].signal_id);
733 *n_ids = result->len;
735 return (guint *) g_array_free (result, FALSE);
739 g_signal_newv (const gchar *signal_name,
741 GSignalFlags signal_flags,
742 GClosure *class_closure,
743 GSignalAccumulator accumulator,
744 GSignalCMarshaller c_marshaller,
753 g_return_val_if_fail (signal_name != NULL, 0);
754 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
756 g_return_val_if_fail (param_types != NULL, 0);
757 if (return_type != G_TYPE_NONE)
758 g_return_val_if_fail (accumulator == NULL, 0);
760 name = g_strdup (signal_name);
761 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); // FIXME do character checks like for types
763 G_LOCK (g_signal_mutex);
765 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
766 node = LOOKUP_SIGNAL_NODE (signal_id);
767 if (node && !node->destroyed)
769 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
771 g_type_name (node->itype),
772 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
774 G_UNLOCK (g_signal_mutex);
777 if (node && node->itype != itype)
779 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
782 g_type_name (node->itype));
784 G_UNLOCK (g_signal_mutex);
787 for (i = 0; i < n_params; i++)
788 if (!G_TYPE_IS_VALUE (param_types[i]) ||
789 param_types[i] == G_TYPE_ENUM || param_types[i] == G_TYPE_FLAGS) /* FIXME: kludge */
791 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
792 i + 1, g_type_name (param_types[i]), g_type_name (itype), name);
794 G_UNLOCK (g_signal_mutex);
797 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type))
799 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
800 g_type_name (param_types[i]), g_type_name (itype), name);
802 G_UNLOCK (g_signal_mutex);
806 /* setup permanent portion of signal node */
811 signal_id = g_n_signal_nodes++;
812 node = g_new (SignalNode, 1);
813 node->signal_id = signal_id;
814 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
815 g_signal_nodes[signal_id] = node;
819 key.quark = g_quark_from_string (node->name);
820 key.signal_id = signal_id;
821 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
822 g_strdelimit (node->name, "_", '-');
823 key.quark = g_quark_from_static_string (node->name);
824 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
826 node->destroyed = FALSE;
828 /* setup reinitializable portion */
829 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
830 node->n_params = n_params;
831 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
832 node->return_type = return_type;
833 node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
834 node->accumulator = accumulator;
835 node->c_marshaller = c_marshaller;
836 node->emission_hooks = NULL;
837 if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
838 g_closure_set_marshal (class_closure, node->c_marshaller);
840 G_UNLOCK (g_signal_mutex);
845 signal_destroy_R (SignalNode *signal_node)
847 SignalNode node = *signal_node;
849 signal_node->destroyed = TRUE;
851 /* reentrancy caution, zero out real contents first */
852 signal_node->n_params = 0;
853 signal_node->param_types = NULL;
854 signal_node->return_type = 0;
855 signal_node->class_closure = NULL;
856 signal_node->accumulator = NULL;
857 signal_node->c_marshaller = NULL;
858 signal_node->emission_hooks = NULL;
860 #ifndef G_DISABLE_CHECKS
861 /* check current emissions */
865 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
866 emission; emission = emission->next)
867 if (emission->signal_id == node.signal_id)
868 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
869 node.name, emission->instance);
873 /* free contents that need to
875 G_UNLOCK (g_signal_mutex);
876 g_free (node.param_types);
877 g_closure_unref (node.class_closure);
878 if (node.emission_hooks)
880 g_hook_list_clear (node.emission_hooks);
881 g_free (node.emission_hooks);
883 G_LOCK (g_signal_mutex);
887 g_signal_connect_closure (gpointer instance,
894 guint handler_id = 0;
896 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
897 g_return_val_if_fail (signal_id > 0, 0);
898 g_return_val_if_fail (closure != NULL, 0);
900 G_LOCK (g_signal_mutex);
901 node = LOOKUP_SIGNAL_NODE (signal_id);
902 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
904 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
905 G_UNLOCK (g_signal_mutex);
908 if (node && g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
910 Handler *handler = handler_new (after);
912 handler_id = handler->id;
913 handler->detail = detail;
914 handler->closure = g_closure_ref (closure);
915 handler_insert (signal_id, instance, handler);
916 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
917 g_closure_set_marshal (closure, node->c_marshaller);
920 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
921 G_UNLOCK (g_signal_mutex);
927 g_signal_handler_disconnect (gpointer instance,
933 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
934 g_return_if_fail (handler_id > 0);
936 G_LOCK (g_signal_mutex);
937 handler = handler_lookup (instance, handler_id, &signal_id);
941 handler->block_count = 1;
942 handler_unref_R (signal_id, instance, handler);
945 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
946 G_UNLOCK (g_signal_mutex);
950 g_signal_handlers_destroy (gpointer instance)
952 GBSearchArray *hlbsa;
954 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
956 G_LOCK (g_signal_mutex);
957 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
962 /* reentrancy caution, delete instance trace first */
963 g_hash_table_remove (g_handler_list_bsa_ht, instance);
965 for (i = 0; i < hlbsa->n_nodes; i++)
967 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
968 Handler *handler = hlist->handlers;
972 Handler *tmp = handler;
975 tmp->block_count = 1;
976 /* cruel unlink, this works because _all_ handlers vanish */
982 handler_unref_R (0, NULL, tmp);
986 g_free (hlbsa->nodes);
987 g_generic_node_free (&g_bsa_ts, hlbsa);
989 G_UNLOCK (g_signal_mutex);
993 g_signal_handler_block (gpointer instance,
998 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
999 g_return_if_fail (handler_id > 0);
1001 G_LOCK (g_signal_mutex);
1002 handler = handler_lookup (instance, handler_id, NULL);
1005 #ifndef G_DISABLE_CHECKS
1006 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1007 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1010 handler->block_count += 1;
1013 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1014 G_UNLOCK (g_signal_mutex);
1018 g_signal_handler_unblock (gpointer instance,
1023 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1024 g_return_if_fail (handler_id > 0);
1026 G_LOCK (g_signal_mutex);
1027 handler = handler_lookup (instance, handler_id, NULL);
1030 if (handler->block_count)
1031 handler->block_count -= 1;
1033 g_warning (G_STRLOC ": handler `%u' of instance `%p' is not blocked", handler_id, instance);
1036 g_warning ("%s: instance `%p' has no handler with id `%u'", G_STRLOC, instance, handler_id);
1037 G_UNLOCK (g_signal_mutex);
1041 g_signal_handler_find (gpointer instance,
1042 GSignalMatchType mask,
1049 Handler *handler = NULL;
1052 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1053 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1055 G_LOCK (g_signal_mutex);
1056 handler = handler_find (instance, mask, signal_id, detail, closure, func, data);
1057 handler_id = handler ? handler->id : 0;
1058 G_UNLOCK (g_signal_mutex);
1064 g_signal_handler_pending (gpointer instance,
1067 gboolean may_be_blocked)
1069 Handler *handler = NULL;
1071 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1072 g_return_val_if_fail (signal_id > 0, FALSE);
1074 G_LOCK (g_signal_mutex);
1077 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1079 if (!(node->flags & G_SIGNAL_DETAILED))
1081 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1082 G_UNLOCK (g_signal_mutex);
1086 handler = handler_find (instance,
1087 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL |
1088 (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1089 signal_id, detail, NULL, NULL, NULL);
1090 G_UNLOCK (g_signal_mutex);
1092 return handler != NULL;
1096 g_signal_emitv (const GValue *instance_and_params,
1099 GValue *return_value)
1103 const GValue *param_values;
1106 g_return_if_fail (instance_and_params != NULL);
1107 instance = g_value_get_as_pointer (instance_and_params);
1108 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1109 g_return_if_fail (signal_id > 0);
1111 param_values = instance_and_params + 1;
1113 G_LOCK (g_signal_mutex);
1114 node = LOOKUP_SIGNAL_NODE (signal_id);
1115 #ifndef G_DISABLE_CHECKS
1116 if (!node || !g_type_conforms_to (G_TYPE_FROM_INSTANCE (instance), node->itype))
1117 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1118 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1120 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1121 G_UNLOCK (g_signal_mutex);
1124 for (i = 0; i < node->n_params; i++)
1125 if (!G_VALUE_HOLDS (param_values + i, node->param_types[i]))
1127 g_critical (G_STRLOC ": value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1128 g_type_name (node->param_types[i]),
1131 G_VALUE_TYPE_NAME (param_values + i));
1132 G_UNLOCK (g_signal_mutex);
1135 if (node->return_type != G_TYPE_NONE)
1139 g_critical (G_STRLOC ": return value `%s' for signal \"%s\" is (NULL)",
1140 g_type_name (node->return_type),
1142 G_UNLOCK (g_signal_mutex);
1145 else if (!node->accumulator && !G_VALUE_HOLDS (return_value, node->return_type))
1147 g_critical (G_STRLOC ": return value `%s' for signal \"%s\" is of type `%s'",
1148 g_type_name (node->return_type),
1150 G_VALUE_TYPE_NAME (return_value));
1151 G_UNLOCK (g_signal_mutex);
1156 return_value = NULL;
1157 #endif /* !G_DISABLE_CHECKS */
1159 signal_emit_R (node, detail, instance, return_value, instance_and_params);
1161 G_UNLOCK (g_signal_mutex);
1165 signal_emit_R (SignalNode *node,
1168 GValue *return_value,
1169 const GValue *instance_and_params)
1171 EmissionState emission_state = 0;
1172 GSignalAccumulator accumulator;
1173 GSignalInvocationHint ihint;
1174 GClosure *class_closure;
1176 Handler *handler_list = NULL;
1178 gboolean accu_used = FALSE;
1179 guint signal_id = node->signal_id;
1181 if (node->flags & G_SIGNAL_NO_RECURSE)
1183 Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
1187 *emission->state_p = EMISSION_RESTART;
1191 ihint.signal_id = node->signal_id;
1192 ihint.detail = detail;
1193 accumulator = node->accumulator;
1196 G_UNLOCK (g_signal_mutex);
1197 g_value_init (&accu, node->return_type);
1198 G_LOCK (g_signal_mutex);
1200 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
1201 signal_id, detail, instance, &emission_state);
1202 class_closure = node->class_closure;
1207 handler_unref_R (signal_id, instance, handler_list);
1208 hlist = handler_list_lookup (signal_id, instance);
1209 handler_list = hlist ? hlist->handlers : NULL;
1211 handler_ref (handler_list);
1213 ihint.run_type = G_SIGNAL_RUN_FIRST;
1215 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
1217 emission_state = EMISSION_RUN;
1219 G_UNLOCK (g_signal_mutex);
1223 g_value_reset (&accu);
1224 g_closure_invoke (class_closure,
1227 instance_and_params,
1229 if (!accumulator (&ihint, return_value, &accu) &&
1230 emission_state == EMISSION_RUN)
1231 emission_state = EMISSION_STOP;
1235 g_closure_invoke (class_closure,
1238 instance_and_params,
1240 G_LOCK (g_signal_mutex);
1242 if (emission_state == EMISSION_STOP)
1244 else if (emission_state == EMISSION_RESTART)
1248 if (node->emission_hooks)
1250 emission_state = EMISSION_HOOK;
1252 G_UNLOCK (g_signal_mutex);
1253 g_print ("emission_hooks()\n");
1254 G_LOCK (g_signal_mutex);
1256 if (emission_state == EMISSION_RESTART)
1262 Handler *handler = handler_list;
1264 emission_state = EMISSION_RUN;
1265 handler_ref (handler);
1272 handler_unref_R (signal_id, instance, handler_list);
1273 handler_list = handler;
1276 else if (!handler->block_count && (!handler->detail || handler->detail == detail))
1278 G_UNLOCK (g_signal_mutex);
1282 g_value_reset (&accu);
1283 g_closure_invoke (handler->closure,
1286 instance_and_params,
1288 if (!accumulator (&ihint, return_value, &accu) &&
1289 emission_state == EMISSION_RUN)
1290 emission_state = EMISSION_STOP;
1294 g_closure_invoke (handler->closure,
1297 instance_and_params,
1299 G_LOCK (g_signal_mutex);
1301 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
1304 tmp = handler->next;
1308 handler_unref_R (signal_id, instance, handler_list);
1309 handler_list = handler;
1314 if (emission_state == EMISSION_STOP)
1316 else if (emission_state == EMISSION_RESTART)
1320 ihint.run_type = G_SIGNAL_RUN_LAST;
1322 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
1324 emission_state = EMISSION_RUN;
1326 G_UNLOCK (g_signal_mutex);
1330 g_value_reset (&accu);
1331 g_closure_invoke (class_closure,
1334 instance_and_params,
1336 if (!accumulator (&ihint, return_value, &accu) &&
1337 emission_state == EMISSION_RUN)
1338 emission_state = EMISSION_STOP;
1342 g_closure_invoke (class_closure,
1345 instance_and_params,
1347 G_LOCK (g_signal_mutex);
1349 if (emission_state == EMISSION_STOP)
1351 else if (emission_state == EMISSION_RESTART)
1357 Handler *handler = handler_list;
1359 emission_state = EMISSION_RUN;
1360 handler_ref (handler);
1365 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail))
1367 G_UNLOCK (g_signal_mutex);
1371 g_value_reset (&accu);
1372 g_closure_invoke (handler->closure,
1375 instance_and_params,
1377 if (!accumulator (&ihint, return_value, &accu) &&
1378 emission_state == EMISSION_RUN)
1379 emission_state = EMISSION_STOP;
1383 g_closure_invoke (handler->closure,
1386 instance_and_params,
1388 G_LOCK (g_signal_mutex);
1390 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
1393 tmp = handler->next;
1397 handler_unref_R (signal_id, instance, handler);
1402 if (emission_state == EMISSION_STOP)
1404 else if (emission_state == EMISSION_RESTART)
1410 ihint.run_type = G_SIGNAL_RUN_CLEANUP;
1412 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
1414 gboolean need_unset = FALSE;
1416 emission_state = EMISSION_STOP;
1418 G_UNLOCK (g_signal_mutex);
1419 if (node->return_type != G_TYPE_NONE)
1423 g_value_init (&accu, node->return_type);
1427 g_value_reset (&accu);
1429 g_closure_invoke (class_closure,
1430 node->return_type != G_TYPE_NONE ? &accu : NULL,
1432 instance_and_params,
1435 g_value_unset (&accu);
1436 G_LOCK (g_signal_mutex);
1438 if (emission_state == EMISSION_RESTART)
1443 handler_unref_R (signal_id, instance, handler_list);
1445 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
1448 G_UNLOCK (g_signal_mutex);
1449 g_value_unset (&accu);
1450 G_LOCK (g_signal_mutex);