[Vala] Thread unsafe GObject?



Hi,

i am not sure if i should post this here but i think there is a problem with the gobject-library and threads 
or object orientation in vala and threads
every time when i create an object in vala some of the resulting c-code looks like this:

GType <class_name>_get_type (void) {
..
                <class_name>_type_id = g_type_register_fundamental (g_type_fundamental_next (), 
"<ClassName>", &g_define_type_info, &g_define_type_fundamental_info, 0);
..
        }
        return <class_name>_type_id__volatile;
}

but when i have some threads (in my program there are about 5 of them) and the program is running on a 
multi-core coimputer it sometimes happens, that the function "g_type_fundamental_next" is called by two 
threads at the same time.
because the function "g_type_fundamental_next" just returns the current value of "static_fundamental_next" 
for the new type (i dont know if i did understand all of the things going up here - so just correct me if 
this is wrong :)).
now two threads got the same fundamental-type id for two different types and the value of 
static_fundamental_next is just incremented in the function "type_node_fundamental_new_W", which is called by 
"g_type_register_fundamental".
because of this - my program crashes with following error:
"GLib-GObject-WARNING **: cannot register existing fundamental type `<type1>' (as `<type2')"

to solve the problem i have rewritten the gobject library so it increments the value of 
static_fundamental_next in function "g_type_fundamental_next" with a write-lock around it.
but wouldn't it be better if vala locks the line
                <class_name>_type_id = g_type_register_fundamental (g_type_fundamental_next (), 
"<ClassName>", &g_define_type_info, &g_define_type_fundamental_info, 0);
in order to guarantee that object orientation is thread safe in vala?

gerhard

ps: the gobject functions are defined in gobject/gtype.c in the glib source


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