Blame gobject/gobjectnotifyqueue.c

Packit ae235b
/* GObject - GLib Type, Object, Parameter and Signal Library
Packit ae235b
 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* WARNING:
Packit ae235b
 *
Packit ae235b
 *    This file is INSTALLED and other projects (outside of glib)
Packit ae235b
 *    #include its contents.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#ifndef __G_OBJECT_NOTIFY_QUEUE_H__
Packit ae235b
#define __G_OBJECT_NOTIFY_QUEUE_H__
Packit ae235b
Packit ae235b
#include <string.h> /* memset */
Packit ae235b
Packit ae235b
#include <glib-object.h>
Packit ae235b
Packit ae235b
G_BEGIN_DECLS
Packit ae235b
Packit ae235b
Packit ae235b
/* --- typedefs --- */
Packit ae235b
typedef struct _GObjectNotifyContext          GObjectNotifyContext;
Packit ae235b
typedef struct _GObjectNotifyQueue            GObjectNotifyQueue;
Packit ae235b
typedef void (*GObjectNotifyQueueDispatcher) (GObject     *object,
Packit ae235b
					      guint        n_pspecs,
Packit ae235b
					      GParamSpec **pspecs);
Packit ae235b
Packit ae235b
Packit ae235b
/* --- structures --- */
Packit ae235b
struct _GObjectNotifyContext
Packit ae235b
{
Packit ae235b
  GQuark                       quark_notify_queue;
Packit ae235b
  GObjectNotifyQueueDispatcher dispatcher;
Packit ae235b
  GTrashStack                 *_nqueue_trash; /* unused */
Packit ae235b
};
Packit ae235b
struct _GObjectNotifyQueue
Packit ae235b
{
Packit ae235b
  GObjectNotifyContext *context;
Packit ae235b
  GSList               *pspecs;
Packit ae235b
  guint16               n_pspecs;
Packit ae235b
  guint16               freeze_count;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC(notify_lock);
Packit ae235b
Packit ae235b
/* --- functions --- */
Packit ae235b
static void
Packit ae235b
g_object_notify_queue_free (gpointer data)
Packit ae235b
{
Packit ae235b
  GObjectNotifyQueue *nqueue = data;
Packit ae235b
Packit ae235b
  g_slist_free (nqueue->pspecs);
Packit ae235b
  g_slice_free (GObjectNotifyQueue, nqueue);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline GObjectNotifyQueue*
Packit ae235b
g_object_notify_queue_freeze (GObject		   *object,
Packit ae235b
			      GObjectNotifyContext *context)
Packit ae235b
{
Packit ae235b
  GObjectNotifyQueue *nqueue;
Packit ae235b
Packit ae235b
  G_LOCK(notify_lock);
Packit ae235b
  nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
Packit ae235b
  if (!nqueue)
Packit ae235b
    {
Packit ae235b
      nqueue = g_slice_new0 (GObjectNotifyQueue);
Packit ae235b
      nqueue->context = context;
Packit ae235b
      g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
Packit ae235b
				   nqueue, g_object_notify_queue_free);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (nqueue->freeze_count >= 65535)
Packit ae235b
    g_critical("Free queue for %s (%p) is larger than 65535,"
Packit ae235b
               " called g_object_freeze_notify() too often."
Packit ae235b
               " Forgot to call g_object_thaw_notify() or infinite loop",
Packit ae235b
               G_OBJECT_TYPE_NAME (object), object);
Packit ae235b
  else
Packit ae235b
    nqueue->freeze_count++;
Packit ae235b
  G_UNLOCK(notify_lock);
Packit ae235b
Packit ae235b
  return nqueue;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
g_object_notify_queue_thaw (GObject            *object,
Packit ae235b
			    GObjectNotifyQueue *nqueue)
Packit ae235b
{
Packit ae235b
  GObjectNotifyContext *context = nqueue->context;
Packit ae235b
  GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
Packit ae235b
  GSList *slist;
Packit ae235b
  guint n_pspecs = 0;
Packit ae235b
Packit ae235b
  g_return_if_fail (nqueue->freeze_count > 0);
Packit ae235b
  g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
Packit ae235b
Packit ae235b
  G_LOCK(notify_lock);
Packit ae235b
Packit ae235b
  /* Just make sure we never get into some nasty race condition */
Packit ae235b
  if (G_UNLIKELY(nqueue->freeze_count == 0)) {
Packit ae235b
    G_UNLOCK(notify_lock);
Packit ae235b
    g_warning ("%s: property-changed notification for %s(%p) is not frozen",
Packit ae235b
	       G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
Packit ae235b
    return;
Packit ae235b
  }
Packit ae235b
Packit ae235b
  nqueue->freeze_count--;
Packit ae235b
  if (nqueue->freeze_count) {
Packit ae235b
    G_UNLOCK(notify_lock);
Packit ae235b
    return;
Packit ae235b
  }
Packit ae235b
Packit ae235b
  pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
Packit ae235b
Packit ae235b
  for (slist = nqueue->pspecs; slist; slist = slist->next)
Packit ae235b
    {
Packit ae235b
      pspecs[n_pspecs++] = slist->data;
Packit ae235b
    }
Packit ae235b
  g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
Packit ae235b
Packit ae235b
  G_UNLOCK(notify_lock);
Packit ae235b
Packit ae235b
  if (n_pspecs)
Packit ae235b
    context->dispatcher (object, n_pspecs, pspecs);
Packit ae235b
  g_free (free_me);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
g_object_notify_queue_clear (GObject            *object,
Packit ae235b
			     GObjectNotifyQueue *nqueue)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (nqueue->freeze_count > 0);
Packit ae235b
Packit ae235b
  G_LOCK(notify_lock);
Packit ae235b
Packit ae235b
  g_slist_free (nqueue->pspecs);
Packit ae235b
  nqueue->pspecs = NULL;
Packit ae235b
  nqueue->n_pspecs = 0;
Packit ae235b
Packit ae235b
  G_UNLOCK(notify_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
g_object_notify_queue_add (GObject            *object,
Packit ae235b
			   GObjectNotifyQueue *nqueue,
Packit ae235b
			   GParamSpec	      *pspec)
Packit ae235b
{
Packit ae235b
  if (pspec->flags & G_PARAM_READABLE)
Packit ae235b
    {
Packit ae235b
      GParamSpec *redirect;
Packit ae235b
Packit ae235b
      G_LOCK(notify_lock);
Packit ae235b
Packit ae235b
      g_return_if_fail (nqueue->n_pspecs < 65535);
Packit ae235b
Packit ae235b
      redirect = g_param_spec_get_redirect_target (pspec);
Packit ae235b
      if (redirect)
Packit ae235b
	pspec = redirect;
Packit ae235b
	    
Packit ae235b
      /* we do the deduping in _thaw */
Packit ae235b
      if (g_slist_find (nqueue->pspecs, pspec) == NULL)
Packit ae235b
        {
Packit ae235b
          nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
Packit ae235b
          nqueue->n_pspecs++;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      G_UNLOCK(notify_lock);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* NB: This function is not threadsafe, do not ever use it if
Packit ae235b
 * you need a threadsafe notify queue.
Packit ae235b
 * Use g_object_notify_queue_freeze() to acquire the queue and
Packit ae235b
 * g_object_notify_queue_thaw() after you are done instead.
Packit ae235b
 */
Packit ae235b
static inline GObjectNotifyQueue*
Packit ae235b
g_object_notify_queue_from_object (GObject              *object,
Packit ae235b
                                   GObjectNotifyContext *context)
Packit ae235b
{
Packit ae235b
  return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
Packit ae235b
}
Packit ae235b
Packit ae235b
G_END_DECLS
Packit ae235b
Packit ae235b
#endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */