Search code examples
multithreadingconstructorstaticglibgobject

How to get static constructor like initialization in GObject?


I use GLib/GObject in C, and I have come in some situations where I would want something like static constructors that exist i.e. in C# and Java.

A static constructor would only be run once, upon first creation of an object. What is a nice feature about static constructors in C# or Java is they are thread safe. Even if multiple threads create objects of that class at the same time, only a single thread would execute the static constructor, and other threads (regular, non-static) constructor would block until that thread has finished.

This make static constructors a perfect place for thread-safe static field initialization (which are just global variables in C/Gobject).

If you can't think of a case where such a feature would be needed, ckeckout this question that would be easily solvable with static constructors: How to cleanly instantiate a global mutex that is shared by threads in one of the threads itself


Solution

  • Put it in the class_init function. In case you're unsure how to do this, here is a trivial example:

    #include <glib.h>
    #include <glib-object.h>
    
    
    #define TYPE_FOO (foo_get_type ())
    #define FOO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FOO, Foo))
    #define FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FOO, FooClass))
    #define IS_FOO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FOO))
    #define IS_FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FOO))
    #define FOO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FOO, FooClass))
    
    typedef struct _Foo Foo;
    typedef struct _FooClass FooClass;
    typedef struct _FooPrivate FooPrivate;
    
    struct _Foo {
        GObject parent_instance;
        FooPrivate * priv;
    };
    
    struct _FooClass {
        GObjectClass parent_class;
    };
    
    
    static gpointer foo_parent_class = NULL;
    
    GType foo_get_type (void) G_GNUC_CONST;
    enum  {
        FOO_DUMMY_PROPERTY
    };
    Foo* foo_new (void);
    Foo* foo_construct (GType object_type);
    
    
    Foo* foo_construct (GType object_type) {
        Foo * self = NULL;
        self = (Foo*) g_object_new (object_type, NULL);
        return self;
    }
    
    
    Foo* foo_new (void) {
        return foo_construct (TYPE_FOO);
    }
    
    
    static void foo_class_init (FooClass * klass) {
        foo_parent_class = g_type_class_peek_parent (klass);
        /* static construct code goes here */
    }
    
    
    static void foo_instance_init (Foo * self) {
    }
    
    
    GType foo_get_type (void) {
        static volatile gsize foo_type_id__volatile = 0;
        if (g_once_init_enter (&foo_type_id__volatile)) {
            static const GTypeInfo g_define_type_info = { sizeof (FooClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) foo_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Foo), 0, (GInstanceInitFunc) foo_instance_init, NULL };
            GType foo_type_id;
            foo_type_id = g_type_register_static (G_TYPE_OBJECT, "Foo", &g_define_type_info, 0);
            g_once_init_leave (&foo_type_id__volatile, foo_type_id);
        }
        return foo_type_id__volatile;
    }