From: Valeri Ochinski Date: Sun, 17 Dec 2023 15:44:31 +0000 (+0300) Subject: codegen: Remove static mutex initialization X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8781e8ae8c360474266c0c72923dc18e3db0993e;p=thirdparty%2Fvala.git codegen: Remove static mutex initialization Mutex.init documentation points out that "It is not necessary to initialize a mutex that has been statically allocated." --- diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala index 0cd60344c..c380763b8 100644 --- a/codegen/valagobjectmodule.vala +++ b/codegen/valagobjectmodule.vala @@ -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)); diff --git a/tests/objects/singleton.c-expected b/tests/objects/singleton.c-expected index b08773c34..e7714c0d8 100644 --- a/tests/objects/singleton.c-expected +++ b/tests/objects/singleton.c-expected @@ -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) {