Is g_source_remove threadsafe?



I'm doing something where i have one thread queueing idles and timeouts
in a thread, and the main loop consumes this. In some cases i want to
remove the sources (to replace a timeout with an idle). However:

gboolean
g_source_remove (guint tag)
{
  GSource *source;
  
  g_return_val_if_fail (tag > 0, FALSE);

  source = g_main_context_find_source_by_id (NULL, tag);
  if (source)
    g_source_destroy (source);

  return source != NULL;
}

This doesn't seem threadsafe to me. g_main_context_find_source_by_id()
grabs the context lock, which is held during dispatches. However, there
is no lock held between g_main_context_find_source_by_id() returning and
g_source_destroy() being called, so what protects against the mainloop
triggering a dispatch on another thread at this point, invalidating the
pointer we have to the source (since we haven't got a ref on the
source).

Am I missing something obvious here?

I guess I will have to keep the actual source object around (with a ref
to it).



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