create a common function for the many places where all nodes in the table
[platform/upstream/glib.git] / glib / gasyncqueue.c
index d9e6489..dbaebde 100644 (file)
  * MT safe
  */
 
+#include "config.h"
+
 #include "glib.h"
+#include "galias.h"
+
 
 struct _GAsyncQueue
 {
@@ -32,9 +36,14 @@ struct _GAsyncQueue
   GCond *cond;
   GQueue *queue;
   guint waiting_threads;
-  guint ref_count;
+  gint32 ref_count;
 };
 
+typedef struct {
+  GCompareDataFunc func;
+  gpointer         user_data;
+} SortData;
+
 /**
  * g_async_queue_new:
  * 
@@ -43,11 +52,11 @@ struct _GAsyncQueue
  * Return value: the new #GAsyncQueue.
  **/
 GAsyncQueue*
-g_async_queue_new ()
+g_async_queue_new (void)
 {
   GAsyncQueue* retval = g_new (GAsyncQueue, 1);
   retval->mutex = g_mutex_new ();
-  retval->cond = g_cond_new ();
+  retval->cond = NULL;
   retval->queue = g_queue_new ();
   retval->waiting_threads = 0;
   retval->ref_count = 1;
@@ -58,33 +67,39 @@ g_async_queue_new ()
  * g_async_queue_ref:
  * @queue: a #GAsyncQueue.
  *
- * Increases the reference count of the asynchronous @queue by 1.
+ * Increases the reference count of the asynchronous @queue by 1. You
+ * do not need to hold the lock to call this function.
+ *
+ * Returns: the @queue that was passed in (since 2.6)
  **/
-void 
+GAsyncQueue *
 g_async_queue_ref (GAsyncQueue *queue)
 {
-  g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
+  g_return_val_if_fail (queue, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
   
-  g_mutex_lock (queue->mutex);
-  queue->ref_count++;
-  g_mutex_unlock (queue->mutex);
+  g_atomic_int_inc (&queue->ref_count);
+
+  return queue;
 }
 
 /**
  * g_async_queue_ref_unlocked:
  * @queue: a #GAsyncQueue.
  * 
- * Increases the reference count of the asynchronous @queue by 1. This
- * function must be called while holding the @queue's lock.
+ * Increases the reference count of the asynchronous @queue by 1.
+ *
+ * @Deprecated: Since 2.8, reference counting is done atomically
+ * so g_async_queue_ref() can be used regardless of the @queue's
+ * lock.
  **/
 void 
 g_async_queue_ref_unlocked (GAsyncQueue *queue)
 {
   g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
+  g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
   
-  queue->ref_count++;
+  g_atomic_int_inc (&queue->ref_count);
 }
 
 /**
@@ -94,32 +109,20 @@ g_async_queue_ref_unlocked (GAsyncQueue *queue)
  * 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. So you are not
- * allowed to use the @queue afterwards, as it might have disappeared.
- * The obvious asymmetry (it is not named
- * g_async_queue_unref_unlocked(<!-- -->)) is because the queue can't be
- * unlocked after unreffing it, as it might already have disappeared.
+ * destroyed and the memory allocated will be freed.
+ *
+ * @Deprecated: Since 2.8, reference counting is done atomically
+ * so g_async_queue_unref() can be used regardless of the @queue's
+ * lock.
  **/
 void 
 g_async_queue_unref_and_unlock (GAsyncQueue *queue)
 {
-  gboolean stop;
-
   g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
+  g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
 
-  queue->ref_count--;
-  stop = (queue->ref_count == 0);
   g_mutex_unlock (queue->mutex);
-  
-  if (stop)
-    {
-      g_return_if_fail (queue->waiting_threads == 0);
-      g_mutex_free (queue->mutex);
-      g_cond_free (queue->cond);
-      g_queue_free (queue->queue);
-      g_free (queue);
-    }
+  g_async_queue_unref (queue);
 }
 
 /**
@@ -129,16 +132,24 @@ g_async_queue_unref_and_unlock (GAsyncQueue *queue)
  * 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.
+ * @queue afterwards, as it might have disappeared. You do not need to
+ * hold the lock to call this function.
  **/
 void 
 g_async_queue_unref (GAsyncQueue *queue)
 {
   g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
-
-  g_mutex_lock (queue->mutex);
-  g_async_queue_unref_and_unlock (queue);
+  g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
+  
+  if (g_atomic_int_dec_and_test (&queue->ref_count))
+    {
+      g_return_if_fail (queue->waiting_threads == 0);
+      g_mutex_free (queue->mutex);
+      if (queue->cond)
+       g_cond_free (queue->cond);
+      g_queue_free (queue->queue);
+      g_free (queue);
+    }
 }
 
 /**
@@ -146,14 +157,14 @@ g_async_queue_unref (GAsyncQueue *queue)
  * @queue: a #GAsyncQueue.
  * 
  * Acquires the @queue's lock. After that you can only call the
- * <function>g_async_queue_*_unlocked(<!-- -->)</function> function variants on that
+ * <function>g_async_queue_*_unlocked()</function> function variants on that
  * @queue. Otherwise it will deadlock.
  **/
 void
 g_async_queue_lock (GAsyncQueue *queue)
 {
   g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
+  g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
 
   g_mutex_lock (queue->mutex);
 }
@@ -168,7 +179,7 @@ void
 g_async_queue_unlock (GAsyncQueue *queue)
 {
   g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
+  g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
 
   g_mutex_unlock (queue->mutex);
 }
@@ -184,7 +195,7 @@ void
 g_async_queue_push (GAsyncQueue* queue, gpointer data)
 {
   g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
+  g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
   g_return_if_fail (data);
 
   g_mutex_lock (queue->mutex);
@@ -204,38 +215,133 @@ void
 g_async_queue_push_unlocked (GAsyncQueue* queue, gpointer data)
 {
   g_return_if_fail (queue);
-  g_return_if_fail (queue->ref_count > 0);
+  g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
   g_return_if_fail (data);
 
   g_queue_push_head (queue->queue, data);
-  g_cond_signal (queue->cond);
+  if (queue->waiting_threads > 0)
+    g_cond_signal (queue->cond);
+}
+
+/**
+ * g_async_queue_push_sorted:
+ * @queue: a #GAsyncQueue
+ * @data: the @data to push into the @queue
+ * @func: the #GCompareDataFunc is used to sort @queue. This function
+ *     is passed two elements of the @queue. The function 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.
+ * @user_data: user data passed to @func.
+ * 
+ * Inserts @data into @queue using @func to determine the new
+ * position. 
+ * 
+ * This function requires that the @queue is sorted before pushing on
+ * new elements.
+ * 
+ * This function will lock @queue before it sorts the queue and unlock
+ * it when it is finished.
+ * 
+ * For an example of @func see g_async_queue_sort(). 
+ *
+ * Since: 2.10
+ **/
+void
+g_async_queue_push_sorted (GAsyncQueue      *queue,
+                          gpointer          data,
+                          GCompareDataFunc  func,
+                          gpointer          user_data)
+{
+  g_return_if_fail (queue != NULL);
+
+  g_mutex_lock (queue->mutex);
+  g_async_queue_push_sorted_unlocked (queue, data, func, user_data);
+  g_mutex_unlock (queue->mutex);
+}
+
+static gint 
+g_async_queue_invert_compare (gpointer  v1, 
+                             gpointer  v2, 
+                             SortData *sd)
+{
+  return -sd->func (v1, v2, sd->user_data);
+}
+
+/**
+ * g_async_queue_push_sorted_unlocked:
+ * @queue: a #GAsyncQueue
+ * @data: the @data to push into the @queue
+ * @func: the #GCompareDataFunc is used to sort @queue. This function
+ *     is passed two elements of the @queue. The function 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.
+ * @user_data: user data passed to @func.
+ * 
+ * Inserts @data into @queue using @func to determine the new
+ * position.
+ * 
+ * This function requires that the @queue is sorted before pushing on
+ * new elements.
+ * 
+ * This function is called while holding the @queue's lock.
+ * 
+ * For an example of @func see g_async_queue_sort(). 
+ *
+ * Since: 2.10
+ **/
+void
+g_async_queue_push_sorted_unlocked (GAsyncQueue      *queue,
+                                   gpointer          data,
+                                   GCompareDataFunc  func,
+                                   gpointer          user_data)
+{
+  SortData sd;
+  
+  g_return_if_fail (queue != NULL);
+
+  sd.func = func;
+  sd.user_data = user_data;
+
+  g_queue_insert_sorted (queue->queue, 
+                        data, 
+                        (GCompareDataFunc)g_async_queue_invert_compare, 
+                        &sd);
+  if (queue->waiting_threads > 0)
+    g_cond_signal (queue->cond);
 }
 
 static gpointer
-g_async_queue_pop_intern_unlocked (GAsyncQueue* queue, gboolean try, 
-                                  GTimeVal *end_time)
+g_async_queue_pop_intern_unlocked (GAsyncQueue *queue, 
+                                  gboolean     try, 
+                                  GTimeVal    *end_time)
 {
   gpointer retval;
 
-  if (!g_queue_peek_tail (queue->queue))
+  if (!g_queue_peek_tail_link (queue->queue))
     {
       if (try)
        return NULL;
+      
+      if (!queue->cond)
+       queue->cond = g_cond_new ();
+
       if (!end_time)
         {
           queue->waiting_threads++;
-         while (!g_queue_peek_tail (queue->queue))
-            g_cond_wait(queue->cond, queue->mutex);
+         while (!g_queue_peek_tail_link (queue->queue))
+            g_cond_wait (queue->cond, queue->mutex);
           queue->waiting_threads--;
         }
       else
         {
           queue->waiting_threads++;
-          while (!g_queue_peek_tail (queue->queue))
+          while (!g_queue_peek_tail_link (queue->queue))
             if (!g_cond_timed_wait (queue->cond, queue->mutex, end_time))
               break;
           queue->waiting_threads--;
-          if (!g_queue_peek_tail (queue->queue))
+          if (!g_queue_peek_tail_link (queue->queue))
            return NULL;
         }
     }
@@ -262,7 +368,7 @@ g_async_queue_pop (GAsyncQueue* queue)
   gpointer retval;
 
   g_return_val_if_fail (queue, NULL);
-  g_return_val_if_fail (queue->ref_count > 0, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
 
   g_mutex_lock (queue->mutex);
   retval = g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
@@ -285,7 +391,7 @@ gpointer
 g_async_queue_pop_unlocked (GAsyncQueue* queue)
 {
   g_return_val_if_fail (queue, NULL);
-  g_return_val_if_fail (queue->ref_count > 0, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
 
   return g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
 }
@@ -306,7 +412,7 @@ g_async_queue_try_pop (GAsyncQueue* queue)
   gpointer retval;
 
   g_return_val_if_fail (queue, NULL);
-  g_return_val_if_fail (queue->ref_count > 0, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
 
   g_mutex_lock (queue->mutex);
   retval = g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
@@ -330,7 +436,7 @@ gpointer
 g_async_queue_try_pop_unlocked (GAsyncQueue* queue)
 {
   g_return_val_if_fail (queue, NULL);
-  g_return_val_if_fail (queue->ref_count > 0, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
 
   return g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
 }
@@ -355,7 +461,7 @@ g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time)
   gpointer retval;
 
   g_return_val_if_fail (queue, NULL);
-  g_return_val_if_fail (queue->ref_count > 0, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
 
   g_mutex_lock (queue->mutex);
   retval = g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
@@ -383,7 +489,7 @@ gpointer
 g_async_queue_timed_pop_unlocked (GAsyncQueue* queue, GTimeVal *end_time)
 {
   g_return_val_if_fail (queue, NULL);
-  g_return_val_if_fail (queue->ref_count > 0, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
 
   return g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
 }
@@ -408,7 +514,7 @@ g_async_queue_length (GAsyncQueue* queue)
   gint retval;
 
   g_return_val_if_fail (queue, 0);
-  g_return_val_if_fail (queue->ref_count > 0, 0);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, 0);
 
   g_mutex_lock (queue->mutex);
   retval = queue->queue->length - queue->waiting_threads;
@@ -436,8 +542,101 @@ gint
 g_async_queue_length_unlocked (GAsyncQueue* queue)
 {
   g_return_val_if_fail (queue, 0);
-  g_return_val_if_fail (queue->ref_count > 0, 0);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, 0);
 
   return queue->queue->length - queue->waiting_threads;
 }
 
+/**
+ * g_async_queue_sort:
+ * @queue: a #GAsyncQueue
+ * @func: the #GCompareDataFunc is used to sort @queue. This
+ *     function is passed two elements of the @queue. The function
+ *     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. 
+ * @user_data: user data passed to @func
+ *
+ * Sorts @queue using @func. 
+ *
+ * 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:
+ * |[
+ *  gint32 id1;
+ *  gint32 id2;
+ *   
+ *  id1 = GPOINTER_TO_INT (element1);
+ *  id2 = GPOINTER_TO_INT (element2);
+ *   
+ *  return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
+ * ]|
+ *
+ * Since: 2.10
+ **/
+void
+g_async_queue_sort (GAsyncQueue      *queue,
+                   GCompareDataFunc  func,
+                   gpointer          user_data)
+{
+  g_return_if_fail (queue != NULL);
+  g_return_if_fail (func != NULL);
+
+  g_mutex_lock (queue->mutex);
+  g_async_queue_sort_unlocked (queue, func, user_data);
+  g_mutex_unlock (queue->mutex);
+}
+
+/**
+ * g_async_queue_sort_unlocked:
+ * @queue: a #GAsyncQueue
+ * @func: the #GCompareDataFunc is used to sort @queue. This
+ *     function is passed two elements of the @queue. The function
+ *     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. 
+ * @user_data: user data passed to @func
+ *
+ * Sorts @queue using @func. 
+ *
+ * This function is called while holding the @queue's lock.
+ * 
+ * Since: 2.10
+ **/
+void
+g_async_queue_sort_unlocked (GAsyncQueue      *queue,
+                            GCompareDataFunc  func,
+                            gpointer          user_data)
+{
+  SortData sd;
+
+  g_return_if_fail (queue != NULL);
+  g_return_if_fail (func != NULL);
+
+  sd.func = func;
+  sd.user_data = user_data;
+
+  g_queue_sort (queue->queue, 
+               (GCompareDataFunc)g_async_queue_invert_compare, 
+               &sd);
+}
+
+/*
+ * Private API
+ */
+
+GMutex*
+_g_async_queue_get_mutex (GAsyncQueue* queue)
+{
+  g_return_val_if_fail (queue, NULL);
+  g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
+
+  return queue->mutex;
+}
+
+#define __G_ASYNCQUEUE_C__
+#include "galiasdef.c"