Only add object to list new objects when it has a custom constructor
authorBenjamin Otte <otte@gnome.org>
Thu, 8 Oct 2009 18:01:15 +0000 (20:01 +0200)
committerAlexander Larsson <alexl@redhat.com>
Mon, 30 Nov 2009 19:52:33 +0000 (20:52 +0100)
This works around the need to take a custom mutex twice and add the
object to a GSList of objects that are currently in construction for the
common case. Only when the constructor is overwritten do we use the
previous behavior and allow things like singleton objects.

The only slightly incompatible change is that previously, it was ok to
call g_object_set() on construct-only properties while the object was
initialized. This will now fail. If that behavior is needed, setting a
custom constructor that just chains up will reenable this functionality.

https://bugzilla.gnome.org/show_bug.cgi?id=557151

gobject/gobject.c

index f906d31..58554c3 100644 (file)
 #define CLASS_HAS_PROPS_FLAG 0x1
 #define CLASS_HAS_PROPS(class) \
     ((class)->flags & CLASS_HAS_PROPS_FLAG)
+#define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
+    ((class)->constructor != g_object_constructor)
 
 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
 #define CLASS_HAS_DERIVED_CLASS(class) \
@@ -715,10 +717,13 @@ g_object_init (GObject            *object,
       g_object_notify_queue_freeze (object, &property_notify_context);
     }
 
-  /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
-  G_LOCK (construction_mutex);
-  construction_objects = g_slist_prepend (construction_objects, object);
-  G_UNLOCK (construction_mutex);
+  if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
+    {
+      /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
+      G_LOCK (construction_mutex);
+      construction_objects = g_slist_prepend (construction_objects, object);
+      G_UNLOCK (construction_mutex);
+    }
 
 #ifdef G_ENABLE_DEBUG
   IF_DEBUG (OBJECTS)
@@ -1257,10 +1262,15 @@ g_object_newv (GType       object_type,
   g_free (cvalues);
 
  did_construction:
-  /* adjust freeze_count according to g_object_init() and remaining properties */
-  G_LOCK (construction_mutex);
-  newly_constructed = slist_maybe_remove (&construction_objects, object);
-  G_UNLOCK (construction_mutex);
+  if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
+    {
+      /* adjust freeze_count according to g_object_init() and remaining properties */
+      G_LOCK (construction_mutex);
+      newly_constructed = slist_maybe_remove (&construction_objects, object);
+      G_UNLOCK (construction_mutex);
+    }
+  else
+    newly_constructed = TRUE;
 
   if (CLASS_HAS_PROPS (class))
     {