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