Monthly Archives: January 2010

Fools rush in where angels fear to tread

I really had to write an email (quoted below) to the author of this blog post regarding the old GNU/Linux vs Linux battle

Just wanted to tell you that you are sooooo wrong in most of the things
you say… and that it is very very funny how you say you can remove all
GNU apps from several distros.

Did you remove GNU coreutils? Then how can you for example do a simple
“ls” in the command line?

Oh, and do you know what the “G” means in GNOME?
http://en.wikipedia.org/wiki/GNOME

Glib? GTK+? All GNU packages.

And you’re part of an Ubuntu LoCo team —> What about Bazaar, widely
used in Ubuntu, as it is written by Canonical people… Also a GNU
package!!
https://lists.ubuntu.com/archives/bazaar-announce/2008-February/000135.html

Tell me now what you get if you remove all GNU packages from Ubuntu (if
theoretically possible). Including GLib, GTK+ (and of course all their
dependencies); including coreutils, bash, gdb, gcc… including the
glibc (GNU C library)!

Now what you will probably get?

Probably *nothing*. Even the Linux kernel depends on the glibc.

Now, next time, I would suggest you just read a little bit more and
understand what GNU means, before writing such wrong post.

Check which is the whole list of GNU packages here:
http://directory.fsf.org/GNU

g_source_unref() and g_source_destroy() are your friends

After almost 2 years developing GLib-based applications, I understood the proper way of using GSource objects. Yes, quite a shame, but better now than never.

Now, after re-reading carefully the GLib Reference Manual, I see that it is quite clearly explained the difference between g_source_destroy() and g_source_unref(). So the typical suggestion is still the best one: RTFM!!

In our applications, we usually need to attach timeout operations to the context of an specific thread, not to the main thread context. Thus, we cannot use g_timeout_add() or the pretty new g_timeout_add_seconds().

For example, you could create a GThreadPool, and in the function to be executed in each thread, you could create a specific context for the thread, plus a main loop in the context:

    /* Create a GLib Main Context */
    context = g_main_context_new();

    /* Create a Main Loop in the context*/
    main_loop = g_main_loop_new(context,
                                FALSE);

Once you have a new context and main loop, you can just create a new GSource, and attach it to the main loop. As soon as you create the GSource, its reference count is 1, and as soon as you attach it to the main loop, its reference count will be 2.

    /* Create new timeout source to be called
     * every 5 seconds.
     * Reference count of source is 1 once created */
    source = g_timeout_source_new(TIMEOUT_MSECS);

    /* Set callback to be called in each timeout */
    g_source_set_callback(source,
                          (GSourceFunc)__timeout_func,
                          main_loop,
                          NULL);

    /* Attach the GSource in the GMainContext.
     * Reference count of source is 2 after this call */
    g_source_attach(source,
                    context);

Of, course, you will be now running the main loop:

    /* Run the main loop, until it is stopped */
    g_main_loop_run(main_loop);

The key now is how to destroy the GSource properly. When calling g_source_destroy(), you are doing 2 things: first, telling the main loop to forget about the GSource; and second, decrementing the reference count of the GSource. Then, you still need to call g_source_unref() to fully decrement the reference counter so that the GSource is disposed.

    /* We did an attach() with the GSource, so we need to
     * destroy() it */
    g_source_destroy(source);

    /* We need to unref() the GSource to end the last reference
     * we got */
    g_source_unref(source);

To end the example, once the main loop is stopped, you will also need to properly dispose the GMainContext and GMainLoop objects:

    /* The main loop should be destroyed before the context */
    g_main_loop_unref(main_loop);

    /* Finally, destroy the context */
    g_main_context_unref(context);

This is just one way of keeping the GSource references properly managed:

  1. You get one reference when you create the GSource
  2. You get a new reference when you attach it in the context
  3. You release one reference when you destroy it from the context
  4. You release last reference when you unref the final one

Of course, you could also choose to avoid storing the “extra” reference, and leave alive only the one inside the main context:

  1. You get one reference when you create the GSource
  2. You get a new reference when you attach it in the context
  3. You release one reference when you unref the the GSource —> Now, the only reference is inside the GMainContext
  4. You release last reference when you destroy it from the context

You can check this simple example in the following program I prepared, released into public domain:
http://es.gnu.org/~aleksander/glib/test-gsource.c

Hope it helps someone out there to fully understand the difference between g_source_destroy() and g_source_unref().

As last comment… I would really rename the following functions in the library, so that no one else is confused with the “unref” and “destroy” terms:

  • Rename g_source_attach() to g_source_attach_to_context()
  • Rename g_source_destroy() to g_source_destroy_from_context()

The main reason is that these two operations actually act on the GMainContext, while the name of the functions do not suggest that fact.