gobjectnotifyqueue.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* GObject - GLib Type, Object, Parameter and Signal Library
  2. * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
  3. *
  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.
  8. *
  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.
  13. *
  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/>.
  16. */
  17. /* WARNING:
  18. *
  19. * This file is INSTALLED and other projects (outside of glib)
  20. * #include its contents.
  21. */
  22. #ifndef __G_OBJECT_NOTIFY_QUEUE_H__
  23. #define __G_OBJECT_NOTIFY_QUEUE_H__
  24. #include <string.h> /* memset */
  25. #include <glib-object.h>
  26. G_BEGIN_DECLS
  27. /* --- typedefs --- */
  28. typedef struct _GObjectNotifyContext GObjectNotifyContext;
  29. typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
  30. typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
  31. guint n_pspecs,
  32. GParamSpec **pspecs);
  33. /* --- structures --- */
  34. struct _GObjectNotifyContext
  35. {
  36. GQuark quark_notify_queue;
  37. GObjectNotifyQueueDispatcher dispatcher;
  38. GTrashStack *_nqueue_trash; /* unused */
  39. };
  40. struct _GObjectNotifyQueue
  41. {
  42. GObjectNotifyContext *context;
  43. GSList *pspecs;
  44. guint16 n_pspecs;
  45. guint16 freeze_count;
  46. };
  47. G_LOCK_DEFINE_STATIC(notify_lock);
  48. /* --- functions --- */
  49. static void
  50. g_object_notify_queue_free (gpointer data)
  51. {
  52. GObjectNotifyQueue *nqueue = data;
  53. g_slist_free (nqueue->pspecs);
  54. g_slice_free (GObjectNotifyQueue, nqueue);
  55. }
  56. static inline GObjectNotifyQueue*
  57. g_object_notify_queue_freeze (GObject *object,
  58. GObjectNotifyContext *context)
  59. {
  60. GObjectNotifyQueue *nqueue;
  61. G_LOCK(notify_lock);
  62. nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
  63. if (!nqueue)
  64. {
  65. nqueue = g_slice_new0 (GObjectNotifyQueue);
  66. nqueue->context = context;
  67. g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
  68. nqueue, g_object_notify_queue_free);
  69. }
  70. if (nqueue->freeze_count >= 65535)
  71. g_critical("Free queue for %s (%p) is larger than 65535,"
  72. " called g_object_freeze_notify() too often."
  73. " Forgot to call g_object_thaw_notify() or infinite loop",
  74. G_OBJECT_TYPE_NAME (object), object);
  75. else
  76. nqueue->freeze_count++;
  77. G_UNLOCK(notify_lock);
  78. return nqueue;
  79. }
  80. static inline void
  81. g_object_notify_queue_thaw (GObject *object,
  82. GObjectNotifyQueue *nqueue)
  83. {
  84. GObjectNotifyContext *context = nqueue->context;
  85. GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
  86. GSList *slist;
  87. guint n_pspecs = 0;
  88. g_return_if_fail (nqueue->freeze_count > 0);
  89. g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
  90. G_LOCK(notify_lock);
  91. /* Just make sure we never get into some nasty race condition */
  92. if (G_UNLIKELY(nqueue->freeze_count == 0)) {
  93. G_UNLOCK(notify_lock);
  94. g_warning ("%s: property-changed notification for %s(%p) is not frozen",
  95. G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
  96. return;
  97. }
  98. nqueue->freeze_count--;
  99. if (nqueue->freeze_count) {
  100. G_UNLOCK(notify_lock);
  101. return;
  102. }
  103. pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
  104. for (slist = nqueue->pspecs; slist; slist = slist->next)
  105. {
  106. pspecs[n_pspecs++] = slist->data;
  107. }
  108. g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
  109. G_UNLOCK(notify_lock);
  110. if (n_pspecs)
  111. context->dispatcher (object, n_pspecs, pspecs);
  112. g_free (free_me);
  113. }
  114. static inline void
  115. g_object_notify_queue_clear (GObject *object,
  116. GObjectNotifyQueue *nqueue)
  117. {
  118. g_return_if_fail (nqueue->freeze_count > 0);
  119. G_LOCK(notify_lock);
  120. g_slist_free (nqueue->pspecs);
  121. nqueue->pspecs = NULL;
  122. nqueue->n_pspecs = 0;
  123. G_UNLOCK(notify_lock);
  124. }
  125. static inline void
  126. g_object_notify_queue_add (GObject *object,
  127. GObjectNotifyQueue *nqueue,
  128. GParamSpec *pspec)
  129. {
  130. if (pspec->flags & G_PARAM_READABLE)
  131. {
  132. GParamSpec *redirect;
  133. G_LOCK(notify_lock);
  134. g_return_if_fail (nqueue->n_pspecs < 65535);
  135. redirect = g_param_spec_get_redirect_target (pspec);
  136. if (redirect)
  137. pspec = redirect;
  138. /* we do the deduping in _thaw */
  139. if (g_slist_find (nqueue->pspecs, pspec) == NULL)
  140. {
  141. nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
  142. nqueue->n_pspecs++;
  143. }
  144. G_UNLOCK(notify_lock);
  145. }
  146. }
  147. /* NB: This function is not threadsafe, do not ever use it if
  148. * you need a threadsafe notify queue.
  149. * Use g_object_notify_queue_freeze() to acquire the queue and
  150. * g_object_notify_queue_thaw() after you are done instead.
  151. */
  152. static inline GObjectNotifyQueue*
  153. g_object_notify_queue_from_object (GObject *object,
  154. GObjectNotifyContext *context)
  155. {
  156. return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
  157. }
  158. G_END_DECLS
  159. #endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */