tests: Mark gdbus-threading test as slow
[glib.git] / glib / ghook.c
blob00187bf79d98e1b5649dcafd8c6c40ec1e9e5eea
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GHook: Callback maintenance functions
5 * Copyright (C) 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
29 * MT safe
32 #include "config.h"
34 #include "ghook.h"
36 #include "gtestutils.h"
37 #include "gslice.h"
39 /**
40 * SECTION:hooks
41 * @title: Hook Functions
42 * @short_description: support for manipulating lists of hook functions
44 * The #GHookList, #GHook and their related functions provide support for
45 * lists of hook functions. Functions can be added and removed from the lists,
46 * and the list of hook functions can be invoked.
49 /**
50 * GHookList:
51 * @seq_id: the next free #GHook id
52 * @hook_size: the size of the #GHookList elements, in bytes
53 * @is_setup: 1 if the #GHookList has been initialized
54 * @hooks: the first #GHook element in the list
55 * @dummy3: unused
56 * @finalize_hook: the function to call to finalize a #GHook element.
57 * The default behaviour is to call the hooks @destroy function
58 * @dummy: unused
60 * The #GHookList struct represents a list of hook functions.
63 /**
64 * GHookFinalizeFunc:
65 * @hook_list: a #GHookList
66 * @hook: the hook in @hook_list that gets finalized
68 * Defines the type of function to be called when a hook in a
69 * list of hooks gets finalized.
72 /**
73 * GHookFlagMask:
74 * @G_HOOK_FLAG_ACTIVE: set if the hook has not been destroyed
75 * @G_HOOK_FLAG_IN_CALL: set if the hook is currently being run
76 * @G_HOOK_FLAG_MASK: A mask covering all bits reserved for
77 * hook flags; see %G_HOOK_FLAG_USER_SHIFT
79 * Flags used internally in the #GHook implementation.
82 /**
83 * G_HOOK_FLAGS:
84 * @hook: a #GHook
86 * Gets the flags of a hook.
89 /**
90 * G_HOOK_FLAG_USER_SHIFT:
92 * The position of the first bit which is not reserved for internal
93 * use be the #GHook implementation, i.e.
94 * `1 << G_HOOK_FLAG_USER_SHIFT` is the first
95 * bit which can be used for application-defined flags.
98 /**
99 * G_HOOK:
100 * @hook: a pointer
102 * Casts a pointer to a `GHook*`.
106 * G_HOOK_IS_VALID:
107 * @hook: a #GHook
109 * Returns %TRUE if the #GHook is valid, i.e. it is in a #GHookList,
110 * it is active and it has not been destroyed.
112 * Returns: %TRUE if the #GHook is valid
116 * G_HOOK_ACTIVE:
117 * @hook: a #GHook
119 * Returns %TRUE if the #GHook is active, which is normally the case
120 * until the #GHook is destroyed.
122 * Returns: %TRUE if the #GHook is active
126 * G_HOOK_IN_CALL:
127 * @hook: a #GHook
129 * Returns %TRUE if the #GHook function is currently executing.
131 * Returns: %TRUE if the #GHook function is currently executing
135 * G_HOOK_IS_UNLINKED:
136 * @hook: a #GHook
138 * Returns %TRUE if the #GHook is not in a #GHookList.
140 * Returns: %TRUE if the #GHook is not in a #GHookList
144 * GHook:
145 * @data: data which is passed to func when this hook is invoked
146 * @next: pointer to the next hook in the list
147 * @prev: pointer to the previous hook in the list
148 * @ref_count: the reference count of this hook
149 * @hook_id: the id of this hook, which is unique within its list
150 * @flags: flags which are set for this hook. See #GHookFlagMask for
151 * predefined flags
152 * @func: the function to call when this hook is invoked. The possible
153 * signatures for this function are #GHookFunc and #GHookCheckFunc
154 * @destroy: the default @finalize_hook function of a #GHookList calls
155 * this member of the hook that is being finalized
157 * The #GHook struct represents a single hook function in a #GHookList.
161 * GHookFunc:
162 * @data: the data field of the #GHook is passed to the hook function here
164 * Defines the type of a hook function that can be invoked
165 * by g_hook_list_invoke().
169 * GHookCheckFunc:
170 * @data: the data field of the #GHook is passed to the hook function here
172 * Defines the type of a hook function that can be invoked
173 * by g_hook_list_invoke_check().
175 * Returns: %FALSE if the #GHook should be destroyed
178 /* --- functions --- */
179 static void
180 default_finalize_hook (GHookList *hook_list,
181 GHook *hook)
183 GDestroyNotify destroy = hook->destroy;
185 if (destroy)
187 hook->destroy = NULL;
188 destroy (hook->data);
193 * g_hook_list_init:
194 * @hook_list: a #GHookList
195 * @hook_size: the size of each element in the #GHookList,
196 * typically `sizeof (GHook)`.
198 * Initializes a #GHookList.
199 * This must be called before the #GHookList is used.
201 void
202 g_hook_list_init (GHookList *hook_list,
203 guint hook_size)
205 g_return_if_fail (hook_list != NULL);
206 g_return_if_fail (hook_size >= sizeof (GHook));
208 hook_list->seq_id = 1;
209 hook_list->hook_size = hook_size;
210 hook_list->is_setup = TRUE;
211 hook_list->hooks = NULL;
212 hook_list->dummy3 = NULL;
213 hook_list->finalize_hook = default_finalize_hook;
214 hook_list->dummy[0] = NULL;
215 hook_list->dummy[1] = NULL;
219 * g_hook_list_clear:
220 * @hook_list: a #GHookList
222 * Removes all the #GHook elements from a #GHookList.
224 void
225 g_hook_list_clear (GHookList *hook_list)
227 g_return_if_fail (hook_list != NULL);
229 if (hook_list->is_setup)
231 GHook *hook;
233 hook_list->is_setup = FALSE;
235 hook = hook_list->hooks;
236 if (!hook)
238 /* destroy hook_list->hook_memchunk */
240 else
243 GHook *tmp;
245 g_hook_ref (hook_list, hook);
246 g_hook_destroy_link (hook_list, hook);
247 tmp = hook->next;
248 g_hook_unref (hook_list, hook);
249 hook = tmp;
251 while (hook);
256 * g_hook_alloc:
257 * @hook_list: a #GHookList
259 * Allocates space for a #GHook and initializes it.
261 * Returns: a new #GHook
263 GHook*
264 g_hook_alloc (GHookList *hook_list)
266 GHook *hook;
268 g_return_val_if_fail (hook_list != NULL, NULL);
269 g_return_val_if_fail (hook_list->is_setup, NULL);
271 hook = g_slice_alloc0 (hook_list->hook_size);
272 hook->data = NULL;
273 hook->next = NULL;
274 hook->prev = NULL;
275 hook->flags = G_HOOK_FLAG_ACTIVE;
276 hook->ref_count = 0;
277 hook->hook_id = 0;
278 hook->func = NULL;
279 hook->destroy = NULL;
281 return hook;
284 * g_hook_free:
285 * @hook_list: a #GHookList
286 * @hook: the #GHook to free
288 * Calls the #GHookList @finalize_hook function if it exists,
289 * and frees the memory allocated for the #GHook.
291 void
292 g_hook_free (GHookList *hook_list,
293 GHook *hook)
295 g_return_if_fail (hook_list != NULL);
296 g_return_if_fail (hook_list->is_setup);
297 g_return_if_fail (hook != NULL);
298 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
299 g_return_if_fail (!G_HOOK_IN_CALL (hook));
301 if(hook_list->finalize_hook != NULL)
302 hook_list->finalize_hook (hook_list, hook);
303 g_slice_free1 (hook_list->hook_size, hook);
307 * g_hook_destroy_link:
308 * @hook_list: a #GHookList
309 * @hook: the #GHook to remove
311 * Removes one #GHook from a #GHookList, marking it
312 * inactive and calling g_hook_unref() on it.
314 void
315 g_hook_destroy_link (GHookList *hook_list,
316 GHook *hook)
318 g_return_if_fail (hook_list != NULL);
319 g_return_if_fail (hook != NULL);
321 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
322 if (hook->hook_id)
324 hook->hook_id = 0;
325 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
330 * g_hook_destroy:
331 * @hook_list: a #GHookList
332 * @hook_id: a hook ID
334 * Destroys a #GHook, given its ID.
336 * Returns: %TRUE if the #GHook was found in the #GHookList and destroyed
338 gboolean
339 g_hook_destroy (GHookList *hook_list,
340 gulong hook_id)
342 GHook *hook;
344 g_return_val_if_fail (hook_list != NULL, FALSE);
345 g_return_val_if_fail (hook_id > 0, FALSE);
347 hook = g_hook_get (hook_list, hook_id);
348 if (hook)
350 g_hook_destroy_link (hook_list, hook);
351 return TRUE;
354 return FALSE;
358 * g_hook_unref:
359 * @hook_list: a #GHookList
360 * @hook: the #GHook to unref
362 * Decrements the reference count of a #GHook.
363 * If the reference count falls to 0, the #GHook is removed
364 * from the #GHookList and g_hook_free() is called to free it.
366 void
367 g_hook_unref (GHookList *hook_list,
368 GHook *hook)
370 g_return_if_fail (hook_list != NULL);
371 g_return_if_fail (hook != NULL);
372 g_return_if_fail (hook->ref_count > 0);
374 hook->ref_count--;
375 if (!hook->ref_count)
377 g_return_if_fail (hook->hook_id == 0);
378 g_return_if_fail (!G_HOOK_IN_CALL (hook));
380 if (hook->prev)
381 hook->prev->next = hook->next;
382 else
383 hook_list->hooks = hook->next;
384 if (hook->next)
386 hook->next->prev = hook->prev;
387 hook->next = NULL;
389 hook->prev = NULL;
391 if (!hook_list->is_setup)
393 hook_list->is_setup = TRUE;
394 g_hook_free (hook_list, hook);
395 hook_list->is_setup = FALSE;
397 if (!hook_list->hooks)
399 /* destroy hook_list->hook_memchunk */
402 else
403 g_hook_free (hook_list, hook);
408 * g_hook_ref:
409 * @hook_list: a #GHookList
410 * @hook: the #GHook to increment the reference count of
412 * Increments the reference count for a #GHook.
414 * Returns: the @hook that was passed in (since 2.6)
416 GHook *
417 g_hook_ref (GHookList *hook_list,
418 GHook *hook)
420 g_return_val_if_fail (hook_list != NULL, NULL);
421 g_return_val_if_fail (hook != NULL, NULL);
422 g_return_val_if_fail (hook->ref_count > 0, NULL);
424 hook->ref_count++;
426 return hook;
430 * g_hook_append:
431 * @hook_list: a #GHookList
432 * @hook: the #GHook to add to the end of @hook_list
434 * Appends a #GHook onto the end of a #GHookList.
438 * g_hook_prepend:
439 * @hook_list: a #GHookList
440 * @hook: the #GHook to add to the start of @hook_list
442 * Prepends a #GHook on the start of a #GHookList.
444 void
445 g_hook_prepend (GHookList *hook_list,
446 GHook *hook)
448 g_return_if_fail (hook_list != NULL);
450 g_hook_insert_before (hook_list, hook_list->hooks, hook);
454 * g_hook_insert_before:
455 * @hook_list: a #GHookList
456 * @sibling: (nullable): the #GHook to insert the new #GHook before
457 * @hook: the #GHook to insert
459 * Inserts a #GHook into a #GHookList, before a given #GHook.
461 void
462 g_hook_insert_before (GHookList *hook_list,
463 GHook *sibling,
464 GHook *hook)
466 g_return_if_fail (hook_list != NULL);
467 g_return_if_fail (hook_list->is_setup);
468 g_return_if_fail (hook != NULL);
469 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
470 g_return_if_fail (hook->ref_count == 0);
472 hook->hook_id = hook_list->seq_id++;
473 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
475 if (sibling)
477 if (sibling->prev)
479 hook->prev = sibling->prev;
480 hook->prev->next = hook;
481 hook->next = sibling;
482 sibling->prev = hook;
484 else
486 hook_list->hooks = hook;
487 hook->next = sibling;
488 sibling->prev = hook;
491 else
493 if (hook_list->hooks)
495 sibling = hook_list->hooks;
496 while (sibling->next)
497 sibling = sibling->next;
498 hook->prev = sibling;
499 sibling->next = hook;
501 else
502 hook_list->hooks = hook;
507 * g_hook_list_invoke:
508 * @hook_list: a #GHookList
509 * @may_recurse: %TRUE if functions which are already running
510 * (e.g. in another thread) can be called. If set to %FALSE,
511 * these are skipped
513 * Calls all of the #GHook functions in a #GHookList.
515 void
516 g_hook_list_invoke (GHookList *hook_list,
517 gboolean may_recurse)
519 GHook *hook;
521 g_return_if_fail (hook_list != NULL);
522 g_return_if_fail (hook_list->is_setup);
524 hook = g_hook_first_valid (hook_list, may_recurse);
525 while (hook)
527 GHookFunc func;
528 gboolean was_in_call;
530 func = (GHookFunc) hook->func;
532 was_in_call = G_HOOK_IN_CALL (hook);
533 hook->flags |= G_HOOK_FLAG_IN_CALL;
534 func (hook->data);
535 if (!was_in_call)
536 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
538 hook = g_hook_next_valid (hook_list, hook, may_recurse);
543 * g_hook_list_invoke_check:
544 * @hook_list: a #GHookList
545 * @may_recurse: %TRUE if functions which are already running
546 * (e.g. in another thread) can be called. If set to %FALSE,
547 * these are skipped
549 * Calls all of the #GHook functions in a #GHookList.
550 * Any function which returns %FALSE is removed from the #GHookList.
552 void
553 g_hook_list_invoke_check (GHookList *hook_list,
554 gboolean may_recurse)
556 GHook *hook;
558 g_return_if_fail (hook_list != NULL);
559 g_return_if_fail (hook_list->is_setup);
561 hook = g_hook_first_valid (hook_list, may_recurse);
562 while (hook)
564 GHookCheckFunc func;
565 gboolean was_in_call;
566 gboolean need_destroy;
568 func = (GHookCheckFunc) hook->func;
570 was_in_call = G_HOOK_IN_CALL (hook);
571 hook->flags |= G_HOOK_FLAG_IN_CALL;
572 need_destroy = !func (hook->data);
573 if (!was_in_call)
574 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
575 if (need_destroy)
576 g_hook_destroy_link (hook_list, hook);
578 hook = g_hook_next_valid (hook_list, hook, may_recurse);
583 * GHookCheckMarshaller:
584 * @hook: a #GHook
585 * @marshal_data: user data
587 * Defines the type of function used by g_hook_list_marshal_check().
589 * Returns: %FALSE if @hook should be destroyed
593 * g_hook_list_marshal_check:
594 * @hook_list: a #GHookList
595 * @may_recurse: %TRUE if hooks which are currently running
596 * (e.g. in another thread) are considered valid. If set to %FALSE,
597 * these are skipped
598 * @marshaller: the function to call for each #GHook
599 * @marshal_data: data to pass to @marshaller
601 * Calls a function on each valid #GHook and destroys it if the
602 * function returns %FALSE.
604 void
605 g_hook_list_marshal_check (GHookList *hook_list,
606 gboolean may_recurse,
607 GHookCheckMarshaller marshaller,
608 gpointer data)
610 GHook *hook;
612 g_return_if_fail (hook_list != NULL);
613 g_return_if_fail (hook_list->is_setup);
614 g_return_if_fail (marshaller != NULL);
616 hook = g_hook_first_valid (hook_list, may_recurse);
617 while (hook)
619 gboolean was_in_call;
620 gboolean need_destroy;
622 was_in_call = G_HOOK_IN_CALL (hook);
623 hook->flags |= G_HOOK_FLAG_IN_CALL;
624 need_destroy = !marshaller (hook, data);
625 if (!was_in_call)
626 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
627 if (need_destroy)
628 g_hook_destroy_link (hook_list, hook);
630 hook = g_hook_next_valid (hook_list, hook, may_recurse);
635 * GHookMarshaller:
636 * @hook: a #GHook
637 * @marshal_data: user data
639 * Defines the type of function used by g_hook_list_marshal().
643 * g_hook_list_marshal:
644 * @hook_list: a #GHookList
645 * @may_recurse: %TRUE if hooks which are currently running
646 * (e.g. in another thread) are considered valid. If set to %FALSE,
647 * these are skipped
648 * @marshaller: the function to call for each #GHook
649 * @marshal_data: data to pass to @marshaller
651 * Calls a function on each valid #GHook.
653 void
654 g_hook_list_marshal (GHookList *hook_list,
655 gboolean may_recurse,
656 GHookMarshaller marshaller,
657 gpointer data)
659 GHook *hook;
661 g_return_if_fail (hook_list != NULL);
662 g_return_if_fail (hook_list->is_setup);
663 g_return_if_fail (marshaller != NULL);
665 hook = g_hook_first_valid (hook_list, may_recurse);
666 while (hook)
668 gboolean was_in_call;
670 was_in_call = G_HOOK_IN_CALL (hook);
671 hook->flags |= G_HOOK_FLAG_IN_CALL;
672 marshaller (hook, data);
673 if (!was_in_call)
674 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
676 hook = g_hook_next_valid (hook_list, hook, may_recurse);
681 * g_hook_first_valid:
682 * @hook_list: a #GHookList
683 * @may_be_in_call: %TRUE if hooks which are currently running
684 * (e.g. in another thread) are considered valid. If set to %FALSE,
685 * these are skipped
687 * Returns the first #GHook in a #GHookList which has not been destroyed.
688 * The reference count for the #GHook is incremented, so you must call
689 * g_hook_unref() to restore it when no longer needed. (Or call
690 * g_hook_next_valid() if you are stepping through the #GHookList.)
692 * Returns: the first valid #GHook, or %NULL if none are valid
694 GHook*
695 g_hook_first_valid (GHookList *hook_list,
696 gboolean may_be_in_call)
698 g_return_val_if_fail (hook_list != NULL, NULL);
700 if (hook_list->is_setup)
702 GHook *hook;
704 hook = hook_list->hooks;
705 if (hook)
707 g_hook_ref (hook_list, hook);
708 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
709 return hook;
710 else
711 return g_hook_next_valid (hook_list, hook, may_be_in_call);
715 return NULL;
719 * g_hook_next_valid:
720 * @hook_list: a #GHookList
721 * @hook: the current #GHook
722 * @may_be_in_call: %TRUE if hooks which are currently running
723 * (e.g. in another thread) are considered valid. If set to %FALSE,
724 * these are skipped
726 * Returns the next #GHook in a #GHookList which has not been destroyed.
727 * The reference count for the #GHook is incremented, so you must call
728 * g_hook_unref() to restore it when no longer needed. (Or continue to call
729 * g_hook_next_valid() until %NULL is returned.)
731 * Returns: the next valid #GHook, or %NULL if none are valid
733 GHook*
734 g_hook_next_valid (GHookList *hook_list,
735 GHook *hook,
736 gboolean may_be_in_call)
738 GHook *ohook = hook;
740 g_return_val_if_fail (hook_list != NULL, NULL);
742 if (!hook)
743 return NULL;
745 hook = hook->next;
746 while (hook)
748 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
750 g_hook_ref (hook_list, hook);
751 g_hook_unref (hook_list, ohook);
753 return hook;
755 hook = hook->next;
757 g_hook_unref (hook_list, ohook);
759 return NULL;
763 * g_hook_get:
764 * @hook_list: a #GHookList
765 * @hook_id: a hook id
767 * Returns the #GHook with the given id, or %NULL if it is not found.
769 * Returns: the #GHook with the given id, or %NULL if it is not found
771 GHook*
772 g_hook_get (GHookList *hook_list,
773 gulong hook_id)
775 GHook *hook;
777 g_return_val_if_fail (hook_list != NULL, NULL);
778 g_return_val_if_fail (hook_id > 0, NULL);
780 hook = hook_list->hooks;
781 while (hook)
783 if (hook->hook_id == hook_id)
784 return hook;
785 hook = hook->next;
788 return NULL;
792 * GHookFindFunc:
793 * @hook: a #GHook
794 * @data: user data passed to g_hook_find_func()
796 * Defines the type of the function passed to g_hook_find().
798 * Returns: %TRUE if the required #GHook has been found
802 * g_hook_find:
803 * @hook_list: a #GHookList
804 * @need_valids: %TRUE if #GHook elements which have been destroyed
805 * should be skipped
806 * @func: the function to call for each #GHook, which should return
807 * %TRUE when the #GHook has been found
808 * @data: the data to pass to @func
810 * Finds a #GHook in a #GHookList using the given function to
811 * test for a match.
813 * Returns: the found #GHook or %NULL if no matching #GHook is found
815 GHook*
816 g_hook_find (GHookList *hook_list,
817 gboolean need_valids,
818 GHookFindFunc func,
819 gpointer data)
821 GHook *hook;
823 g_return_val_if_fail (hook_list != NULL, NULL);
824 g_return_val_if_fail (func != NULL, NULL);
826 hook = hook_list->hooks;
827 while (hook)
829 GHook *tmp;
831 /* test only non-destroyed hooks */
832 if (!hook->hook_id)
834 hook = hook->next;
835 continue;
838 g_hook_ref (hook_list, hook);
840 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
842 g_hook_unref (hook_list, hook);
844 return hook;
847 tmp = hook->next;
848 g_hook_unref (hook_list, hook);
849 hook = tmp;
852 return NULL;
856 * g_hook_find_data:
857 * @hook_list: a #GHookList
858 * @need_valids: %TRUE if #GHook elements which have been destroyed
859 * should be skipped
860 * @data: the data to find
862 * Finds a #GHook in a #GHookList with the given data.
864 * Returns: the #GHook with the given @data or %NULL if no matching
865 * #GHook is found
867 GHook*
868 g_hook_find_data (GHookList *hook_list,
869 gboolean need_valids,
870 gpointer data)
872 GHook *hook;
874 g_return_val_if_fail (hook_list != NULL, NULL);
876 hook = hook_list->hooks;
877 while (hook)
879 /* test only non-destroyed hooks */
880 if (hook->data == data &&
881 hook->hook_id &&
882 (!need_valids || G_HOOK_ACTIVE (hook)))
883 return hook;
885 hook = hook->next;
888 return NULL;
892 * g_hook_find_func:
893 * @hook_list: a #GHookList
894 * @need_valids: %TRUE if #GHook elements which have been destroyed
895 * should be skipped
896 * @func: the function to find
898 * Finds a #GHook in a #GHookList with the given function.
900 * Returns: the #GHook with the given @func or %NULL if no matching
901 * #GHook is found
903 GHook*
904 g_hook_find_func (GHookList *hook_list,
905 gboolean need_valids,
906 gpointer func)
908 GHook *hook;
910 g_return_val_if_fail (hook_list != NULL, NULL);
911 g_return_val_if_fail (func != NULL, NULL);
913 hook = hook_list->hooks;
914 while (hook)
916 /* test only non-destroyed hooks */
917 if (hook->func == func &&
918 hook->hook_id &&
919 (!need_valids || G_HOOK_ACTIVE (hook)))
920 return hook;
922 hook = hook->next;
925 return NULL;
929 * g_hook_find_func_data:
930 * @hook_list: a #GHookList
931 * @need_valids: %TRUE if #GHook elements which have been destroyed
932 * should be skipped
933 * @func: (not nullable): the function to find
934 * @data: the data to find
936 * Finds a #GHook in a #GHookList with the given function and data.
938 * Returns: the #GHook with the given @func and @data or %NULL if
939 * no matching #GHook is found
941 GHook*
942 g_hook_find_func_data (GHookList *hook_list,
943 gboolean need_valids,
944 gpointer func,
945 gpointer data)
947 GHook *hook;
949 g_return_val_if_fail (hook_list != NULL, NULL);
950 g_return_val_if_fail (func != NULL, NULL);
952 hook = hook_list->hooks;
953 while (hook)
955 /* test only non-destroyed hooks */
956 if (hook->data == data &&
957 hook->func == func &&
958 hook->hook_id &&
959 (!need_valids || G_HOOK_ACTIVE (hook)))
960 return hook;
962 hook = hook->next;
965 return NULL;
969 * GHookCompareFunc:
970 * @new_hook: the #GHook being inserted
971 * @sibling: the #GHook to compare with @new_hook
973 * Defines the type of function used to compare #GHook elements in
974 * g_hook_insert_sorted().
976 * Returns: a value <= 0 if @new_hook should be before @sibling
980 * g_hook_insert_sorted:
981 * @hook_list: a #GHookList
982 * @hook: the #GHook to insert
983 * @func: the comparison function used to sort the #GHook elements
985 * Inserts a #GHook into a #GHookList, sorted by the given function.
987 void
988 g_hook_insert_sorted (GHookList *hook_list,
989 GHook *hook,
990 GHookCompareFunc func)
992 GHook *sibling;
994 g_return_if_fail (hook_list != NULL);
995 g_return_if_fail (hook_list->is_setup);
996 g_return_if_fail (hook != NULL);
997 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
998 g_return_if_fail (hook->func != NULL);
999 g_return_if_fail (func != NULL);
1001 /* first non-destroyed hook */
1002 sibling = hook_list->hooks;
1003 while (sibling && !sibling->hook_id)
1004 sibling = sibling->next;
1006 while (sibling)
1008 GHook *tmp;
1010 g_hook_ref (hook_list, sibling);
1011 if (func (hook, sibling) <= 0 && sibling->hook_id)
1013 g_hook_unref (hook_list, sibling);
1014 break;
1017 /* next non-destroyed hook */
1018 tmp = sibling->next;
1019 while (tmp && !tmp->hook_id)
1020 tmp = tmp->next;
1022 g_hook_unref (hook_list, sibling);
1023 sibling = tmp;
1027 g_hook_insert_before (hook_list, sibling, hook);
1031 * g_hook_compare_ids:
1032 * @new_hook: a #GHook
1033 * @sibling: a #GHook to compare with @new_hook
1035 * Compares the ids of two #GHook elements, returning a negative value
1036 * if the second id is greater than the first.
1038 * Returns: a value <= 0 if the id of @sibling is >= the id of @new_hook
1040 gint
1041 g_hook_compare_ids (GHook *new_hook,
1042 GHook *sibling)
1044 if (new_hook->hook_id < sibling->hook_id)
1045 return -1;
1046 else if (new_hook->hook_id > sibling->hook_id)
1047 return 1;
1049 return 0;