Index: gslice.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gslice.c,v
retrieving revision 1.18
diff -p -u -r1.18 gslice.c
--- gslice.c	14 Feb 2006 16:31:37 -0000	1.18
+++ gslice.c	20 Feb 2006 02:59:28 -0000
@@ -194,6 +194,8 @@ static SliceConfig slice_config = {
   15 * 1000,    /* working_set_msecs */
   1,            /* color increment, alt: 0x7fffffff */
 };
+static GThread *main_thread_self = NULL;
+static ThreadMemory *main_thread_memory = NULL;
 
 /* --- auxillary funcitons --- */
 void
@@ -365,6 +367,7 @@ _g_slice_thread_init_nomessage (void)
   private_thread_memory = g_private_new (private_thread_memory_cleanup);
   allocator->magazine_mutex = g_mutex_new();
   allocator->slab_mutex = g_mutex_new();
+  main_thread_self = g_thread_self ();
 }
 
 static inline void
@@ -404,10 +407,41 @@ thread_memory_from_self (void)
   if (G_UNLIKELY (!tmem))
     {
       const guint n_magazines = MAX_SLAB_INDEX (allocator);
-      tmem = g_malloc0 (sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines);
-      tmem->magazine1 = (Magazine*) (tmem + 1);
-      tmem->magazine2 = &tmem->magazine1[n_magazines];
+      if (g_thread_supported ())
+	{
+	  g_assert (main_thread_self != NULL);
+	  if (g_thread_self () == main_thread_self && main_thread_memory != NULL)
+	    {
+	      /* GSlice has been used before g_thread_init(), and now
+	       * we are running threaded. Boo!, apps should call
+	       * g_thread_init() the first thing they do if they are
+	       * going to use threads. But cope with it, use the saved
+	       * tmem from when we weren't threaded.
+	       */
+	      tmem = main_thread_memory;
+	    }
+	}
+
+      if (G_UNLIKELY (!tmem))
+	{
+	  tmem = g_malloc0 (sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines);
+	  tmem->magazine1 = (Magazine*) (tmem + 1);
+	  tmem->magazine2 = &tmem->magazine1[n_magazines];
+	}
+
+      /* If not threaded yet, this will set private_thread_memory
+       * itself as tmem
+       */
       g_private_set (private_thread_memory, tmem);
+
+      if (!g_thread_supported ())
+	{
+	  /* We are obviously running in the main thread. Save tmem in
+	   * another place so we can use the same later when/if we do
+	   * go threaded.
+	   */
+	  main_thread_memory = tmem;
+	}
     }
   return tmem;
 }
