[glib/wip/gcleanup] GThread posix: add impl structs to cleanup list



commit 18283a63268cc9687fe932556d150bfa505c324d
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Mar 24 22:19:14 2013 -0400

    GThread posix: add impl structs to cleanup list
    
    There are two ways of using the structs like GMutex, GCond, etc.
    
    The first is to explicitly _init() and _clear() them.  This is when you
    use them as part of another structure.  This case is not interesting for
    gcleanup.
    
    The other is to have them in static storage (zero-filled) and just use
    them for the first time.  This is the difficult case, because we don't
    ever free the impl in that case.
    
    In the first case, the impl is created and set in the _init() function.
    We can therfore tell that we are in the second case if we get to the
    get_impl() function and the impl is not there yet.  In that case, add it
    to the cleanup list.
    
    There are probably people that allocate a GMutex as part of a
    zero-filled structure and use it, then call g_mutex_clear() on it.  This
    is technically a violation of the API and these users will crash with
    G_DEBUG=cleanup, but that's a good thing because it will cause them to
    fix their code.

 glib/gthread-posix.c |   23 ++++++++++++++++++-----
 1 files changed, 18 insertions(+), 5 deletions(-)
---
diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index f7e60b7..aefef8d 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -47,6 +47,7 @@
 #include "gslice.h"
 #include "gmessages.h"
 #include "gstrfuncs.h"
+#include "gcleanup.h"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -123,7 +124,9 @@ g_mutex_get_impl (GMutex *mutex)
   if G_UNLIKELY (impl == NULL)
     {
       impl = g_mutex_impl_new ();
-      if (!g_atomic_pointer_compare_and_exchange (&mutex->p, NULL, impl))
+      if (g_atomic_pointer_compare_and_exchange (&mutex->p, NULL, impl))
+        G_CLEANUP_ADD (impl, g_mutex_impl_free);
+      else
         g_mutex_impl_free (impl);
       impl = mutex->p;
     }
@@ -292,7 +295,9 @@ g_rec_mutex_get_impl (GRecMutex *rec_mutex)
   if G_UNLIKELY (impl == NULL)
     {
       impl = g_rec_mutex_impl_new ();
-      if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl))
+      if (g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl))
+        G_CLEANUP_ADD (impl, g_rec_mutex_impl_free);
+      else
         g_rec_mutex_impl_free (impl);
       impl = rec_mutex->p;
     }
@@ -452,7 +457,9 @@ g_rw_lock_get_impl (GRWLock *lock)
   if G_UNLIKELY (impl == NULL)
     {
       impl = g_rw_lock_impl_new ();
-      if (!g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl))
+      if (g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl))
+        G_CLEANUP_ADD (impl, g_rw_lock_impl_free);
+      else
         g_rw_lock_impl_free (impl);
       impl = lock->p;
     }
@@ -669,7 +676,9 @@ g_cond_get_impl (GCond *cond)
   if G_UNLIKELY (impl == NULL)
     {
       impl = g_cond_impl_new ();
-      if (!g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl))
+      if (g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl))
+        G_CLEANUP_ADD (impl, g_cond_impl_free);
+      else
         g_cond_impl_free (impl);
       impl = cond->p;
     }
@@ -978,7 +987,11 @@ g_private_get_impl (GPrivate *key)
   if G_UNLIKELY (impl == NULL)
     {
       impl = g_private_impl_new (key->notify);
-      if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
+      if (g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
+        {
+          G_CLEANUP_ADD (impl, g_private_impl_free);
+        }
+      else
         {
           g_private_impl_free (impl);
           impl = key->p;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]