[vala/staging: 2/2] Use default_init instead of base_init when registering interfaces



commit 71f779e9309282be6effdc79220815fddecbce86
Author: Stef Walter <stefw redhat com>
Date:   Fri May 3 08:07:19 2013 +0200

    Use default_init instead of base_init when registering interfaces
    
    Using base_init() is overly complex, requires multi-initialization
    guards, and in the case of vala generated code these are currently
    not thread safe.
    
    The recommended approach is to use default_init() to initialize
    interfaces. It is called only once, solves thread safety issues.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=699550

 codegen/valagtypemodule.vala               |   22 +++++++---------------
 codegen/valainterfaceregisterfunction.vala |    4 ++--
 2 files changed, 9 insertions(+), 17 deletions(-)
---
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 6c58d61..abb19f6 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -2206,7 +2206,7 @@ public class Vala.GTypeModule : GErrorModule {
                        cfile.add_type_declaration (signal_array_decl);
                }
 
-               add_interface_base_init_function (iface);
+               add_interface_default_init_function (iface);
 
                if (iface.comment != null) {
                        cfile.add_type_member_definition (new CCodeComment (iface.comment.content));
@@ -2223,20 +2223,14 @@ public class Vala.GTypeModule : GErrorModule {
                pop_context ();
        }
 
-       private void add_interface_base_init_function (Interface iface) {
+       private void add_interface_default_init_function (Interface iface) {
                push_context (new EmitContext (iface));
 
-               var base_init = new CCodeFunction ("%s_base_init".printf (get_ccode_lower_case_name (iface, 
null)), "void");
-               base_init.add_parameter (new CCodeParameter ("iface", "%sIface *".printf (get_ccode_name 
(iface))));
-               base_init.modifiers = CCodeModifiers.STATIC;
-
-               push_function (base_init);
+               var default_init = new CCodeFunction ("%s_default_init".printf (get_ccode_lower_case_name 
(iface, null)), "void");
+               default_init.add_parameter (new CCodeParameter ("iface", "%sIface *".printf (get_ccode_name 
(iface))));
+               default_init.modifiers = CCodeModifiers.STATIC;
 
-               /* make sure not to run the initialization code twice */
-               ccode.add_declaration (get_ccode_name (bool_type), new CCodeVariableDeclarator 
("initialized", new CCodeConstant ("FALSE")), CCodeModifiers.STATIC);
-               ccode.open_if (new CCodeUnaryExpression (CCodeUnaryOperator.LOGICAL_NEGATION, new 
CCodeIdentifier ("initialized")));
-
-               ccode.add_assignment (new CCodeIdentifier ("initialized"), new CCodeConstant ("TRUE"));
+               push_function (default_init);
 
                if (iface.is_subtype_of (gobject_type)) {
                        /* create properties */
@@ -2303,11 +2297,9 @@ public class Vala.GTypeModule : GErrorModule {
                        }
                }
 
-               ccode.close ();
-
                pop_context ();
 
-               cfile.add_function (base_init);
+               cfile.add_function (default_init);
        }
 
        public override void visit_struct (Struct st) {
diff --git a/codegen/valainterfaceregisterfunction.vala b/codegen/valainterfaceregisterfunction.vala
index e2c6732..9990175 100644
--- a/codegen/valainterfaceregisterfunction.vala
+++ b/codegen/valainterfaceregisterfunction.vala
@@ -46,7 +46,7 @@ public class Vala.InterfaceRegisterFunction : TypeRegisterFunction {
        }
 
        public override string get_base_init_func_name () {
-               return "%s_base_init".printf (get_ccode_lower_case_name (interface_reference));
+               return "NULL";
        }
 
        public override string get_class_finalize_func_name () {
@@ -58,7 +58,7 @@ public class Vala.InterfaceRegisterFunction : TypeRegisterFunction {
        }
 
        public override string get_class_init_func_name () {
-               return "NULL";
+               return "%s_default_init".printf (get_ccode_lower_case_name (interface_reference));
        }
 
        public override string get_instance_struct_size () {


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