Use g_atomic_int_(inc|dec_and_test) for reference counting.
authorSebastian Wilhelmi <seppi@seppi.de>
Fri, 27 Feb 2004 16:06:31 +0000 (16:06 +0000)
committerSebastian Wilhelmi <wilhelmi@src.gnome.org>
Fri, 27 Feb 2004 16:06:31 +0000 (16:06 +0000)
2004-02-27  Sebastian Wilhelmi  <seppi@seppi.de>

* glib/gasyncqueue.c, glib/gasyncqueue.h: Use
g_atomic_int_(inc|dec_and_test) for reference
counting. g_async_queue_unref_and_unlock and
g_async_queue_ref_locked is deprecated, but still there to
preserve ABI.

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/gasyncqueue.c
glib/gasyncqueue.h

index 91f163f56153a87aa0f2984e4859c0d7eadd2268..1b2898f82d6c261c7ab065dd5680ad6b24bafe55 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2004-02-27  Sebastian Wilhelmi  <seppi@seppi.de>
+
+       * glib/gasyncqueue.c, glib/gasyncqueue.h: Use
+       g_atomic_int_(inc|dec_and_test) for reference
+       counting. g_async_queue_unref_and_unlock and
+       g_async_queue_ref_locked is deprecated, but still there to
+       preserve ABI.
+
 Fri Feb 27 02:00:34 2004  Matthias Clasen  <maclas@gmx.de>
 
        * acglib.m4: quote AC_DEFUN macro names so automake
index 91f163f56153a87aa0f2984e4859c0d7eadd2268..1b2898f82d6c261c7ab065dd5680ad6b24bafe55 100644 (file)
@@ -1,3 +1,11 @@
+2004-02-27  Sebastian Wilhelmi  <seppi@seppi.de>
+
+       * glib/gasyncqueue.c, glib/gasyncqueue.h: Use
+       g_atomic_int_(inc|dec_and_test) for reference
+       counting. g_async_queue_unref_and_unlock and
+       g_async_queue_ref_locked is deprecated, but still there to
+       preserve ABI.
+
 Fri Feb 27 02:00:34 2004  Matthias Clasen  <maclas@gmx.de>
 
        * acglib.m4: quote AC_DEFUN macro names so automake
index 91f163f56153a87aa0f2984e4859c0d7eadd2268..1b2898f82d6c261c7ab065dd5680ad6b24bafe55 100644 (file)
@@ -1,3 +1,11 @@
+2004-02-27  Sebastian Wilhelmi  <seppi@seppi.de>
+
+       * glib/gasyncqueue.c, glib/gasyncqueue.h: Use
+       g_atomic_int_(inc|dec_and_test) for reference
+       counting. g_async_queue_unref_and_unlock and
+       g_async_queue_ref_locked is deprecated, but still there to
+       preserve ABI.
+
 Fri Feb 27 02:00:34 2004  Matthias Clasen  <maclas@gmx.de>
 
        * acglib.m4: quote AC_DEFUN macro names so automake
index 91f163f56153a87aa0f2984e4859c0d7eadd2268..1b2898f82d6c261c7ab065dd5680ad6b24bafe55 100644 (file)
@@ -1,3 +1,11 @@
+2004-02-27  Sebastian Wilhelmi  <seppi@seppi.de>
+
+       * glib/gasyncqueue.c, glib/gasyncqueue.h: Use
+       g_atomic_int_(inc|dec_and_test) for reference
+       counting. g_async_queue_unref_and_unlock and
+       g_async_queue_ref_locked is deprecated, but still there to
+       preserve ABI.
+
 Fri Feb 27 02:00:34 2004  Matthias Clasen  <maclas@gmx.de>
 
        * acglib.m4: quote AC_DEFUN macro names so automake
index 91f163f56153a87aa0f2984e4859c0d7eadd2268..1b2898f82d6c261c7ab065dd5680ad6b24bafe55 100644 (file)
@@ -1,3 +1,11 @@
+2004-02-27  Sebastian Wilhelmi  <seppi@seppi.de>
+
+       * glib/gasyncqueue.c, glib/gasyncqueue.h: Use
+       g_atomic_int_(inc|dec_and_test) for reference
+       counting. g_async_queue_unref_and_unlock and
+       g_async_queue_ref_locked is deprecated, but still there to
+       preserve ABI.
+
 Fri Feb 27 02:00:34 2004  Matthias Clasen  <maclas@gmx.de>
 
        * acglib.m4: quote AC_DEFUN macro names so automake
index 91f163f56153a87aa0f2984e4859c0d7eadd2268..1b2898f82d6c261c7ab065dd5680ad6b24bafe55 100644 (file)
@@ -1,3 +1,11 @@
+2004-02-27  Sebastian Wilhelmi  <seppi@seppi.de>
+
+       * glib/gasyncqueue.c, glib/gasyncqueue.h: Use
+       g_atomic_int_(inc|dec_and_test) for reference
+       counting. g_async_queue_unref_and_unlock and
+       g_async_queue_ref_locked is deprecated, but still there to
+       preserve ABI.
+
 Fri Feb 27 02:00:34 2004  Matthias Clasen  <maclas@gmx.de>
 
        * acglib.m4: quote AC_DEFUN macro names so automake
index 3ed3eb8939f3168f0ef9a78716c50c9f14d2abca..71dde1259023a0abed939cb6497d199ed87d988c 100644 (file)
@@ -35,7 +35,7 @@ struct _GAsyncQueue
   GCond *cond;
   GQueue *queue;
   guint waiting_threads;
-  guint ref_count;
+  gint32 ref_count;
 };
 
 /**
@@ -61,7 +61,8 @@ 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.
  **/
 void 
 g_async_queue_ref (GAsyncQueue *queue)
@@ -69,17 +70,14 @@ g_async_queue_ref (GAsyncQueue *queue)
   g_return_if_fail (queue);
   g_return_if_fail (queue->ref_count > 0);
   
-  g_mutex_lock (queue->mutex);
-  queue->ref_count++;
-  g_mutex_unlock (queue->mutex);
+  g_atomic_int_inc (&queue->ref_count);
 }
 
 /**
  * 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.
  **/
 void 
 g_async_queue_ref_unlocked (GAsyncQueue *queue)
@@ -87,7 +85,7 @@ g_async_queue_ref_unlocked (GAsyncQueue *queue)
   g_return_if_fail (queue);
   g_return_if_fail (queue->ref_count > 0);
   
-  queue->ref_count++;
+  g_atomic_int_inc (&queue->ref_count);
 }
 
 /**
@@ -97,33 +95,16 @@ 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.
  **/
 void 
 g_async_queue_unref_and_unlock (GAsyncQueue *queue)
 {
-  gboolean stop;
-
   g_return_if_fail (queue);
   g_return_if_fail (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);
-      if (queue->cond)
-       g_cond_free (queue->cond);
-      g_queue_free (queue->queue);
-      g_free (queue);
-    }
+  g_async_queue_unref (queue);
 }
 
 /**
@@ -133,16 +114,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);
+  
+  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);
+    }
 }
 
 /**
index bfc7b69e9e51f273a00bd7e8e8fc93e84d9339c4..80044e0bc382e0e935970366fd7860230586f01f 100644 (file)
@@ -46,17 +46,14 @@ GAsyncQueue*  g_async_queue_new                (void);
 void          g_async_queue_lock               (GAsyncQueue *queue);
 void          g_async_queue_unlock             (GAsyncQueue *queue);
 
-/* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
- * no sense, as after the unreffing the Queue might be gone and can't
- * be unlocked. So you have a function to call, if you don't hold the
- * lock (g_async_queue_unref) and one to call, when you already hold
- * the lock (g_async_queue_unref_and_unlock). After that however, you
- * don't hold the lock anymore and the Queue might in fact be
- * destroyed, if you unrefed to zero. */
+/* Ref and unref the GAsyncQueue. */
 void          g_async_queue_ref                (GAsyncQueue *queue);
-void          g_async_queue_ref_unlocked       (GAsyncQueue *queue);
 void          g_async_queue_unref              (GAsyncQueue *queue);
+#ifndef G_DISABLE_DEPRECATED
+/* You don't have to hold the lock for calling *_ref and *_unref anymore. */
+void          g_async_queue_ref_unlocked       (GAsyncQueue *queue);
 void          g_async_queue_unref_and_unlock   (GAsyncQueue *queue);
+#endif /* !G_DISABLE_DEPRECATED */
 
 /* Push data into the async queue. Must not be NULL. */
 void          g_async_queue_push               (GAsyncQueue *queue,