Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gobject / gobjectnotifyqueue.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifndef __G_OBJECT_NOTIFY_QUEUE_H__
21 #define __G_OBJECT_NOTIFY_QUEUE_H__
22
23 #include <string.h> /* memset */
24
25 #include <glib-object.h>
26
27 G_BEGIN_DECLS
28
29
30 /* --- typedefs --- */
31 typedef struct _GObjectNotifyContext          GObjectNotifyContext;
32 typedef struct _GObjectNotifyQueue            GObjectNotifyQueue;
33 typedef void (*GObjectNotifyQueueDispatcher) (GObject     *object,
34                                               guint        n_pspecs,
35                                               GParamSpec **pspecs);
36
37
38 /* --- structures --- */
39 struct _GObjectNotifyContext
40 {
41   GQuark                       quark_notify_queue;
42   GObjectNotifyQueueDispatcher dispatcher;
43   GTrashStack                 *_nqueue_trash; /* unused */
44 };
45 struct _GObjectNotifyQueue
46 {
47   GObjectNotifyContext *context;
48   GSList               *pspecs;
49   guint16               n_pspecs;
50   guint16               freeze_count;
51 };
52
53 G_LOCK_DEFINE_STATIC(notify_lock);
54
55 /* --- functions --- */
56 static void
57 g_object_notify_queue_free (gpointer data)
58 {
59   GObjectNotifyQueue *nqueue = data;
60
61   g_slist_free (nqueue->pspecs);
62   g_slice_free (GObjectNotifyQueue, nqueue);
63 }
64
65 static inline GObjectNotifyQueue*
66 g_object_notify_queue_freeze (GObject              *object,
67                               GObjectNotifyContext *context)
68 {
69   GObjectNotifyQueue *nqueue;
70
71   G_LOCK(notify_lock);
72   nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
73   if (!nqueue)
74     {
75       nqueue = g_slice_new0 (GObjectNotifyQueue);
76       nqueue->context = context;
77       g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
78                                    nqueue, g_object_notify_queue_free);
79     }
80
81   if (nqueue->freeze_count >= 65535)
82     g_critical("Free queue for %s (%p) is larger than 65535,"
83                " called g_object_freeze_notify() too often."
84                " Forgot to call g_object_thaw_notify() or infinite loop",
85                G_OBJECT_TYPE_NAME (object), object);
86   else
87     nqueue->freeze_count++;
88   G_UNLOCK(notify_lock);
89
90   return nqueue;
91 }
92
93 static inline void
94 g_object_notify_queue_thaw (GObject            *object,
95                             GObjectNotifyQueue *nqueue)
96 {
97   GObjectNotifyContext *context = nqueue->context;
98   GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
99   GSList *slist;
100   guint n_pspecs = 0;
101
102   g_return_if_fail (nqueue->freeze_count > 0);
103   g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
104
105   G_LOCK(notify_lock);
106
107   /* Just make sure we never get into some nasty race condition */
108   if (G_UNLIKELY(nqueue->freeze_count == 0)) {
109     G_UNLOCK(notify_lock);
110     g_warning ("%s: property-changed notification for %s(%p) is not frozen",
111                G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
112     return;
113   }
114
115   nqueue->freeze_count--;
116   if (nqueue->freeze_count) {
117     G_UNLOCK(notify_lock);
118     return;
119   }
120
121   pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
122
123   for (slist = nqueue->pspecs; slist; slist = slist->next)
124     {
125       pspecs[n_pspecs++] = slist->data;
126     }
127   g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
128
129   G_UNLOCK(notify_lock);
130
131   if (n_pspecs)
132     context->dispatcher (object, n_pspecs, pspecs);
133   g_free (free_me);
134 }
135
136 static inline void
137 g_object_notify_queue_clear (GObject            *object,
138                              GObjectNotifyQueue *nqueue)
139 {
140   g_return_if_fail (nqueue->freeze_count > 0);
141
142   G_LOCK(notify_lock);
143
144   g_slist_free (nqueue->pspecs);
145   nqueue->pspecs = NULL;
146   nqueue->n_pspecs = 0;
147
148   G_UNLOCK(notify_lock);
149 }
150
151 static inline void
152 g_object_notify_queue_add (GObject            *object,
153                            GObjectNotifyQueue *nqueue,
154                            GParamSpec         *pspec)
155 {
156   if (pspec->flags & G_PARAM_READABLE)
157     {
158       GParamSpec *redirect;
159
160       G_LOCK(notify_lock);
161
162       g_return_if_fail (nqueue->n_pspecs < 65535);
163
164       redirect = g_param_spec_get_redirect_target (pspec);
165       if (redirect)
166         pspec = redirect;
167             
168       /* we do the deduping in _thaw */
169       if (g_slist_find (nqueue->pspecs, pspec) == NULL)
170         {
171           nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
172           nqueue->n_pspecs++;
173         }
174
175       G_UNLOCK(notify_lock);
176     }
177 }
178
179 /* NB: This function is not threadsafe, do not ever use it if
180  * you need a threadsafe notify queue.
181  * Use g_object_notify_queue_freeze() to acquire the queue and
182  * g_object_notify_queue_thaw() after you are done instead.
183  */
184 static inline GObjectNotifyQueue*
185 g_object_notify_queue_from_object (GObject              *object,
186                                    GObjectNotifyContext *context)
187 {
188   return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
189 }
190
191 G_END_DECLS
192
193 #endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */