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 Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 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
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the
20
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
 * Boston, MA 02110-1301, USA.
22
 */
23

            
24
#include <config.h>
25
#include <glib.h>
26

            
27
#include "reentrant-list.h"
28

            
29
typedef struct
30
{
31
  GList **list;
32
  GList *iterator;
33
} Iteration;
34

            
35
static GSList *working_list = NULL; /* of Iteration */
36

            
37
/*
38
 *   deletes an element from the list - in a re-entrant
39
 * safe fashion; advances the element pointer to the next
40
 * element.
41
 */
42
void
43
spi_re_entrant_list_delete_link (GList *const *element_ptr)
44
{
45
  GSList *l;
46
  GList *next;
47
  GList *element;
48
  gboolean first_item;
49
  GList *dummy G_GNUC_UNUSED;
50

            
51
  g_return_if_fail (element_ptr != NULL);
52

            
53
  element = *element_ptr;
54
  g_return_if_fail (element != NULL);
55

            
56
  next = element->next;
57
  first_item = (element->prev == NULL);
58

            
59
  dummy = g_list_remove_link (NULL, element);
60

            
61
  for (l = working_list; l; l = l->next)
62
    {
63
      Iteration *i = l->data;
64

            
65
      if (i->iterator == element)
66
        {
67
          i->iterator = next;
68
        }
69

            
70
      if (first_item && *(i->list) == element)
71
        {
72
          *(i->list) = next;
73
        }
74
    }
75

            
76
  g_list_free_1 (element);
77
}
78

            
79
void
80
spi_re_entrant_list_foreach (GList **list,
81
                             SpiReEntrantFn func,
82
                             gpointer user_data)
83
{
84
  Iteration i;
85

            
86
  if (!list || !*list)
87
    {
88
      return;
89
    }
90

            
91
  i.list = list;
92
  i.iterator = *list;
93

            
94
  working_list = g_slist_prepend (working_list, &i);
95

            
96
  while (i.iterator)
97
    {
98
      GList *l = i.iterator;
99

            
100
      func (&i.iterator, user_data);
101

            
102
      if (i.iterator == l)
103
        i.iterator = i.iterator->next;
104
    }
105

            
106
  working_list = g_slist_remove (working_list, &i);
107
}