Update Friulian translation
[glib.git] / gobject / gobjectnotifyqueue.c
blob1a7d23f26668449b2a086820ba6b13df99144024
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.1 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, see <http://www.gnu.org/licenses/>.
18 /* WARNING:
20 * This file is INSTALLED and other projects (outside of glib)
21 * #include its contents.
24 #ifndef __G_OBJECT_NOTIFY_QUEUE_H__
25 #define __G_OBJECT_NOTIFY_QUEUE_H__
27 #include <string.h> /* memset */
29 #include <glib-object.h>
31 G_BEGIN_DECLS
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);
42 /* --- structures --- */
43 struct _GObjectNotifyContext
45 GQuark quark_notify_queue;
46 GObjectNotifyQueueDispatcher dispatcher;
47 GTrashStack *_nqueue_trash; /* unused */
49 struct _GObjectNotifyQueue
51 GObjectNotifyContext *context;
52 GSList *pspecs;
53 guint16 n_pspecs;
54 guint16 freeze_count;
57 G_LOCK_DEFINE_STATIC(notify_lock);
59 /* --- functions --- */
60 static void
61 g_object_notify_queue_free (gpointer data)
63 GObjectNotifyQueue *nqueue = data;
65 g_slist_free (nqueue->pspecs);
66 g_slice_free (GObjectNotifyQueue, nqueue);
69 static inline GObjectNotifyQueue*
70 g_object_notify_queue_freeze (GObject *object,
71 GObjectNotifyContext *context)
73 GObjectNotifyQueue *nqueue;
75 G_LOCK(notify_lock);
76 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
77 if (!nqueue)
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);
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);
94 return nqueue;
97 static inline void
98 g_object_notify_queue_thaw (GObject *object,
99 GObjectNotifyQueue *nqueue)
101 GObjectNotifyContext *context = nqueue->context;
102 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
103 GSList *slist;
104 guint n_pspecs = 0;
106 g_return_if_fail (nqueue->freeze_count > 0);
107 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
109 G_LOCK(notify_lock);
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;
119 nqueue->freeze_count--;
120 if (nqueue->freeze_count) {
121 G_UNLOCK(notify_lock);
122 return;
125 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
127 for (slist = nqueue->pspecs; slist; slist = slist->next)
129 pspecs[n_pspecs++] = slist->data;
131 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
133 G_UNLOCK(notify_lock);
135 if (n_pspecs)
136 context->dispatcher (object, n_pspecs, pspecs);
137 g_free (free_me);
140 static inline void
141 g_object_notify_queue_clear (GObject *object,
142 GObjectNotifyQueue *nqueue)
144 g_return_if_fail (nqueue->freeze_count > 0);
146 G_LOCK(notify_lock);
148 g_slist_free (nqueue->pspecs);
149 nqueue->pspecs = NULL;
150 nqueue->n_pspecs = 0;
152 G_UNLOCK(notify_lock);
155 static inline void
156 g_object_notify_queue_add (GObject *object,
157 GObjectNotifyQueue *nqueue,
158 GParamSpec *pspec)
160 if (pspec->flags & G_PARAM_READABLE)
162 GParamSpec *redirect;
164 G_LOCK(notify_lock);
166 g_return_if_fail (nqueue->n_pspecs < 65535);
168 redirect = g_param_spec_get_redirect_target (pspec);
169 if (redirect)
170 pspec = redirect;
172 /* we do the deduping in _thaw */
173 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
175 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
176 nqueue->n_pspecs++;
179 G_UNLOCK(notify_lock);
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.
188 static inline GObjectNotifyQueue*
189 g_object_notify_queue_from_object (GObject *object,
190 GObjectNotifyContext *context)
192 return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
195 G_END_DECLS
197 #endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */