Creating a new GSource in GLib

I am trying to understand what it means when I call g_source_new. The latest API documentation (currently 2.38.2) in the call simply says:

Creates a new GSource structure. The size is specified to create structures created from GSource that contain additional data. The missing size must be at least sizeof (GSource).

I am trying to understand if this API refers to the fact that I am creating a new instance of mine GSourceor if it is designed to register a new type GSource.

The main question is: am I allowed to create one new one GSourcewith g_source_new, and then apply it to any number of contexts (via g_source_attach)? Or should I use both functions even when trying to apply the same ones GSourcethat I defined for several contexts?

+4
source share
1 answer

From the source definition, it seems you can only attach GSource to one GMainContext

struct _GSource
{
  /*< private >*/
  gpointer callback_data;
  GSourceCallbackFuncs *callback_funcs;

  const GSourceFuncs *source_funcs;
  guint ref_count;

  GMainContext *context;    // <<<<<

  gint priority;
  guint flags;
  guint source_id;

  GSList *poll_fds;

  GSource *prev;
  GSource *next;

  char    *name;

  GSourcePrivate *priv;
};

Take a look

static guint
g_source_attach_unlocked (GSource      *source,
                      GMainContext *context,
                      gboolean      do_wakeup)

who will tell you that only the bound will be awakened GMainContext.

Example derived GSourceuse: https://github.com/chergert/iris/blob/master/iris/iris-gsource.c

+4
source

Source: https://habr.com/ru/post/1524298/


All Articles