gtk2-ng/gtk/gtknotifyqueue.h
Daemonratte 32cbd190c2 gtk: own copy of GLib's notify queue
gobjectnotifyqueue.c, which gtkwidget.c and gtkcontainer.c included for
the child property notifications, now uses the deprecated GTrashStack.
gtknotifyqueue.h is the same code without it (LGPL 2.1 or later, like
the original by Tim Janik and Red Hat).
2026-10-19 13:50:39 +02:00

186 lines
5.3 KiB
C

/* GTK - The GIMP Toolkit
* Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
*
* The queue of child property notifications that gtkwidget.c and
* gtkcontainer.c share. It is GLib's gobjectnotifyqueue.c, which they
* included until it came to depend on the deprecated GTrashStack, with
* the unused trash stack left out, GTK+'s names, and the lock no
* longer kept when adding to a full queue.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_NOTIFY_QUEUE_H__
#define __GTK_NOTIFY_QUEUE_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _GtkNotifyContext GtkNotifyContext;
typedef struct _GtkNotifyQueue GtkNotifyQueue;
typedef void (*GtkNotifyQueueDispatcher) (GObject *object,
guint n_pspecs,
GParamSpec **pspecs);
struct _GtkNotifyContext
{
GQuark quark_notify_queue;
GtkNotifyQueueDispatcher dispatcher;
};
struct _GtkNotifyQueue
{
GtkNotifyContext *context;
GSList *pspecs;
guint16 n_pspecs;
guint16 freeze_count;
};
G_LOCK_DEFINE_STATIC (notify_lock);
static void
_gtk_notify_queue_free (gpointer data)
{
GtkNotifyQueue *nqueue = data;
g_slist_free (nqueue->pspecs);
g_slice_free (GtkNotifyQueue, nqueue);
}
static inline GtkNotifyQueue *
_gtk_notify_queue_freeze (GObject *object,
GtkNotifyContext *context)
{
GtkNotifyQueue *nqueue;
G_LOCK (notify_lock);
nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
if (!nqueue)
{
nqueue = g_slice_new0 (GtkNotifyQueue);
nqueue->context = context;
g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
nqueue, _gtk_notify_queue_free);
}
if (nqueue->freeze_count >= 65535)
g_critical ("Free queue for %s (%p) is larger than 65535,"
" called g_object_freeze_notify() too often."
" Forgot to call g_object_thaw_notify() or infinite loop",
G_OBJECT_TYPE_NAME (object), object);
else
nqueue->freeze_count++;
G_UNLOCK (notify_lock);
return nqueue;
}
static inline void
_gtk_notify_queue_thaw (GObject *object,
GtkNotifyQueue *nqueue)
{
GtkNotifyContext *context = nqueue->context;
GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
GSList *slist;
guint n_pspecs = 0;
g_return_if_fail (nqueue->freeze_count > 0);
g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0);
G_LOCK (notify_lock);
/* Just make sure we never get into some nasty race condition */
if (G_UNLIKELY (nqueue->freeze_count == 0))
{
G_UNLOCK (notify_lock);
g_critical ("%s: property-changed notification for %s(%p) is not frozen",
G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
return;
}
nqueue->freeze_count--;
if (nqueue->freeze_count)
{
G_UNLOCK (notify_lock);
return;
}
pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec *, nqueue->n_pspecs) : pspecs_mem;
for (slist = nqueue->pspecs; slist; slist = slist->next)
pspecs[n_pspecs++] = slist->data;
g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
G_UNLOCK (notify_lock);
if (n_pspecs)
context->dispatcher (object, n_pspecs, pspecs);
g_free (free_me);
}
static inline void
_gtk_notify_queue_clear (GObject *object,
GtkNotifyQueue *nqueue)
{
g_return_if_fail (nqueue->freeze_count > 0);
G_LOCK (notify_lock);
g_slist_free (nqueue->pspecs);
nqueue->pspecs = NULL;
nqueue->n_pspecs = 0;
G_UNLOCK (notify_lock);
}
static inline void
_gtk_notify_queue_add (GObject *object,
GtkNotifyQueue *nqueue,
GParamSpec *pspec)
{
if (pspec->flags & G_PARAM_READABLE)
{
GParamSpec *redirect;
g_return_if_fail (nqueue->n_pspecs < 65535);
G_LOCK (notify_lock);
redirect = g_param_spec_get_redirect_target (pspec);
if (redirect)
pspec = redirect;
/* each property once */
if (g_slist_find (nqueue->pspecs, pspec) == NULL)
{
nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
nqueue->n_pspecs++;
}
G_UNLOCK (notify_lock);
}
}
/* Not threadsafe: _gtk_notify_queue_freeze() and _thaw() are */
static inline GtkNotifyQueue *
_gtk_notify_queue_from_object (GObject *object,
GtkNotifyContext *context)
{
return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
}
G_END_DECLS
#endif /* __GTK_NOTIFY_QUEUE_H__ */