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
This commit is contained in:
Tim-Philipp Müller 2007-12-18 16:46:36 +00:00
parent ac1723ea97
commit 3ca41fe109
4 changed files with 35 additions and 0 deletions

View File

@ -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

View 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

View File

@ -35,6 +35,7 @@ struct _GAsyncQueue
GMutex *mutex;
GCond *cond;
GQueue *queue;
GDestroyNotify item_free_func;
guint waiting_threads;
gint32 ref_count;
};
@ -60,9 +61,29 @@ 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);
}

View 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.