add g_async_queue_new_full() which takes a GDestroyNotify function to free
authorTim-Philipp Müller <tpm@src.gnome.org>
Tue, 18 Dec 2007 16:46:36 +0000 (16:46 +0000)
committerTim-Philipp Müller <tpm@src.gnome.org>
Tue, 18 Dec 2007 16:46:36 +0000 (16:46 +0000)
* docs/reference/glib/glib-sections.txt:
* glib/gasyncqueue.c: (g_async_queue_new), (g_async_queue_new_full),
  (g_async_queue_unref):
* glib/gasyncqueue.h: add g_async_queue_new_full() which takes a
  GDestroyNotify function to free any remaining queue items when the
  queue is destroyed after the final atomic unref (#367550).

svn path=/trunk/; revision=6152

ChangeLog
docs/reference/glib/glib-sections.txt
glib/gasyncqueue.c
glib/gasyncqueue.h

index 6fbfefb..74f143a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2007-12-18  Tim-Philipp Müller  <tim at centricular dot net>
+
+       * docs/reference/glib/glib-sections.txt:
+       * glib/gasyncqueue.c: (g_async_queue_new), (g_async_queue_new_full),
+         (g_async_queue_unref):
+       * glib/gasyncqueue.h: add g_async_queue_new_full() which takes a
+         GDestroyNotify function to free any remaining queue items when the
+         queue is destroyed after the final atomic unref (#367550).
+
 2007-12-18 13:45:23  Tim Janik  <timj@imendio.com>
 
        * glib/gtestutils.[hc]: added g_test_trap_assert_stdout_unmatched() and
index ed81477..0f8f583 100644 (file)
@@ -671,6 +671,7 @@ g_thread_pool_get_max_idle_time
 <FILE>async_queues</FILE>
 GAsyncQueue
 g_async_queue_new
+g_async_queue_new_full
 g_async_queue_ref
 g_async_queue_unref
 g_async_queue_push
index dbaebde..94062ef 100644 (file)
@@ -35,6 +35,7 @@ struct _GAsyncQueue
   GMutex *mutex;
   GCond *cond;
   GQueue *queue;
+  GDestroyNotify item_free_func;
   guint waiting_threads;
   gint32 ref_count;
 };
@@ -60,10 +61,30 @@ g_async_queue_new (void)
   retval->queue = g_queue_new ();
   retval->waiting_threads = 0;
   retval->ref_count = 1;
+  retval->item_free_func = NULL;
   return retval;
 }
 
 /**
+ * g_async_queue_new_full:
+ * 
+ * Creates a new asynchronous queue with an initial reference count of 1 and
+ * sets up a destroy notify function that is used to free any remaining
+ * queue items when the queue is destroyed after the final unref.
+ *
+ * Return value: the new #GAsyncQueue.
+ *
+ * Since: 2.16
+ **/
+GAsyncQueue*
+g_async_queue_new_full (GDestroyNotify item_free_func)
+{
+  GAsyncQueue *async_queue = g_async_queue_new ();
+  async_queue->item_free_func = item_free_func;
+  return async_queue;
+}
+
+/**
  * g_async_queue_ref:
  * @queue: a #GAsyncQueue.
  *
@@ -147,6 +168,8 @@ g_async_queue_unref (GAsyncQueue *queue)
       g_mutex_free (queue->mutex);
       if (queue->cond)
        g_cond_free (queue->cond);
+      if (queue->item_free_func)
+        g_queue_foreach (queue->queue, (GFunc) queue->item_free_func, NULL);
       g_queue_free (queue->queue);
       g_free (queue);
     }
index 413093f..7c860e8 100644 (file)
@@ -38,6 +38,8 @@ typedef struct _GAsyncQueue GAsyncQueue;
 /* Get a new GAsyncQueue with the ref_count 1 */
 GAsyncQueue*  g_async_queue_new                 (void);
 
+GAsyncQueue*  g_async_queue_new_full            (GDestroyNotify item_free_func);
+
 /* Lock and unlock a GAsyncQueue. All functions lock the queue for
  * themselves, but in certain cirumstances you want to hold the lock longer,
  * thus you lock the queue, call the *_unlocked functions and unlock it again.