glocalfile: add private worker monitor APIs

Add a convenient and race-free method of watching local files from the
GLib worker thread.

Without this, the race-free way to create a monitor that dispatches
events to the worker thread looked something like this:

 - dispatch an idle to the worker thread
 - from the idle, create the monitor and connect signals
 - from the original thread, wait (on a cond?) until the worker thread
   has finished setting up the monitor
 - read the file that you were monitoring

which is just ridiculously complicated...

To use the new API:

  monitor = g_local_file_monitor_new_in_worker ("/path/to/some/file",
                                                G_FILE_MONITOR_NONE,
                                                &error);
  g_assert_no_error (error);

  g_signal_connect (monitor, "changed", G_CALLBACK (callback), NULL);

  g_local_file_monitor_start (monitor);

'callback' will run from the GLib worker thread.

This is the reason that the start() call was introduced in the previous
commit.  The backends that don't use the start() call will have a very
thin race between creating the monitor and connecting the signal, but
hopefully they will be fixed soon.

These new APIs will be used (at least) from gdesktopappinfo to watch for
changes in the desktop file directories.

https://bugzilla.gnome.org/show_bug.cgi?id=704887
This commit is contained in:
Ryan Lortie
2013-07-25 14:05:36 -04:00
parent 5409d7827e
commit 33762a4173
5 changed files with 48 additions and 8 deletions

View File

@@ -200,9 +200,11 @@ mounts_changed (GUnixMountMonitor *mount_monitor,
GFileMonitor*
_g_local_directory_monitor_new (const char *dirname,
GFileMonitorFlags flags,
GFileMonitorFlags flags,
GMainContext *context,
gboolean is_remote_fs,
GError **error)
gboolean do_start,
GError **error)
{
GFileMonitor *monitor = NULL;
GType type = G_TYPE_INVALID;
@@ -218,12 +220,12 @@ _g_local_directory_monitor_new (const char *dirname,
G_STRUCT_OFFSET (GLocalDirectoryMonitorClass, is_supported));
if (type != G_TYPE_INVALID)
monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, NULL));
monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, "context", context, NULL));
else
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Unable to find default local directory monitor type"));
if (monitor)
if (monitor && do_start)
g_local_directory_monitor_start (G_LOCAL_DIRECTORY_MONITOR (monitor));
return monitor;