summaryrefslogtreecommitdiff
path: root/glib/gasyncqueue.c
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2015-06-18 10:26:14 -0400
committerMatthias Clasen <mclasen@redhat.com>2015-06-29 08:20:26 -0700
commitb662c6f09fe1a01bb1345f6cd7ab5a702eec5ee3 (patch)
treeef0ccd3f4f1f8c4de8d710aea71ea8f6dfc72e26 /glib/gasyncqueue.c
parent5574315b5207e959d162553e3bb5681b6264fe27 (diff)
downloadglib-b662c6f09fe1a01bb1345f6cd7ab5a702eec5ee3.tar.gz
GAsyncQueue: Add some useful api
The underlying queue supports removing and pushing items to the front, and these operations can sometimes be useful. https://bugzilla.gnome.org/show_bug.cgi?id=751160
Diffstat (limited to 'glib/gasyncqueue.c')
-rw-r--r--glib/gasyncqueue.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/glib/gasyncqueue.c b/glib/gasyncqueue.c
index 8ed66ab6d..5fc7300c6 100644
--- a/glib/gasyncqueue.c
+++ b/glib/gasyncqueue.c
@@ -785,6 +785,106 @@ g_async_queue_sort_unlocked (GAsyncQueue *queue,
&sd);
}
+/**
+ * g_async_queue_remove:
+ * @queue: a #GAsyncQueue
+ * @data: the @data to remove from the @queue
+ *
+ * Remove an item from the queue. This function does not block.
+ *
+ * Returns: %TRUE if the item was removed
+ *
+ * Since: 2.46
+ */
+gboolean
+g_async_queue_remove (GAsyncQueue *queue,
+ gpointer data)
+{
+ gboolean ret;
+
+ g_return_val_if_fail (queue != NULL, FALSE);
+ g_return_val_if_fail (data != NULL, FALSE);
+
+ g_mutex_lock (&queue->mutex);
+ ret = g_async_queue_remove_unlocked (queue, data);
+ g_mutex_unlock (&queue->mutex);
+
+ return ret;
+}
+
+/**
+ * g_async_queue_remove_unlocked:
+ * @queue: a #GAsyncQueue
+ * @data: the @data to remove from the @queue
+ *
+ * Remove an item from the queue. This function does not block.
+ *
+ * This function must be called while holding the @queue's lock.
+ *
+ * Returns: %TRUE if the item was removed
+ *
+ * Since: 2.46
+ */
+gboolean
+g_async_queue_remove_unlocked (GAsyncQueue *queue,
+ gpointer data)
+{
+ g_return_val_if_fail (queue != NULL, FALSE);
+ g_return_val_if_fail (data != NULL, FALSE);
+
+ return g_queue_remove (&queue->queue, data);
+}
+
+/**
+ * g_async_queue_push_front:
+ * @queue: a #GAsyncQueue
+ * @data: @data to push into the @queue
+ *
+ * Pushes the @data into the @queue. @data must not be %NULL.
+ * In contrast to g_async_queue_push(), this function
+ * pushes the new item ahead of the items already in the queue,
+ * so that it will be the next one to be popped off the queue.
+ *
+ * Since: 2.46
+ */
+void
+g_async_queue_push_front (GAsyncQueue *queue,
+ gpointer data)
+{
+ g_return_if_fail (queue != NULL);
+ g_return_if_fail (data != NULL);
+
+ g_mutex_lock (&queue->mutex);
+ g_async_queue_push_front_unlocked (queue, data);
+ g_mutex_unlock (&queue->mutex);
+}
+
+/**
+ * g_async_queue_push_front_unlocked:
+ * @queue: a #GAsyncQueue
+ * @data: @data to push into the @queue
+ *
+ * Pushes the @data into the @queue. @data must not be %NULL.
+ * In contrast to g_async_queue_push_unlocked(), this function
+ * pushes the new item ahead of the items already in the queue,
+ * so that it will be the next one to be popped off the queue.
+ *
+ * This function must be called while holding the @queue's lock.
+ *
+ * Since: 2.46
+ */
+void
+g_async_queue_push_front_unlocked (GAsyncQueue *queue,
+ gpointer data)
+{
+ g_return_if_fail (queue != NULL);
+ g_return_if_fail (data != NULL);
+
+ g_queue_push_tail (&queue->queue, data);
+ if (queue->waiting_threads > 0)
+ g_cond_signal (&queue->cond);
+}
+
/*
* Private API
*/