2000-04-28 14:24:53 +02:00
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* GAsyncQueue: asynchronous queue implementation, based on GQueue.
|
2000-04-28 14:24:53 +02:00
|
|
|
* Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 13:02:02 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2000-04-28 14:24:53 +02:00
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 12:47:07 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2000-04-28 14:24:53 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 13:02:02 +02:00
|
|
|
* Lesser General Public License for more details.
|
2000-04-28 14:24:53 +02:00
|
|
|
*
|
2000-07-26 13:02:02 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2000-04-28 14:24:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MT safe
|
|
|
|
*/
|
|
|
|
|
2002-12-04 02:27:44 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-05-05 18:12:04 +02:00
|
|
|
#include "gasyncqueue.h"
|
2011-10-02 00:42:48 +02:00
|
|
|
#include "gasyncqueueprivate.h"
|
2010-05-05 18:12:04 +02:00
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
#include "gmain.h"
|
2010-05-05 18:12:04 +02:00
|
|
|
#include "gmem.h"
|
|
|
|
#include "gqueue.h"
|
|
|
|
#include "gtestutils.h"
|
2012-02-08 14:26:36 +01:00
|
|
|
#include "gtimer.h"
|
2010-05-05 18:12:04 +02:00
|
|
|
#include "gthread.h"
|
2011-10-16 02:06:32 +02:00
|
|
|
#include "deprecated/gthread.h"
|
2010-05-05 18:12:04 +02:00
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
2010-07-11 03:26:28 +02:00
|
|
|
/**
|
2011-02-01 19:17:23 +01:00
|
|
|
* SECTION:async_queues
|
2010-07-11 03:26:28 +02:00
|
|
|
* @title: Asynchronous Queues
|
|
|
|
* @short_description: asynchronous communication between threads
|
2011-10-02 01:24:24 +02:00
|
|
|
* @see_also: #GThreadPool
|
2010-07-11 03:26:28 +02:00
|
|
|
*
|
|
|
|
* Often you need to communicate between different threads. In general
|
|
|
|
* it's safer not to do this by shared memory, but by explicit message
|
|
|
|
* passing. These messages only make sense asynchronously for
|
|
|
|
* multi-threaded applications though, as a synchronous operation could
|
|
|
|
* as well be done in the same thread.
|
|
|
|
*
|
|
|
|
* Asynchronous queues are an exception from most other GLib data
|
|
|
|
* structures, as they can be used simultaneously from multiple threads
|
|
|
|
* without explicit locking and they bring their own builtin reference
|
|
|
|
* counting. This is because the nature of an asynchronous queue is that
|
|
|
|
* it will always be used by at least 2 concurrent threads.
|
|
|
|
*
|
|
|
|
* For using an asynchronous queue you first have to create one with
|
2011-10-02 01:24:24 +02:00
|
|
|
* g_async_queue_new(). #GAsyncQueue structs are reference counted,
|
|
|
|
* use g_async_queue_ref() and g_async_queue_unref() to manage your
|
|
|
|
* references.
|
|
|
|
*
|
|
|
|
* A thread which wants to send a message to that queue simply calls
|
2010-07-11 03:26:28 +02:00
|
|
|
* g_async_queue_push() to push the message to the queue.
|
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* A thread which is expecting messages from an asynchronous queue
|
2010-07-11 03:26:28 +02:00
|
|
|
* simply calls g_async_queue_pop() for that queue. If no message is
|
|
|
|
* available in the queue at that point, the thread is now put to sleep
|
|
|
|
* until a message arrives. The message will be removed from the queue
|
|
|
|
* and returned. The functions g_async_queue_try_pop() and
|
2012-02-08 14:26:36 +01:00
|
|
|
* g_async_queue_timeout_pop() can be used to only check for the presence
|
2010-07-11 03:26:28 +02:00
|
|
|
* of messages or to only wait a certain time for messages respectively.
|
|
|
|
*
|
|
|
|
* For almost every function there exist two variants, one that locks
|
|
|
|
* the queue and one that doesn't. That way you can hold the queue lock
|
|
|
|
* (acquire it with g_async_queue_lock() and release it with
|
|
|
|
* g_async_queue_unlock()) over multiple queue accessing instructions.
|
|
|
|
* This can be necessary to ensure the integrity of the queue, but should
|
|
|
|
* only be used when really necessary, as it can make your life harder
|
|
|
|
* if used unwisely. Normally you should only use the locking function
|
2011-10-02 01:24:24 +02:00
|
|
|
* variants (those without the _unlocked suffix).
|
|
|
|
*
|
|
|
|
* In many cases, it may be more convenient to use #GThreadPool when
|
|
|
|
* you need to distribute work to a set of worker threads instead of
|
|
|
|
* using #GAsyncQueue manually. #GThreadPool uses a GAsyncQueue
|
|
|
|
* internally.
|
2010-07-11 03:26:28 +02:00
|
|
|
*/
|
2002-12-04 02:27:44 +01:00
|
|
|
|
2010-07-11 03:26:28 +02:00
|
|
|
/**
|
|
|
|
* GAsyncQueue:
|
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* The GAsyncQueue struct is an opaque data structure which represents
|
2010-07-11 03:26:28 +02:00
|
|
|
* an asynchronous queue. It should only be accessed through the
|
2014-02-01 18:19:04 +01:00
|
|
|
* g_async_queue_* functions.
|
2010-07-11 03:26:28 +02:00
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
struct _GAsyncQueue
|
|
|
|
{
|
2011-09-17 23:56:33 +02:00
|
|
|
GMutex mutex;
|
2011-10-02 02:09:35 +02:00
|
|
|
GCond cond;
|
2010-08-13 05:57:26 +02:00
|
|
|
GQueue queue;
|
2007-12-18 17:46:36 +01:00
|
|
|
GDestroyNotify item_free_func;
|
2000-04-28 14:24:53 +02:00
|
|
|
guint waiting_threads;
|
2011-05-29 04:33:37 +02:00
|
|
|
gint ref_count;
|
2000-04-28 14:24:53 +02:00
|
|
|
};
|
|
|
|
|
2011-10-02 01:24:24 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2005-12-07 13:09:44 +01:00
|
|
|
GCompareDataFunc func;
|
|
|
|
gpointer user_data;
|
|
|
|
} SortData;
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_new:
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
|
|
|
* Creates a new asynchronous queue.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a new #GAsyncQueue. Free with g_async_queue_unref()
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
|
|
|
GAsyncQueue *
|
2004-10-24 03:37:42 +02:00
|
|
|
g_async_queue_new (void)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
2011-10-02 01:24:24 +02:00
|
|
|
return g_async_queue_new_full (NULL);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2007-12-18 17:46:36 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_new_full:
|
2008-02-23 06:06:18 +01:00
|
|
|
* @item_free_func: function to free queue elements
|
2007-12-18 17:46:36 +01:00
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* Creates a new asynchronous queue 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.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a new #GAsyncQueue. Free with g_async_queue_unref()
|
2007-12-18 17:46:36 +01:00
|
|
|
*
|
|
|
|
* Since: 2.16
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
|
|
|
GAsyncQueue *
|
2007-12-18 17:46:36 +01:00
|
|
|
g_async_queue_new_full (GDestroyNotify item_free_func)
|
|
|
|
{
|
2011-10-02 01:24:24 +02:00
|
|
|
GAsyncQueue *queue;
|
|
|
|
|
|
|
|
queue = g_new (GAsyncQueue, 1);
|
|
|
|
g_mutex_init (&queue->mutex);
|
2011-10-02 02:09:35 +02:00
|
|
|
g_cond_init (&queue->cond);
|
2011-10-02 01:24:24 +02:00
|
|
|
g_queue_init (&queue->queue);
|
|
|
|
queue->waiting_threads = 0;
|
|
|
|
queue->ref_count = 1;
|
2011-10-04 04:18:01 +02:00
|
|
|
queue->item_free_func = item_free_func;
|
2011-10-02 01:24:24 +02:00
|
|
|
|
|
|
|
return queue;
|
2007-12-18 17:46:36 +01:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_ref:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* Increases the reference count of the asynchronous @queue by 1.
|
|
|
|
* You do not need to hold the lock to call this function.
|
2004-11-08 19:26:56 +01:00
|
|
|
*
|
|
|
|
* Returns: the @queue that was passed in (since 2.6)
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2004-11-08 19:26:56 +01:00
|
|
|
GAsyncQueue *
|
2000-04-28 14:24:53 +02:00
|
|
|
g_async_queue_ref (GAsyncQueue *queue)
|
|
|
|
{
|
2004-11-08 19:49:35 +01:00
|
|
|
g_return_val_if_fail (queue, NULL);
|
2011-10-02 01:24:24 +02:00
|
|
|
|
2004-02-27 17:06:31 +01:00
|
|
|
g_atomic_int_inc (&queue->ref_count);
|
2004-11-08 19:26:56 +01:00
|
|
|
|
|
|
|
return queue;
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_ref_unlocked:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
2004-02-27 17:06:31 +01:00
|
|
|
* Increases the reference count of the asynchronous @queue by 1.
|
2005-12-05 14:50:39 +01:00
|
|
|
*
|
2012-04-03 20:17:00 +02:00
|
|
|
* Deprecated: 2.8: Reference counting is done atomically.
|
2005-12-05 14:50:39 +01:00
|
|
|
* so g_async_queue_ref() can be used regardless of the @queue's
|
|
|
|
* lock.
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
|
|
|
void
|
2000-04-28 14:24:53 +02:00
|
|
|
g_async_queue_ref_unlocked (GAsyncQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue);
|
2011-10-02 01:24:24 +02:00
|
|
|
|
2004-02-27 17:06:31 +01:00
|
|
|
g_atomic_int_inc (&queue->ref_count);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_unref_and_unlock:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
|
|
|
* Decreases the reference count of the asynchronous @queue by 1
|
|
|
|
* and releases the lock. This function must be called while holding
|
|
|
|
* the @queue's lock. If the reference count went to 0, the @queue
|
|
|
|
* will be destroyed and the memory allocated will be freed.
|
2005-12-05 14:50:39 +01:00
|
|
|
*
|
2012-04-03 20:17:00 +02:00
|
|
|
* Deprecated: 2.8: Reference counting is done atomically.
|
2005-12-05 14:50:39 +01:00
|
|
|
* so g_async_queue_unref() can be used regardless of the @queue's
|
|
|
|
* lock.
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
|
|
|
void
|
2000-04-28 14:24:53 +02:00
|
|
|
g_async_queue_unref_and_unlock (GAsyncQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2004-02-27 17:06:31 +01:00
|
|
|
g_async_queue_unref (queue);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_unref:
|
|
|
|
* @queue: a #GAsyncQueue.
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
|
|
|
* Decreases the reference count of the asynchronous @queue by 1.
|
|
|
|
*
|
|
|
|
* If the reference count went to 0, the @queue will be destroyed
|
|
|
|
* and the memory allocated will be freed. So you are not allowed
|
|
|
|
* to use the @queue afterwards, as it might have disappeared.
|
|
|
|
* You do not need to hold the lock to call this function.
|
|
|
|
*/
|
|
|
|
void
|
2000-04-28 14:24:53 +02:00
|
|
|
g_async_queue_unref (GAsyncQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue);
|
2011-10-02 01:24:24 +02:00
|
|
|
|
2004-02-27 17:06:31 +01:00
|
|
|
if (g_atomic_int_dec_and_test (&queue->ref_count))
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue->waiting_threads == 0);
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_clear (&queue->mutex);
|
2011-10-02 02:09:35 +02:00
|
|
|
g_cond_clear (&queue->cond);
|
2007-12-18 17:46:36 +01:00
|
|
|
if (queue->item_free_func)
|
2010-08-13 05:57:26 +02:00
|
|
|
g_queue_foreach (&queue->queue, (GFunc) queue->item_free_func, NULL);
|
|
|
|
g_queue_clear (&queue->queue);
|
2004-02-27 17:06:31 +01:00
|
|
|
g_free (queue);
|
|
|
|
}
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_lock:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
|
|
|
* Acquires the @queue's lock. If another thread is already
|
|
|
|
* holding the lock, this call will block until the lock
|
|
|
|
* becomes available.
|
|
|
|
*
|
|
|
|
* Call g_async_queue_unlock() to drop the lock again.
|
|
|
|
*
|
|
|
|
* While holding the lock, you can only call the
|
2014-02-01 18:19:04 +01:00
|
|
|
* g_async_queue_*_unlocked() functions on @queue. Otherwise,
|
|
|
|
* deadlock may occur.
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
void
|
|
|
|
g_async_queue_lock (GAsyncQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_unlock:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
2001-12-16 20:31:36 +01:00
|
|
|
* Releases the queue's lock.
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
|
|
|
* Calling this function when you have not acquired
|
|
|
|
* the with g_async_queue_lock() leads to undefined
|
|
|
|
* behaviour.
|
|
|
|
*/
|
|
|
|
void
|
2000-04-28 14:24:53 +02:00
|
|
|
g_async_queue_unlock (GAsyncQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_push:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
* @data: @data to push into the @queue
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2001-12-16 20:31:36 +01:00
|
|
|
* Pushes the @data into the @queue. @data must not be %NULL.
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
void
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_push (GAsyncQueue *queue,
|
|
|
|
gpointer data)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
|
|
|
g_return_if_fail (queue);
|
|
|
|
g_return_if_fail (data);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
g_async_queue_push_unlocked (queue, data);
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_push_unlocked:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
* @data: @data to push into the @queue
|
|
|
|
*
|
|
|
|
* Pushes the @data into the @queue. @data must not be %NULL.
|
|
|
|
*
|
|
|
|
* This function must be called while holding the @queue's lock.
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
void
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_push_unlocked (GAsyncQueue *queue,
|
|
|
|
gpointer data)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
|
|
|
g_return_if_fail (queue);
|
|
|
|
g_return_if_fail (data);
|
|
|
|
|
2010-08-13 05:57:26 +02:00
|
|
|
g_queue_push_head (&queue->queue, data);
|
2004-02-18 13:30:01 +01:00
|
|
|
if (queue->waiting_threads > 0)
|
2011-10-02 02:09:35 +02:00
|
|
|
g_cond_signal (&queue->cond);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2005-12-05 16:08:45 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_push_sorted:
|
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
* @data: the @data to push into the @queue
|
2011-10-02 01:24:24 +02:00
|
|
|
* @func: the #GCompareDataFunc is used to sort @queue
|
2005-12-05 16:08:45 +01:00
|
|
|
* @user_data: user data passed to @func.
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
2005-12-05 16:08:45 +01:00
|
|
|
* Inserts @data into @queue using @func to determine the new
|
2011-10-02 01:24:24 +02:00
|
|
|
* position.
|
|
|
|
*
|
2005-12-05 16:08:45 +01:00
|
|
|
* This function requires that the @queue is sorted before pushing on
|
2011-10-02 01:24:24 +02:00
|
|
|
* new elements, see g_async_queue_sort().
|
|
|
|
*
|
2005-12-05 16:08:45 +01:00
|
|
|
* This function will lock @queue before it sorts the queue and unlock
|
|
|
|
* it when it is finished.
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
|
|
|
* For an example of @func see g_async_queue_sort().
|
2005-12-05 16:08:45 +01:00
|
|
|
*
|
|
|
|
* Since: 2.10
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2005-12-05 16:08:45 +01:00
|
|
|
void
|
|
|
|
g_async_queue_push_sorted (GAsyncQueue *queue,
|
2011-10-02 01:24:24 +02:00
|
|
|
gpointer data,
|
|
|
|
GCompareDataFunc func,
|
|
|
|
gpointer user_data)
|
2005-12-05 16:08:45 +01:00
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2005-12-05 16:08:45 +01:00
|
|
|
g_async_queue_push_sorted_unlocked (queue, data, func, user_data);
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2005-12-05 16:08:45 +01:00
|
|
|
}
|
|
|
|
|
2011-10-02 01:24:24 +02:00
|
|
|
static gint
|
|
|
|
g_async_queue_invert_compare (gpointer v1,
|
|
|
|
gpointer v2,
|
|
|
|
SortData *sd)
|
2005-12-07 13:09:44 +01:00
|
|
|
{
|
|
|
|
return -sd->func (v1, v2, sd->user_data);
|
|
|
|
}
|
|
|
|
|
2005-12-05 16:08:45 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_push_sorted_unlocked:
|
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
* @data: the @data to push into the @queue
|
2011-10-02 01:24:24 +02:00
|
|
|
* @func: the #GCompareDataFunc is used to sort @queue
|
2005-12-05 16:08:45 +01:00
|
|
|
* @user_data: user data passed to @func.
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
2005-12-05 16:08:45 +01:00
|
|
|
* Inserts @data into @queue using @func to determine the new
|
|
|
|
* position.
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
|
|
|
* The sort function @func is passed two elements of the @queue.
|
|
|
|
* It should return 0 if they are equal, a negative value if the
|
|
|
|
* first element should be higher in the @queue or a positive value
|
|
|
|
* if the first element should be lower in the @queue than the second
|
|
|
|
* element.
|
|
|
|
*
|
2005-12-05 16:08:45 +01:00
|
|
|
* This function requires that the @queue is sorted before pushing on
|
2011-10-02 01:24:24 +02:00
|
|
|
* new elements, see g_async_queue_sort().
|
|
|
|
*
|
|
|
|
* This function must be called while holding the @queue's lock.
|
|
|
|
*
|
|
|
|
* For an example of @func see g_async_queue_sort().
|
2005-12-05 16:08:45 +01:00
|
|
|
*
|
|
|
|
* Since: 2.10
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2005-12-05 16:08:45 +01:00
|
|
|
void
|
|
|
|
g_async_queue_push_sorted_unlocked (GAsyncQueue *queue,
|
2011-10-02 01:24:24 +02:00
|
|
|
gpointer data,
|
|
|
|
GCompareDataFunc func,
|
|
|
|
gpointer user_data)
|
2005-12-05 16:08:45 +01:00
|
|
|
{
|
2005-12-07 13:09:44 +01:00
|
|
|
SortData sd;
|
2011-10-02 01:24:24 +02:00
|
|
|
|
2005-12-05 16:08:45 +01:00
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
|
2005-12-07 13:09:44 +01:00
|
|
|
sd.func = func;
|
|
|
|
sd.user_data = user_data;
|
2005-12-05 16:08:45 +01:00
|
|
|
|
2010-08-13 05:57:26 +02:00
|
|
|
g_queue_insert_sorted (&queue->queue,
|
2011-10-02 01:24:24 +02:00
|
|
|
data,
|
|
|
|
(GCompareDataFunc)g_async_queue_invert_compare,
|
|
|
|
&sd);
|
2006-01-16 23:14:17 +01:00
|
|
|
if (queue->waiting_threads > 0)
|
2011-10-02 02:09:35 +02:00
|
|
|
g_cond_signal (&queue->cond);
|
2005-12-05 16:08:45 +01:00
|
|
|
}
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
static gpointer
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_pop_intern_unlocked (GAsyncQueue *queue,
|
2011-10-02 02:11:38 +02:00
|
|
|
gboolean wait,
|
2012-02-08 14:26:36 +01:00
|
|
|
gint64 end_time)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
|
|
|
gpointer retval;
|
|
|
|
|
2011-10-02 02:16:32 +02:00
|
|
|
if (!g_queue_peek_tail_link (&queue->queue) && wait)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
2011-10-02 02:16:32 +02:00
|
|
|
queue->waiting_threads++;
|
|
|
|
while (!g_queue_peek_tail_link (&queue->queue))
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
2012-02-08 14:26:36 +01:00
|
|
|
if (end_time == -1)
|
|
|
|
g_cond_wait (&queue->cond, &queue->mutex);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!g_cond_wait_until (&queue->cond, &queue->mutex, end_time))
|
|
|
|
break;
|
|
|
|
}
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
2011-10-02 02:16:32 +02:00
|
|
|
queue->waiting_threads--;
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 05:57:26 +02:00
|
|
|
retval = g_queue_pop_tail (&queue->queue);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
g_assert (retval || !wait || end_time > 0);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_pop:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* Pops data from the @queue. If @queue is empty, this function
|
|
|
|
* blocks until data becomes available.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
gpointer
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_pop (GAsyncQueue *queue)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
|
|
|
gpointer retval;
|
|
|
|
|
|
|
|
g_return_val_if_fail (queue, NULL);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2012-02-08 14:26:36 +01:00
|
|
|
retval = g_async_queue_pop_intern_unlocked (queue, TRUE, -1);
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_pop_unlocked:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
|
|
|
* Pops data from the @queue. If @queue is empty, this function
|
|
|
|
* blocks until data becomes available.
|
|
|
|
*
|
|
|
|
* This function must be called while holding the @queue's lock.
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue.
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
gpointer
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_pop_unlocked (GAsyncQueue *queue)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue, NULL);
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
return g_async_queue_pop_intern_unlocked (queue, TRUE, -1);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_try_pop:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
|
|
|
* Tries to pop data from the @queue. If no data is available,
|
|
|
|
* %NULL is returned.
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue or %NULL, when no data is
|
2011-10-02 01:24:24 +02:00
|
|
|
* available immediately.
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
gpointer
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_try_pop (GAsyncQueue *queue)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
|
|
|
gpointer retval;
|
|
|
|
|
|
|
|
g_return_val_if_fail (queue, NULL);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2012-02-08 14:26:36 +01:00
|
|
|
retval = g_async_queue_pop_intern_unlocked (queue, FALSE, -1);
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_try_pop_unlocked:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
|
|
|
* Tries to pop data from the @queue. If no data is available,
|
|
|
|
* %NULL is returned.
|
|
|
|
*
|
|
|
|
* This function must be called while holding the @queue's lock.
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue or %NULL, when no data is
|
2011-10-02 01:24:24 +02:00
|
|
|
* available immediately.
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
gpointer
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_try_pop_unlocked (GAsyncQueue *queue)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue, NULL);
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
return g_async_queue_pop_intern_unlocked (queue, FALSE, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_async_queue_timeout_pop:
|
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
* @timeout: the number of microseconds to wait
|
|
|
|
*
|
|
|
|
* Pops data from the @queue. If the queue is empty, blocks for
|
|
|
|
* @timeout microseconds, or until data becomes available.
|
|
|
|
*
|
|
|
|
* If no data is received before the timeout, %NULL is returned.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue or %NULL, when no data is
|
2012-02-08 14:26:36 +01:00
|
|
|
* received before the timeout.
|
|
|
|
*/
|
|
|
|
gpointer
|
|
|
|
g_async_queue_timeout_pop (GAsyncQueue *queue,
|
|
|
|
guint64 timeout)
|
|
|
|
{
|
|
|
|
gint64 end_time = g_get_monotonic_time () + timeout;
|
|
|
|
gpointer retval;
|
|
|
|
|
2018-07-29 16:12:51 +02:00
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
g_mutex_lock (&queue->mutex);
|
|
|
|
retval = g_async_queue_pop_intern_unlocked (queue, TRUE, end_time);
|
|
|
|
g_mutex_unlock (&queue->mutex);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_async_queue_timeout_pop_unlocked:
|
|
|
|
* @queue: a #GAsyncQueue
|
2012-02-18 12:56:09 +01:00
|
|
|
* @timeout: the number of microseconds to wait
|
2012-02-08 14:26:36 +01:00
|
|
|
*
|
|
|
|
* Pops data from the @queue. If the queue is empty, blocks for
|
|
|
|
* @timeout microseconds, or until data becomes available.
|
|
|
|
*
|
|
|
|
* If no data is received before the timeout, %NULL is returned.
|
|
|
|
*
|
|
|
|
* This function must be called while holding the @queue's lock.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue or %NULL, when no data is
|
2012-02-08 14:26:36 +01:00
|
|
|
* received before the timeout.
|
|
|
|
*/
|
|
|
|
gpointer
|
|
|
|
g_async_queue_timeout_pop_unlocked (GAsyncQueue *queue,
|
|
|
|
guint64 timeout)
|
|
|
|
{
|
|
|
|
gint64 end_time = g_get_monotonic_time () + timeout;
|
|
|
|
|
2018-07-29 16:12:51 +02:00
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
return g_async_queue_pop_intern_unlocked (queue, TRUE, end_time);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_timed_pop:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
* @end_time: a #GTimeVal, determining the final time
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* Pops data from the @queue. If the queue is empty, blocks until
|
|
|
|
* @end_time or until data becomes available.
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* If no data is received before @end_time, %NULL is returned.
|
|
|
|
*
|
2019-07-24 15:10:26 +02:00
|
|
|
* To easily calculate @end_time, a combination of g_get_real_time()
|
2001-06-07 15:46:14 +02:00
|
|
|
* and g_time_val_add() can be used.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue or %NULL, when no data is
|
2011-10-02 01:24:24 +02:00
|
|
|
* received before @end_time.
|
2012-02-08 14:26:36 +01:00
|
|
|
*
|
|
|
|
* Deprecated: use g_async_queue_timeout_pop().
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2019-06-28 20:00:13 +02:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
2000-04-28 14:24:53 +02:00
|
|
|
gpointer
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_timed_pop (GAsyncQueue *queue,
|
|
|
|
GTimeVal *end_time)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
2012-02-08 14:26:36 +01:00
|
|
|
gint64 m_end_time;
|
2000-04-28 14:24:53 +02:00
|
|
|
gpointer retval;
|
|
|
|
|
|
|
|
g_return_val_if_fail (queue, NULL);
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
if (end_time != NULL)
|
|
|
|
{
|
|
|
|
m_end_time = g_get_monotonic_time () +
|
2014-02-23 07:11:50 +01:00
|
|
|
((gint64) end_time->tv_sec * G_USEC_PER_SEC + end_time->tv_usec - g_get_real_time ());
|
2012-02-08 14:26:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_end_time = -1;
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2012-02-08 14:26:36 +01:00
|
|
|
retval = g_async_queue_pop_intern_unlocked (queue, TRUE, m_end_time);
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
2011-10-02 01:24:24 +02:00
|
|
|
return retval;
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
2019-06-28 20:00:13 +02:00
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
2000-04-28 14:24:53 +02:00
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_timed_pop_unlocked:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
* @end_time: a #GTimeVal, determining the final time
|
|
|
|
*
|
|
|
|
* Pops data from the @queue. If the queue is empty, blocks until
|
|
|
|
* @end_time or until data becomes available.
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* If no data is received before @end_time, %NULL is returned.
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2019-07-24 15:10:26 +02:00
|
|
|
* To easily calculate @end_time, a combination of g_get_real_time()
|
2001-06-07 15:46:14 +02:00
|
|
|
* and g_time_val_add() can be used.
|
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* This function must be called while holding the @queue's lock.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: data from the queue or %NULL, when no data is
|
2011-10-02 01:24:24 +02:00
|
|
|
* received before @end_time.
|
2012-02-08 14:26:36 +01:00
|
|
|
*
|
|
|
|
* Deprecated: use g_async_queue_timeout_pop_unlocked().
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2019-06-28 20:00:13 +02:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
2000-04-28 14:24:53 +02:00
|
|
|
gpointer
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
|
|
|
|
GTimeVal *end_time)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
2012-02-08 14:26:36 +01:00
|
|
|
gint64 m_end_time;
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
g_return_val_if_fail (queue, NULL);
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
if (end_time != NULL)
|
|
|
|
{
|
|
|
|
m_end_time = g_get_monotonic_time () +
|
2014-02-23 07:11:50 +01:00
|
|
|
((gint64) end_time->tv_sec * G_USEC_PER_SEC + end_time->tv_usec - g_get_real_time ());
|
2012-02-08 14:26:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_end_time = -1;
|
|
|
|
|
|
|
|
return g_async_queue_pop_intern_unlocked (queue, TRUE, m_end_time);
|
2000-04-28 14:24:53 +02:00
|
|
|
}
|
2019-06-28 20:00:13 +02:00
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
2000-04-28 14:24:53 +02:00
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_length:
|
|
|
|
* @queue: a #GAsyncQueue.
|
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* Returns the length of the queue.
|
|
|
|
*
|
|
|
|
* Actually this function returns the number of data items in
|
|
|
|
* the queue minus the number of waiting threads, so a negative
|
|
|
|
* value means waiting threads, and a positive value means available
|
|
|
|
* entries in the @queue. A return value of 0 could mean n entries
|
|
|
|
* in the queue and n threads waiting. This can happen due to locking
|
|
|
|
* of the queue or due to scheduling.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: the length of the @queue
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
gint
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_length (GAsyncQueue *queue)
|
2000-04-28 14:24:53 +02:00
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gint retval;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (queue, 0);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2010-08-13 05:57:26 +02:00
|
|
|
retval = queue->queue.length - queue->waiting_threads;
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2000-11-02 15:54:52 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_length_unlocked:
|
2011-10-02 01:24:24 +02:00
|
|
|
* @queue: a #GAsyncQueue
|
|
|
|
*
|
|
|
|
* Returns the length of the queue.
|
|
|
|
*
|
|
|
|
* Actually this function returns the number of data items in
|
|
|
|
* the queue minus the number of waiting threads, so a negative
|
|
|
|
* value means waiting threads, and a positive value means available
|
|
|
|
* entries in the @queue. A return value of 0 could mean n entries
|
|
|
|
* in the queue and n threads waiting. This can happen due to locking
|
|
|
|
* of the queue or due to scheduling.
|
|
|
|
*
|
|
|
|
* This function must be called while holding the @queue's lock.
|
2000-11-02 15:54:52 +01:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: the length of the @queue.
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2000-11-02 15:54:52 +01:00
|
|
|
gint
|
2011-10-02 01:24:24 +02:00
|
|
|
g_async_queue_length_unlocked (GAsyncQueue *queue)
|
2000-11-02 15:54:52 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue, 0);
|
|
|
|
|
2010-08-13 05:57:26 +02:00
|
|
|
return queue->queue.length - queue->waiting_threads;
|
2000-11-02 15:54:52 +01:00
|
|
|
}
|
|
|
|
|
2005-12-05 16:08:45 +01:00
|
|
|
/**
|
|
|
|
* g_async_queue_sort:
|
|
|
|
* @queue: a #GAsyncQueue
|
2011-10-02 01:24:24 +02:00
|
|
|
* @func: the #GCompareDataFunc is used to sort @queue
|
2005-12-05 16:08:45 +01:00
|
|
|
* @user_data: user data passed to @func
|
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* Sorts @queue using @func.
|
|
|
|
*
|
|
|
|
* The sort function @func is passed two elements of the @queue.
|
|
|
|
* It should return 0 if they are equal, a negative value if the
|
|
|
|
* first element should be higher in the @queue or a positive value
|
|
|
|
* if the first element should be lower in the @queue than the second
|
|
|
|
* element.
|
2005-12-05 16:08:45 +01:00
|
|
|
*
|
|
|
|
* This function will lock @queue before it sorts the queue and unlock
|
|
|
|
* it when it is finished.
|
|
|
|
*
|
|
|
|
* If you were sorting a list of priority numbers to make sure the
|
|
|
|
* lowest priority would be at the top of the queue, you could use:
|
2014-02-01 21:11:49 +01:00
|
|
|
* |[<!-- language="C" -->
|
2005-12-07 13:09:44 +01:00
|
|
|
* gint32 id1;
|
|
|
|
* gint32 id2;
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
2005-12-05 16:08:45 +01:00
|
|
|
* id1 = GPOINTER_TO_INT (element1);
|
|
|
|
* id2 = GPOINTER_TO_INT (element2);
|
2011-10-02 01:24:24 +02:00
|
|
|
*
|
2005-12-07 13:09:44 +01:00
|
|
|
* return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
|
2007-11-25 07:05:06 +01:00
|
|
|
* ]|
|
2005-12-05 16:08:45 +01:00
|
|
|
*
|
|
|
|
* Since: 2.10
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2005-12-05 16:08:45 +01:00
|
|
|
void
|
|
|
|
g_async_queue_sort (GAsyncQueue *queue,
|
2011-10-02 01:24:24 +02:00
|
|
|
GCompareDataFunc func,
|
|
|
|
gpointer user_data)
|
2005-12-05 16:08:45 +01:00
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
g_return_if_fail (func != NULL);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_lock (&queue->mutex);
|
2005-12-05 16:08:45 +01:00
|
|
|
g_async_queue_sort_unlocked (queue, func, user_data);
|
2011-09-17 23:56:33 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
2005-12-05 16:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_async_queue_sort_unlocked:
|
|
|
|
* @queue: a #GAsyncQueue
|
2011-10-02 01:24:24 +02:00
|
|
|
* @func: the #GCompareDataFunc is used to sort @queue
|
2005-12-05 16:08:45 +01:00
|
|
|
* @user_data: user data passed to @func
|
|
|
|
*
|
2011-10-02 01:24:24 +02:00
|
|
|
* Sorts @queue using @func.
|
|
|
|
*
|
|
|
|
* The sort function @func is passed two elements of the @queue.
|
|
|
|
* It should return 0 if they are equal, a negative value if the
|
|
|
|
* first element should be higher in the @queue or a positive value
|
|
|
|
* if the first element should be lower in the @queue than the second
|
|
|
|
* element.
|
|
|
|
*
|
|
|
|
* This function must be called while holding the @queue's lock.
|
2005-12-05 16:08:45 +01:00
|
|
|
*
|
|
|
|
* Since: 2.10
|
2011-10-02 01:24:24 +02:00
|
|
|
*/
|
2005-12-05 16:08:45 +01:00
|
|
|
void
|
|
|
|
g_async_queue_sort_unlocked (GAsyncQueue *queue,
|
2011-10-02 01:24:24 +02:00
|
|
|
GCompareDataFunc func,
|
|
|
|
gpointer user_data)
|
2005-12-05 16:08:45 +01:00
|
|
|
{
|
2005-12-07 13:09:44 +01:00
|
|
|
SortData sd;
|
2005-12-05 16:08:45 +01:00
|
|
|
|
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
g_return_if_fail (func != NULL);
|
|
|
|
|
2005-12-07 13:09:44 +01:00
|
|
|
sd.func = func;
|
|
|
|
sd.user_data = user_data;
|
2005-12-05 16:08:45 +01:00
|
|
|
|
2010-08-13 05:57:26 +02:00
|
|
|
g_queue_sort (&queue->queue,
|
2011-10-02 01:24:24 +02:00
|
|
|
(GCompareDataFunc)g_async_queue_invert_compare,
|
|
|
|
&sd);
|
2005-12-05 16:08:45 +01:00
|
|
|
}
|
|
|
|
|
2015-06-18 16:26:14 +02:00
|
|
|
/**
|
|
|
|
* g_async_queue_remove:
|
|
|
|
* @queue: a #GAsyncQueue
|
2015-09-21 12:27:07 +02:00
|
|
|
* @item: the data to remove from the @queue
|
2015-06-18 16:26:14 +02:00
|
|
|
*
|
2015-07-27 12:24:44 +02:00
|
|
|
* Remove an item from the queue.
|
2015-06-18 16:26:14 +02:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if the item was removed
|
|
|
|
*
|
|
|
|
* Since: 2.46
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
g_async_queue_remove (GAsyncQueue *queue,
|
2015-09-21 12:27:07 +02:00
|
|
|
gpointer item)
|
2015-06-18 16:26:14 +02:00
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (queue != NULL, FALSE);
|
2015-09-21 12:27:07 +02:00
|
|
|
g_return_val_if_fail (item != NULL, FALSE);
|
2015-06-18 16:26:14 +02:00
|
|
|
|
|
|
|
g_mutex_lock (&queue->mutex);
|
2015-09-21 12:27:07 +02:00
|
|
|
ret = g_async_queue_remove_unlocked (queue, item);
|
2015-06-18 16:26:14 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_async_queue_remove_unlocked:
|
|
|
|
* @queue: a #GAsyncQueue
|
2015-09-21 12:27:07 +02:00
|
|
|
* @item: the data to remove from the @queue
|
2015-06-18 16:26:14 +02:00
|
|
|
*
|
2015-07-27 12:24:44 +02:00
|
|
|
* Remove an item from the queue.
|
2015-06-18 16:26:14 +02:00
|
|
|
*
|
|
|
|
* 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,
|
2015-09-21 12:27:07 +02:00
|
|
|
gpointer item)
|
2015-06-18 16:26:14 +02:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue != NULL, FALSE);
|
2015-09-21 12:27:07 +02:00
|
|
|
g_return_val_if_fail (item != NULL, FALSE);
|
2015-06-18 16:26:14 +02:00
|
|
|
|
2015-09-21 12:27:07 +02:00
|
|
|
return g_queue_remove (&queue->queue, item);
|
2015-06-18 16:26:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_async_queue_push_front:
|
|
|
|
* @queue: a #GAsyncQueue
|
2015-09-21 12:27:07 +02:00
|
|
|
* @item: data to push into the @queue
|
2015-06-18 16:26:14 +02:00
|
|
|
*
|
2015-09-21 12:27:07 +02:00
|
|
|
* Pushes the @item into the @queue. @item must not be %NULL.
|
2015-06-18 16:26:14 +02:00
|
|
|
* 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,
|
2015-09-21 12:27:07 +02:00
|
|
|
gpointer item)
|
2015-06-18 16:26:14 +02:00
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
2015-09-21 12:27:07 +02:00
|
|
|
g_return_if_fail (item != NULL);
|
2015-06-18 16:26:14 +02:00
|
|
|
|
|
|
|
g_mutex_lock (&queue->mutex);
|
2015-09-21 12:27:07 +02:00
|
|
|
g_async_queue_push_front_unlocked (queue, item);
|
2015-06-18 16:26:14 +02:00
|
|
|
g_mutex_unlock (&queue->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_async_queue_push_front_unlocked:
|
|
|
|
* @queue: a #GAsyncQueue
|
2015-09-21 12:27:07 +02:00
|
|
|
* @item: data to push into the @queue
|
2015-06-18 16:26:14 +02:00
|
|
|
*
|
2015-09-21 12:27:07 +02:00
|
|
|
* Pushes the @item into the @queue. @item must not be %NULL.
|
2015-06-18 16:26:14 +02:00
|
|
|
* 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,
|
2015-09-21 12:27:07 +02:00
|
|
|
gpointer item)
|
2015-06-18 16:26:14 +02:00
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
2015-09-21 12:27:07 +02:00
|
|
|
g_return_if_fail (item != NULL);
|
2015-06-18 16:26:14 +02:00
|
|
|
|
2015-09-21 12:27:07 +02:00
|
|
|
g_queue_push_tail (&queue->queue, item);
|
2015-06-18 16:26:14 +02:00
|
|
|
if (queue->waiting_threads > 0)
|
|
|
|
g_cond_signal (&queue->cond);
|
|
|
|
}
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
/*
|
|
|
|
* Private API
|
|
|
|
*/
|
|
|
|
|
2011-10-02 01:24:24 +02:00
|
|
|
GMutex *
|
|
|
|
_g_async_queue_get_mutex (GAsyncQueue *queue)
|
2006-04-07 11:23:42 +02:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue, NULL);
|
|
|
|
|
2011-09-17 23:56:33 +02:00
|
|
|
return &queue->mutex;
|
2006-04-07 11:23:42 +02:00
|
|
|
}
|