Index: gobject/gtype.c
===================================================================
--- gobject/gtype.c	(revision 7583)
+++ gobject/gtype.c	(working copy)
@@ -2622,7 +2622,11 @@
   GType ptype;
 
   /* optimize for common code path */
-  G_WRITE_LOCK (&type_rw_lock);
+  /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
+
+   g_static_rec_mutex_lock (&class_init_rec_mutex);
+   G_WRITE_LOCK (&type_rw_lock);
+
   node = lookup_type_node_I (type);
   if (node && node->is_classed && node->data &&
       node->data->class.class &&
@@ -2630,21 +2634,21 @@
     {
       type_data_ref_Wm (node);
       G_WRITE_UNLOCK (&type_rw_lock);
+      g_static_rec_mutex_unlock (&class_init_rec_mutex);
       return node->data->class.class;
     }
   if (!node || !node->is_classed ||
       (node->data && node->data->common.ref_count < 1))
     {
       G_WRITE_UNLOCK (&type_rw_lock);
+      g_static_rec_mutex_unlock (&class_init_rec_mutex);
       g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
 		 type_descriptive_name_I (type));
       return NULL;
     }
   type_data_ref_Wm (node);
   ptype = NODE_PARENT_TYPE (node);
-  G_WRITE_UNLOCK (&type_rw_lock);
 
-  g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
   /* here, we either have node->data->class.class == NULL, or a recursive
    * call to g_type_class_ref() with a partly initialized class, or
    * node->data->class.init_state == INITIALIZED, because any
@@ -2654,12 +2658,12 @@
     {
       /* acquire reference on parent class */
       GTypeClass *pclass = ptype ? g_type_class_ref (ptype) : NULL;
-      G_WRITE_LOCK (&type_rw_lock);
       if (node->data->class.class) /* class was initialized during parent class initialization? */
         INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
       type_class_init_Wm (node, pclass);
-      G_WRITE_UNLOCK (&type_rw_lock);
     }
+
+  G_WRITE_UNLOCK (&type_rw_lock);
   g_static_rec_mutex_unlock (&class_init_rec_mutex);
 
   return node->data->class.class;
