gobjectnotifyqueue.c 5.5 KB

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