2009-06-11 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / registryd / reentrant-list.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <config.h>
25 #include <glib.h>
26
27 #include "reentrant-list.h"
28
29 typedef struct {
30         GList **list;
31         GList  *iterator;
32 } Iteration;
33
34 static GSList *working_list = NULL; /* of Iteration */
35
36 /*
37  *   deletes an element from the list - in a re-entrant
38  * safe fashion; advances the element pointer to the next
39  * element.
40  */
41 void
42 spi_re_entrant_list_delete_link (GList * const *element_ptr)
43 {
44   GSList    *l;
45   GList     *next;
46   GList     *element;
47   gboolean   first_item;
48
49   g_return_if_fail (element_ptr != NULL);
50
51   element = *element_ptr;
52   g_return_if_fail (element != NULL);
53
54   next = element->next;
55   first_item = (element->prev == NULL);
56
57   g_list_remove_link (NULL, element);
58
59   for (l = working_list; l; l = l->next)
60     {
61        Iteration *i = l->data;
62
63        if (i->iterator == element)
64          {
65            i->iterator = next;
66          }
67
68        if (first_item && *(i->list) == element)
69          {
70            *(i->list) = next;
71          }
72     }
73
74   g_list_free_1 (element);
75 }
76
77 void
78 spi_re_entrant_list_foreach (GList         **list,
79                              SpiReEntrantFn  func,
80                              gpointer        user_data)
81 {
82         Iteration i;
83
84         if (!list || !*list)
85           {
86             return;
87           }
88
89         i.list = list;
90         i.iterator = *list;
91
92         working_list = g_slist_prepend (working_list, &i);
93
94         while (i.iterator) {
95                 GList *l = i.iterator;
96
97                 func (&i.iterator, user_data);
98
99                 if (i.iterator == l)
100                         i.iterator = i.iterator->next;
101         }
102
103         working_list = g_slist_remove (working_list, &i);
104 }