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));
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));
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) {
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) {