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.
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>
36 /* --- typedefs --- */
37 typedef struct _GObjectNotifyContext GObjectNotifyContext
;
38 typedef struct _GObjectNotifyQueue GObjectNotifyQueue
;
39 typedef void (*GObjectNotifyQueueDispatcher
) (GObject
*object
,
44 /* --- structures --- */
45 struct _GObjectNotifyContext
47 GQuark quark_notify_queue
;
48 GObjectNotifyQueueDispatcher dispatcher
;
49 GTrashStack
*_nqueue_trash
; /* unused */
51 struct _GObjectNotifyQueue
53 GObjectNotifyContext
*context
;
59 G_LOCK_DEFINE_STATIC(notify_lock
);
61 /* --- functions --- */
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
;
78 nqueue
= g_datalist_id_get_data (&object
->qdata
, context
->quark_notify_queue
);
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
);
93 nqueue
->freeze_count
++;
94 G_UNLOCK(notify_lock
);
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
;
108 g_return_if_fail (nqueue
->freeze_count
> 0);
109 g_return_if_fail (g_atomic_int_get(&object
->ref_count
) > 0);
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
);
121 nqueue
->freeze_count
--;
122 if (nqueue
->freeze_count
) {
123 G_UNLOCK(notify_lock
);
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
);
138 context
->dispatcher (object
, n_pspecs
, pspecs
);
143 g_object_notify_queue_clear (GObject
*object
,
144 GObjectNotifyQueue
*nqueue
)
146 g_return_if_fail (nqueue
->freeze_count
> 0);
150 g_slist_free (nqueue
->pspecs
);
151 nqueue
->pspecs
= NULL
;
152 nqueue
->n_pspecs
= 0;
154 G_UNLOCK(notify_lock
);
158 g_object_notify_queue_add (GObject
*object
,
159 GObjectNotifyQueue
*nqueue
,
162 if (pspec
->flags
& G_PARAM_READABLE
)
164 GParamSpec
*redirect
;
168 g_return_if_fail (nqueue
->n_pspecs
< 65535);
170 redirect
= g_param_spec_get_redirect_target (pspec
);
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
);
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
);
199 #endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */