From 3ca41fe1092a7de0ad5d43b167349edf8c1a3feb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim-Philipp=20M=C3=BCller?= Date: Tue, 18 Dec 2007 16:46:36 +0000 Subject: [PATCH] add g_async_queue_new_full() which takes a GDestroyNotify function to free * 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 | 9 +++++++++ docs/reference/glib/glib-sections.txt | 1 + glib/gasyncqueue.c | 23 +++++++++++++++++++++++ glib/gasyncqueue.h | 2 ++ 4 files changed, 35 insertions(+) diff --git a/ChangeLog b/ChangeLog index 6fbfefb..74f143a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2007-12-18 Tim-Philipp Müller + + * 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 * glib/gtestutils.[hc]: added g_test_trap_assert_stdout_unmatched() and diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index ed81477..0f8f583 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -671,6 +671,7 @@ g_thread_pool_get_max_idle_time async_queues GAsyncQueue g_async_queue_new +g_async_queue_new_full g_async_queue_ref g_async_queue_unref g_async_queue_push diff --git a/glib/gasyncqueue.c b/glib/gasyncqueue.c index dbaebde..94062ef 100644 --- a/glib/gasyncqueue.c +++ b/glib/gasyncqueue.c @@ -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); } diff --git a/glib/gasyncqueue.h b/glib/gasyncqueue.h index 413093f..7c860e8 100644 --- a/glib/gasyncqueue.h +++ b/glib/gasyncqueue.h @@ -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. -- 2.7.4