]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Remove static mutex initialization
authorValeri Ochinski <v19930312@gmail.com>
Sun, 17 Dec 2023 15:44:31 +0000 (18:44 +0300)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 18 Dec 2023 19:36:55 +0000 (20:36 +0100)
Mutex.init documentation points out that "It is not necessary to initialize
a mutex that has been statically allocated."

codegen/valagobjectmodule.vala
tests/objects/singleton.c-expected

index 0cd60344cb2dafdf278a579c7c8a09d5113f5fd4..c380763b880201fbb1ef24bef9fcdd036e0dba98 100644 (file)
@@ -522,7 +522,6 @@ public class Vala.GObjectModule : GTypeModule {
                        if (cl.is_singleton) {
                                var singleton_ref_name = "%s_singleton__ref".printf (get_ccode_name (cl));
                                var singleton_lock_name = "%s_singleton__lock".printf (get_ccode_name (cl));
-                               var singleton_once_name = "%s_singleton__once".printf (get_ccode_name (cl));
 
                                var singleton_ref = new CCodeDeclaration("GWeakRef");
                                singleton_ref.add_declarator (new CCodeVariableDeclarator (singleton_ref_name));
@@ -534,32 +533,6 @@ public class Vala.GObjectModule : GTypeModule {
                                mutex_lock.modifiers = CCodeModifiers.STATIC;
                                ccode.add_statement (mutex_lock);
 
-                               var once_lock = new CCodeDeclaration("gsize");
-                               once_lock.add_declarator (new CCodeVariableDeclarator (singleton_once_name, new CCodeConstant ("0")));
-                               if (context.require_glib_version (2, 68)) {
-                                       once_lock.modifiers = CCodeModifiers.STATIC;
-                               } else {
-                                       once_lock.modifiers = CCodeModifiers.STATIC | CCodeModifiers.VOLATILE;
-                               }
-                               ccode.add_statement (once_lock);
-
-                               var once_init = new CCodeFunctionCall (new CCodeIdentifier ("g_once_init_enter"));
-                               once_init.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (singleton_once_name)));
-
-                               var once_block = new CCodeBlock();
-
-                               var singleton_mutex_init = new CCodeFunctionCall (new CCodeIdentifier ("g_mutex_init"));
-                               singleton_mutex_init.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (singleton_lock_name)));
-                               once_block.add_statement (new CCodeExpressionStatement (singleton_mutex_init));
-
-                               var once_leave = new CCodeFunctionCall (new CCodeIdentifier ("g_once_init_leave"));
-                               once_leave.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (singleton_once_name)));
-                               once_leave.add_argument (new CCodeConstant ("42"));
-                               once_block.add_statement (new CCodeExpressionStatement (once_leave));
-
-                               var if_once = new CCodeIfStatement (once_init, once_block);
-                               ccode.add_statement (if_once);
-
                                var singleton_mutex_lock = new CCodeFunctionCall (new CCodeIdentifier ("g_mutex_lock"));
                                singleton_mutex_lock.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (singleton_lock_name)));
                                ccode.add_statement (new CCodeExpressionStatement (singleton_mutex_lock));
index b08773c34c1ad6229b6b994634ab6bb3d715e9ea..e7714c0d8aa8365f35be1e2f844e802a0daeecd3 100644 (file)
@@ -120,12 +120,7 @@ foo_constructor (GType type,
        GObjectClass * parent_class;
        static GWeakRef Foo_singleton__ref;
        static GMutex Foo_singleton__lock;
-       static volatile gsize Foo_singleton__once = 0;
        Foo * self;
-       if (g_once_init_enter (&Foo_singleton__once)) {
-               g_mutex_init (&Foo_singleton__lock);
-               g_once_init_leave (&Foo_singleton__once, 42);
-       }
        g_mutex_lock (&Foo_singleton__lock);
        obj = g_weak_ref_get (&Foo_singleton__ref);
        if (obj != NULL) {
@@ -208,12 +203,7 @@ bar_constructor (GType type,
        GObjectClass * parent_class;
        static GWeakRef Bar_singleton__ref;
        static GMutex Bar_singleton__lock;
-       static volatile gsize Bar_singleton__once = 0;
        Bar * self;
-       if (g_once_init_enter (&Bar_singleton__once)) {
-               g_mutex_init (&Bar_singleton__lock);
-               g_once_init_leave (&Bar_singleton__once, 42);
-       }
        g_mutex_lock (&Bar_singleton__lock);
        obj = g_weak_ref_get (&Bar_singleton__ref);
        if (obj != NULL) {