This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsignal.c
3476 lines (3091 loc) · 106 KB
/
gsignal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GObject - GLib Type, Object, Parameter and Signal Library
* Copyright (C) 2000-2001 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* this code is based on the original GtkSignal implementation
* for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
*/
/*
* MT safe
*/
#include "config.h"
#include <string.h>
#include <signal.h>
#include "gsignal.h"
#include "gbsearcharray.h"
#include "gvaluecollector.h"
#include "gvaluetypes.h"
#include "gboxed.h"
#include "gobject.h"
#include "genums.h"
#include "gobject_trace.h"
/**
* SECTION:signals
* @short_description: A means for customization of object behaviour
* and a general purpose notification mechanism
* @title: Signals
*
* The basic concept of the signal system is that of the
* <emphasis>emission</emphasis> of a signal. Signals are introduced
* per-type and are identified through strings. Signals introduced
* for a parent type are available in derived types as well, so
* basically they are a per-type facility that is inherited. A signal
* emission mainly involves invocation of a certain set of callbacks
* in precisely defined manner. There are two main categories of such
* callbacks, per-object
* <footnote><para>Although signals can deal with any kind of instantiatable
* type, i'm referring to those types as "object types" in the following,
* simply because that is the context most users will encounter signals in.
* </para></footnote>
* ones and user provided ones.
* The per-object callbacks are most often referred to as "object method
* handler" or "default (signal) handler", while user provided callbacks are
* usually just called "signal handler".
* The object method handler is provided at signal creation time (this most
* frequently happens at the end of an object class' creation), while user
* provided handlers are frequently connected and disconnected to/from a certain
* signal on certain object instances.
*
* A signal emission consists of five stages, unless prematurely stopped:
* <variablelist>
* <varlistentry><term></term><listitem><para>
* 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
* </para></listitem></varlistentry>
* <varlistentry><term></term><listitem><para>
* 2 - Invocation of normal user-provided signal handlers (<emphasis>after</emphasis> flag %FALSE)
* </para></listitem></varlistentry>
* <varlistentry><term></term><listitem><para>
* 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
* </para></listitem></varlistentry>
* <varlistentry><term></term><listitem><para>
* 4 - Invocation of user provided signal handlers, connected with an <emphasis>after</emphasis> flag of %TRUE
* </para></listitem></varlistentry>
* <varlistentry><term></term><listitem><para>
* 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
* </para></listitem></varlistentry>
* </variablelist>
* The user-provided signal handlers are called in the order they were
* connected in.
* All handlers may prematurely stop a signal emission, and any number of
* handlers may be connected, disconnected, blocked or unblocked during
* a signal emission.
* There are certain criteria for skipping user handlers in stages 2 and 4
* of a signal emission.
* First, user handlers may be <emphasis>blocked</emphasis>, blocked handlers are omitted
* during callback invocation, to return from the "blocked" state, a
* handler has to get unblocked exactly the same amount of times
* it has been blocked before.
* Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
* "detail" argument passed in to g_signal_emit() has to match the detail
* argument of the signal handler currently subject to invocation.
* Specification of no detail argument for signal handlers (omission of the
* detail part of the signal specification upon connection) serves as a
* wildcard and matches any detail argument passed in to emission.
*/
#define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
#ifdef G_ENABLE_DEBUG
#define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
static volatile gpointer g_trace_instance_signals = NULL;
static volatile gpointer g_trap_instance_signals = NULL;
#endif /* G_ENABLE_DEBUG */
/* --- typedefs --- */
typedef struct _SignalNode SignalNode;
typedef struct _SignalKey SignalKey;
typedef struct _Emission Emission;
typedef struct _Handler Handler;
typedef struct _HandlerList HandlerList;
typedef struct _HandlerMatch HandlerMatch;
typedef enum
{
EMISSION_STOP,
EMISSION_RUN,
EMISSION_HOOK,
EMISSION_RESTART
} EmissionState;
/* --- prototypes --- */
static inline guint signal_id_lookup (GQuark quark,
GType itype);
static void signal_destroy_R (SignalNode *signal_node);
static inline HandlerList* handler_list_ensure (guint signal_id,
gpointer instance);
static inline HandlerList* handler_list_lookup (guint signal_id,
gpointer instance);
static inline Handler* handler_new (gboolean after);
static void handler_insert (guint signal_id,
gpointer instance,
Handler *handler);
static Handler* handler_lookup (gpointer instance,
gulong handler_id,
guint *signal_id_p);
static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
Handler *handler,
guint signal_id);
static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
gpointer instance);
static HandlerMatch* handlers_find (gpointer instance,
GSignalMatchType mask,
guint signal_id,
GQuark detail,
GClosure *closure,
gpointer func,
gpointer data,
gboolean one_and_only);
static inline void handler_ref (Handler *handler);
static inline void handler_unref_R (guint signal_id,
gpointer instance,
Handler *handler);
static gint handler_lists_cmp (gconstpointer node1,
gconstpointer node2);
static inline void emission_push (Emission **emission_list_p,
Emission *emission);
static inline void emission_pop (Emission **emission_list_p,
Emission *emission);
static inline Emission* emission_find (Emission *emission_list,
guint signal_id,
GQuark detail,
gpointer instance);
static gint class_closures_cmp (gconstpointer node1,
gconstpointer node2);
static gint signal_key_cmp (gconstpointer node1,
gconstpointer node2);
static gboolean signal_emit_unlocked_R (SignalNode *node,
GQuark detail,
gpointer instance,
GValue *return_value,
const GValue *instance_and_params);
static const gchar * type_debug_name (GType type);
/* --- structures --- */
typedef struct
{
GSignalAccumulator func;
gpointer data;
} SignalAccumulator;
typedef struct
{
GHook hook;
GQuark detail;
} SignalHook;
#define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
struct _SignalNode
{
/* permanent portion */
guint signal_id;
GType itype;
const gchar *name;
guint destroyed : 1;
/* reinitializable portion */
guint test_class_offset : 12;
guint flags : 8;
guint n_params : 8;
GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
GBSearchArray *class_closure_bsa;
SignalAccumulator *accumulator;
GSignalCMarshaller c_marshaller;
GHookList *emission_hooks;
};
#define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
#define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
struct _SignalKey
{
GType itype;
GQuark quark;
guint signal_id;
};
struct _Emission
{
Emission *next;
gpointer instance;
GSignalInvocationHint ihint;
EmissionState state;
GType chain_type;
};
struct _HandlerList
{
guint signal_id;
Handler *handlers;
Handler *tail_before; /* normal signal handlers are appended here */
Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
};
struct _Handler
{
gulong sequential_number;
Handler *next;
Handler *prev;
GQuark detail;
guint ref_count;
guint block_count : 16;
#define HANDLER_MAX_BLOCK_COUNT (1 << 16)
guint after : 1;
GClosure *closure;
};
struct _HandlerMatch
{
Handler *handler;
HandlerMatch *next;
guint signal_id;
};
typedef struct
{
GType instance_type; /* 0 for default closure */
GClosure *closure;
} ClassClosure;
/* --- variables --- */
static GBSearchArray *g_signal_key_bsa = NULL;
static const GBSearchConfig g_signal_key_bconfig = {
sizeof (SignalKey),
signal_key_cmp,
G_BSEARCH_ARRAY_ALIGN_POWER2,
};
static GBSearchConfig g_signal_hlbsa_bconfig = {
sizeof (HandlerList),
handler_lists_cmp,
0,
};
static GBSearchConfig g_class_closure_bconfig = {
sizeof (ClassClosure),
class_closures_cmp,
0,
};
static GHashTable *g_handler_list_bsa_ht = NULL;
static Emission *g_recursive_emissions = NULL;
static Emission *g_restart_emissions = NULL;
static gulong g_handler_sequential_number = 1;
G_LOCK_DEFINE_STATIC (g_signal_mutex);
#define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
#define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
/* --- signal nodes --- */
static guint g_n_signal_nodes = 0;
static SignalNode **g_signal_nodes = NULL;
static inline SignalNode*
LOOKUP_SIGNAL_NODE (register guint signal_id)
{
if (signal_id < g_n_signal_nodes)
return g_signal_nodes[signal_id];
else
return NULL;
}
/* --- functions --- */
static inline guint
signal_id_lookup (GQuark quark,
GType itype)
{
GType *ifaces, type = itype;
SignalKey key;
guint n_ifaces;
key.quark = quark;
/* try looking up signals for this type and its ancestors */
do
{
SignalKey *signal_key;
key.itype = type;
signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
if (signal_key)
return signal_key->signal_id;
type = g_type_parent (type);
}
while (type);
/* no luck, try interfaces it exports */
ifaces = g_type_interfaces (itype, &n_ifaces);
while (n_ifaces--)
{
SignalKey *signal_key;
key.itype = ifaces[n_ifaces];
signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
if (signal_key)
{
g_free (ifaces);
return signal_key->signal_id;
}
}
g_free (ifaces);
return 0;
}
static gint
class_closures_cmp (gconstpointer node1,
gconstpointer node2)
{
const ClassClosure *c1 = node1, *c2 = node2;
return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
}
static gint
handler_lists_cmp (gconstpointer node1,
gconstpointer node2)
{
const HandlerList *hlist1 = node1, *hlist2 = node2;
return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
}
static inline HandlerList*
handler_list_ensure (guint signal_id,
gpointer instance)
{
GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
HandlerList key;
key.signal_id = signal_id;
key.handlers = NULL;
key.tail_before = NULL;
key.tail_after = NULL;
if (!hlbsa)
{
hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
}
else
{
GBSearchArray *o = hlbsa;
hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
if (hlbsa != o)
g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
}
return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
}
static inline HandlerList*
handler_list_lookup (guint signal_id,
gpointer instance)
{
GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
HandlerList key;
key.signal_id = signal_id;
return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
}
static Handler*
handler_lookup (gpointer instance,
gulong handler_id,
guint *signal_id_p)
{
GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
if (hlbsa)
{
guint i;
for (i = 0; i < hlbsa->n_nodes; i++)
{
HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
Handler *handler;
for (handler = hlist->handlers; handler; handler = handler->next)
if (handler->sequential_number == handler_id)
{
if (signal_id_p)
*signal_id_p = hlist->signal_id;
return handler;
}
}
}
return NULL;
}
static inline HandlerMatch*
handler_match_prepend (HandlerMatch *list,
Handler *handler,
guint signal_id)
{
HandlerMatch *node;
node = g_slice_new (HandlerMatch);
node->handler = handler;
node->next = list;
node->signal_id = signal_id;
handler_ref (handler);
return node;
}
static inline HandlerMatch*
handler_match_free1_R (HandlerMatch *node,
gpointer instance)
{
HandlerMatch *next = node->next;
handler_unref_R (node->signal_id, instance, node->handler);
g_slice_free (HandlerMatch, node);
return next;
}
static HandlerMatch*
handlers_find (gpointer instance,
GSignalMatchType mask,
guint signal_id,
GQuark detail,
GClosure *closure,
gpointer func,
gpointer data,
gboolean one_and_only)
{
HandlerMatch *mlist = NULL;
if (mask & G_SIGNAL_MATCH_ID)
{
HandlerList *hlist = handler_list_lookup (signal_id, instance);
Handler *handler;
SignalNode *node = NULL;
if (mask & G_SIGNAL_MATCH_FUNC)
{
node = LOOKUP_SIGNAL_NODE (signal_id);
if (!node || !node->c_marshaller)
return NULL;
}
mask = ~mask;
for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
if (handler->sequential_number &&
((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
handler->closure->meta_marshal == 0 &&
((GCClosure*) handler->closure)->callback == func)))
{
mlist = handler_match_prepend (mlist, handler, signal_id);
if (one_and_only)
return mlist;
}
}
else
{
GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
mask = ~mask;
if (hlbsa)
{
guint i;
for (i = 0; i < hlbsa->n_nodes; i++)
{
HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
SignalNode *node = NULL;
Handler *handler;
if (!(mask & G_SIGNAL_MATCH_FUNC))
{
node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
if (!node->c_marshaller)
continue;
}
for (handler = hlist->handlers; handler; handler = handler->next)
if (handler->sequential_number &&
((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
handler->closure->meta_marshal == 0 &&
((GCClosure*) handler->closure)->callback == func)))
{
mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
if (one_and_only)
return mlist;
}
}
}
}
return mlist;
}
static inline Handler*
handler_new (gboolean after)
{
Handler *handler = g_slice_new (Handler);
#ifndef G_DISABLE_CHECKS
if (g_handler_sequential_number < 1)
g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
#endif
handler->sequential_number = g_handler_sequential_number++;
handler->prev = NULL;
handler->next = NULL;
handler->detail = 0;
handler->ref_count = 1;
handler->block_count = 0;
handler->after = after != FALSE;
handler->closure = NULL;
return handler;
}
static inline void
handler_ref (Handler *handler)
{
g_return_if_fail (handler->ref_count > 0);
g_atomic_int_inc ((int *)&handler->ref_count);
}
static inline void
handler_unref_R (guint signal_id,
gpointer instance,
Handler *handler)
{
gboolean is_zero;
g_return_if_fail (handler->ref_count > 0);
is_zero = g_atomic_int_dec_and_test ((int *)&handler->ref_count);
if (G_UNLIKELY (is_zero))
{
HandlerList *hlist = NULL;
if (handler->next)
handler->next->prev = handler->prev;
if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
handler->prev->next = handler->next;
else
{
hlist = handler_list_lookup (signal_id, instance);
hlist->handlers = handler->next;
}
if (instance)
{
/* check if we are removing the handler pointed to by tail_before */
if (!handler->after && (!handler->next || handler->next->after))
{
if (!hlist)
hlist = handler_list_lookup (signal_id, instance);
if (hlist)
{
g_assert (hlist->tail_before == handler); /* paranoid */
hlist->tail_before = handler->prev;
}
}
/* check if we are removing the handler pointed to by tail_after */
if (!handler->next)
{
if (!hlist)
hlist = handler_list_lookup (signal_id, instance);
if (hlist)
{
g_assert (hlist->tail_after == handler); /* paranoid */
hlist->tail_after = handler->prev;
}
}
}
SIGNAL_UNLOCK ();
g_closure_unref (handler->closure);
SIGNAL_LOCK ();
g_slice_free (Handler, handler);
}
}
static void
handler_insert (guint signal_id,
gpointer instance,
Handler *handler)
{
HandlerList *hlist;
g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
hlist = handler_list_ensure (signal_id, instance);
if (!hlist->handlers)
{
hlist->handlers = handler;
if (!handler->after)
hlist->tail_before = handler;
}
else if (handler->after)
{
handler->prev = hlist->tail_after;
hlist->tail_after->next = handler;
}
else
{
if (hlist->tail_before)
{
handler->next = hlist->tail_before->next;
if (handler->next)
handler->next->prev = handler;
handler->prev = hlist->tail_before;
hlist->tail_before->next = handler;
}
else /* insert !after handler into a list of only after handlers */
{
handler->next = hlist->handlers;
if (handler->next)
handler->next->prev = handler;
hlist->handlers = handler;
}
hlist->tail_before = handler;
}
if (!handler->next)
hlist->tail_after = handler;
}
static inline void
emission_push (Emission **emission_list_p,
Emission *emission)
{
emission->next = *emission_list_p;
*emission_list_p = emission;
}
static inline void
emission_pop (Emission **emission_list_p,
Emission *emission)
{
Emission *node, *last = NULL;
for (node = *emission_list_p; node; last = node, node = last->next)
if (node == emission)
{
if (last)
last->next = node->next;
else
*emission_list_p = node->next;
return;
}
g_assert_not_reached ();
}
static inline Emission*
emission_find (Emission *emission_list,
guint signal_id,
GQuark detail,
gpointer instance)
{
Emission *emission;
for (emission = emission_list; emission; emission = emission->next)
if (emission->instance == instance &&
emission->ihint.signal_id == signal_id &&
emission->ihint.detail == detail)
return emission;
return NULL;
}
static inline Emission*
emission_find_innermost (gpointer instance)
{
Emission *emission, *s = NULL, *c = NULL;
for (emission = g_restart_emissions; emission; emission = emission->next)
if (emission->instance == instance)
{
s = emission;
break;
}
for (emission = g_recursive_emissions; emission; emission = emission->next)
if (emission->instance == instance)
{
c = emission;
break;
}
if (!s)
return c;
else if (!c)
return s;
else
return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
}
static gint
signal_key_cmp (gconstpointer node1,
gconstpointer node2)
{
const SignalKey *key1 = node1, *key2 = node2;
if (key1->itype == key2->itype)
return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
else
return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
}
void
g_signal_init (void)
{
SIGNAL_LOCK ();
if (!g_n_signal_nodes)
{
/* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
/* invalid (0) signal_id */
g_n_signal_nodes = 1;
g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
g_signal_nodes[0] = NULL;
}
SIGNAL_UNLOCK ();
}
void
_g_signals_destroy (GType itype)
{
guint i;
SIGNAL_LOCK ();
for (i = 1; i < g_n_signal_nodes; i++)
{
SignalNode *node = g_signal_nodes[i];
if (node->itype == itype)
{
if (node->destroyed)
g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
node->name,
type_debug_name (node->itype));
else
signal_destroy_R (node);
}
}
SIGNAL_UNLOCK ();
}
/**
* g_signal_stop_emission:
* @instance: the object whose signal handlers you wish to stop.
* @signal_id: the signal identifier, as returned by g_signal_lookup().
* @detail: the detail which the signal was emitted with.
*
* Stops a signal's current emission.
*
* This will prevent the default method from running, if the signal was
* %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
* flag).
*
* Prints a warning if used on a signal which isn't being emitted.
*/
void
g_signal_stop_emission (gpointer instance,
guint signal_id,
GQuark detail)
{
SignalNode *node;
g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
g_return_if_fail (signal_id > 0);
SIGNAL_LOCK ();
node = LOOKUP_SIGNAL_NODE (signal_id);
if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
{
g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
SIGNAL_UNLOCK ();
return;
}
if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
{
Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
Emission *emission = emission_find (emission_list, signal_id, detail, instance);
if (emission)
{
if (emission->state == EMISSION_HOOK)
g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
node->name, instance);
else if (emission->state == EMISSION_RUN)
emission->state = EMISSION_STOP;
}
else
g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
node->name, instance);
}
else
g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
SIGNAL_UNLOCK ();
}
static void
signal_finalize_hook (GHookList *hook_list,
GHook *hook)
{
GDestroyNotify destroy = hook->destroy;
if (destroy)
{
hook->destroy = NULL;
SIGNAL_UNLOCK ();
destroy (hook->data);
SIGNAL_LOCK ();
}
}
/**
* g_signal_add_emission_hook:
* @signal_id: the signal identifier, as returned by g_signal_lookup().
* @detail: the detail on which to call the hook.
* @hook_func: a #GSignalEmissionHook function.
* @hook_data: user data for @hook_func.
* @data_destroy: a #GDestroyNotify for @hook_data.
*
* Adds an emission hook for a signal, which will get called for any emission
* of that signal, independent of the instance. This is possible only
* for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
*
* Returns: the hook id, for later use with g_signal_remove_emission_hook().
*/
gulong
g_signal_add_emission_hook (guint signal_id,
GQuark detail,
GSignalEmissionHook hook_func,
gpointer hook_data,
GDestroyNotify data_destroy)
{
static gulong seq_hook_id = 1;
SignalNode *node;
GHook *hook;
SignalHook *signal_hook;
g_return_val_if_fail (signal_id > 0, 0);
g_return_val_if_fail (hook_func != NULL, 0);
SIGNAL_LOCK ();
node = LOOKUP_SIGNAL_NODE (signal_id);
if (!node || node->destroyed)
{
g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
SIGNAL_UNLOCK ();
return 0;
}
if (node->flags & G_SIGNAL_NO_HOOKS)
{
g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
SIGNAL_UNLOCK ();
return 0;
}
if (detail && !(node->flags & G_SIGNAL_DETAILED))
{
g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
SIGNAL_UNLOCK ();
return 0;
}
if (!node->emission_hooks)
{
node->emission_hooks = g_new (GHookList, 1);
g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
node->emission_hooks->finalize_hook = signal_finalize_hook;
}
hook = g_hook_alloc (node->emission_hooks);
hook->data = hook_data;
hook->func = (gpointer) hook_func;
hook->destroy = data_destroy;
signal_hook = SIGNAL_HOOK (hook);
signal_hook->detail = detail;
node->emission_hooks->seq_id = seq_hook_id;
g_hook_append (node->emission_hooks, hook);
seq_hook_id = node->emission_hooks->seq_id;
SIGNAL_UNLOCK ();
return hook->hook_id;
}
/**
* g_signal_remove_emission_hook:
* @signal_id: the id of the signal
* @hook_id: the id of the emission hook, as returned by
* g_signal_add_emission_hook()
*
* Deletes an emission hook.
*/
void
g_signal_remove_emission_hook (guint signal_id,
gulong hook_id)
{
SignalNode *node;
g_return_if_fail (signal_id > 0);
g_return_if_fail (hook_id > 0);
SIGNAL_LOCK ();
node = LOOKUP_SIGNAL_NODE (signal_id);
if (!node || node->destroyed)
g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
SIGNAL_UNLOCK ();
}
static inline guint
signal_parse_name (const gchar *name,
GType itype,
GQuark *detail_p,
gboolean force_quark)
{
const gchar *colon = strchr (name, ':');
guint signal_id;
if (!colon)
{
signal_id = signal_id_lookup (g_quark_try_string (name), itype);
if (signal_id && detail_p)
*detail_p = 0;
}
else if (colon[1] == ':')
{
gchar buffer[32];
guint l = colon - name;
if (l < 32)
{
memcpy (buffer, name, l);
buffer[l] = 0;
signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
}
else
{
gchar *signal = g_new (gchar, l + 1);