fix up refcount/properties test case
[glib.git] / gobject / gobjectnotifyqueue.c
blobcfe01ba0b36ce6473d0210622e8b6eed76e5221b
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
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.
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.
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.
20 /* WARNING:
22 * This file is INSTALLED and other projects (outside of glib)
23 * #include its contents.
26 #ifndef __G_OBJECT_NOTIFY_QUEUE_H__
27 #define __G_OBJECT_NOTIFY_QUEUE_H__
29 #include <string.h> /* memset */
31 #include <glib-object.h>
33 G_BEGIN_DECLS
36 /* --- typedefs --- */
37 typedef struct _GObjectNotifyContext GObjectNotifyContext;
38 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
39 typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
40 guint n_pspecs,
41 GParamSpec **pspecs);
44 /* --- structures --- */
45 struct _GObjectNotifyContext
47 GQuark quark_notify_queue;
48 GObjectNotifyQueueDispatcher dispatcher;
49 GTrashStack *_nqueue_trash; /* unused */
51 struct _GObjectNotifyQueue
53 GObjectNotifyContext *context;
54 GSList *pspecs;
55 guint16 n_pspecs;
56 guint16 freeze_count;
59 G_LOCK_DEFINE_STATIC(notify_lock);
61 /* --- functions --- */
62 static void
63 g_object_notify_queue_free (gpointer data)
65 GObjectNotifyQueue *nqueue = data;
67 g_slist_free (nqueue->pspecs);
68 g_slice_free (GObjectNotifyQueue, nqueue);
71 static inline GObjectNotifyQueue*
72 g_object_notify_queue_freeze (GObject *object,
73 GObjectNotifyContext *context)
75 GObjectNotifyQueue *nqueue;
77 G_LOCK(notify_lock);
78 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
79 if (!nqueue)
81 nqueue = g_slice_new0 (GObjectNotifyQueue);
82 nqueue->context = context;
83 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
84 nqueue, g_object_notify_queue_free);
87 if (nqueue->freeze_count >= 65535)
88 g_critical("Free queue for %s (%p) is larger than 65535,"
89 " called g_object_freeze_notify() too often."
90 " Forgot to call g_object_thaw_notify() or infinite loop",
91 G_OBJECT_TYPE_NAME (object), object);
92 else
93 nqueue->freeze_count++;
94 G_UNLOCK(notify_lock);
96 return nqueue;
99 static inline void
100 g_object_notify_queue_thaw (GObject *object,
101 GObjectNotifyQueue *nqueue)
103 GObjectNotifyContext *context = nqueue->context;
104 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
105 GSList *slist;
106 guint n_pspecs = 0;
108 g_return_if_fail (nqueue->freeze_count > 0);
109 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
111 G_LOCK(notify_lock);
113 /* Just make sure we never get into some nasty race condition */
114 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
115 G_UNLOCK(notify_lock);
116 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
117 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
118 return;
121 nqueue->freeze_count--;
122 if (nqueue->freeze_count) {
123 G_UNLOCK(notify_lock);
124 return;
127 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
129 for (slist = nqueue->pspecs; slist; slist = slist->next)
131 pspecs[n_pspecs++] = slist->data;
133 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
135 G_UNLOCK(notify_lock);
137 if (n_pspecs)
138 context->dispatcher (object, n_pspecs, pspecs);
139 g_free (free_me);
142 static inline void
143 g_object_notify_queue_clear (GObject *object,
144 GObjectNotifyQueue *nqueue)
146 g_return_if_fail (nqueue->freeze_count > 0);
148 G_LOCK(notify_lock);
150 g_slist_free (nqueue->pspecs);
151 nqueue->pspecs = NULL;
152 nqueue->n_pspecs = 0;
154 G_UNLOCK(notify_lock);
157 static inline void
158 g_object_notify_queue_add (GObject *object,
159 GObjectNotifyQueue *nqueue,
160 GParamSpec *pspec)
162 if (pspec->flags & G_PARAM_READABLE)
164 GParamSpec *redirect;
166 G_LOCK(notify_lock);
168 g_return_if_fail (nqueue->n_pspecs < 65535);
170 redirect = g_param_spec_get_redirect_target (pspec);
171 if (redirect)
172 pspec = redirect;
174 /* we do the deduping in _thaw */
175 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
177 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
178 nqueue->n_pspecs++;
181 G_UNLOCK(notify_lock);
185 /* NB: This function is not threadsafe, do not ever use it if
186 * you need a threadsafe notify queue.
187 * Use g_object_notify_queue_freeze() to acquire the queue and
188 * g_object_notify_queue_thaw() after you are done instead.
190 static inline GObjectNotifyQueue*
191 g_object_notify_queue_from_object (GObject *object,
192 GObjectNotifyContext *context)
194 return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
197 G_END_DECLS
199 #endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */