[glib] windows XP threads: fix hilariously obvious race



commit 11015f165229ac3cb5f008a8824f8135ec45c89a
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Dec 15 13:27:27 2011 -0500

    windows XP threads: fix hilariously obvious race
    
    I tried to do a double-checked lock without the double check.
    
    Rodrigo Rivas Costa caught the problem and suggested the (obviously
    correct) fix.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=666296

 glib/gthread-win32.c |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
---
diff --git a/glib/gthread-win32.c b/glib/gthread-win32.c
index dd777c2..29065b5 100644
--- a/glib/gthread-win32.c
+++ b/glib/gthread-win32.c
@@ -611,15 +611,20 @@ g_thread_xp_get_srwlock (GThreadSRWLock * volatile *lock)
     {
       EnterCriticalSection (&g_thread_xp_lock);
 
-      result = malloc (sizeof (GThreadSRWLock));
-
+      /* Check again */
+      result = *lock;
       if (result == NULL)
-        g_thread_abort (errno, "malloc");
+        {
+          result = malloc (sizeof (GThreadSRWLock));
 
-      InitializeCriticalSection (&result->writer_lock);
-      result->writer_locked = FALSE;
-      result->ever_shared = FALSE;
-      *lock = result;
+          if (result == NULL)
+            g_thread_abort (errno, "malloc");
+
+          InitializeCriticalSection (&result->writer_lock);
+          result->writer_locked = FALSE;
+          result->ever_shared = FALSE;
+          *lock = result;
+        }
 
       LeaveCriticalSection (&g_thread_xp_lock);
     }



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