From 33762a417381db8cdd3229f12959ed01c0389165 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Thu, 25 Jul 2013 14:05:36 -0400 Subject: [PATCH] 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 --- gio/glocaldirectorymonitor.c | 10 ++++++---- gio/glocaldirectorymonitor.h | 7 +++++++ gio/glocalfile.c | 26 ++++++++++++++++++++++++-- gio/glocalfilemonitor.c | 6 ++++-- gio/glocalfilemonitor.h | 7 +++++++ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/gio/glocaldirectorymonitor.c b/gio/glocaldirectorymonitor.c index 588f6e1c1..942643e89 100644 --- a/gio/glocaldirectorymonitor.c +++ b/gio/glocaldirectorymonitor.c @@ -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; diff --git a/gio/glocaldirectorymonitor.h b/gio/glocaldirectorymonitor.h index 16d8ac1ec..94740bd77 100644 --- a/gio/glocaldirectorymonitor.h +++ b/gio/glocaldirectorymonitor.h @@ -70,10 +70,17 @@ GType g_local_directory_monitor_get_type (void) G_GNUC_CONST; GFileMonitor * _g_local_directory_monitor_new (const char *dirname, GFileMonitorFlags flags, + GMainContext *context, gboolean is_remote_fs, + gboolean do_start, GError **error); void g_local_directory_monitor_start (GLocalDirectoryMonitor *local_monitor); +/* Actually in glocalfile.c */ +GLocalDirectoryMonitor * g_local_directory_monitor_new_in_worker (const char *pathname, + GFileMonitorFlags flags, + GError **error); + G_END_DECLS #endif /* __G_LOCAL_DIRECTORY_MONITOR_H__ */ diff --git a/gio/glocalfile.c b/gio/glocalfile.c index 3763a464d..e28a8be85 100644 --- a/gio/glocalfile.c +++ b/gio/glocalfile.c @@ -72,6 +72,8 @@ #endif #include "glib-private.h" +#include "glib-private.h" + #ifdef G_OS_WIN32 #include #include @@ -2505,7 +2507,7 @@ g_local_file_monitor_dir (GFile *file, GError **error) { GLocalFile* local_file = G_LOCAL_FILE(file); - return _g_local_directory_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error); + return _g_local_directory_monitor_new (local_file->filename, flags, NULL, is_remote (local_file->filename), TRUE, error); } static GFileMonitor* @@ -2515,7 +2517,27 @@ g_local_file_monitor_file (GFile *file, GError **error) { GLocalFile* local_file = G_LOCAL_FILE(file); - return _g_local_file_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error); + return _g_local_file_monitor_new (local_file->filename, flags, NULL, is_remote (local_file->filename), TRUE, error); +} + +GLocalDirectoryMonitor * +g_local_directory_monitor_new_in_worker (const char *pathname, + GFileMonitorFlags flags, + GError **error) +{ + return (gpointer) _g_local_directory_monitor_new (pathname, flags, + GLIB_PRIVATE_CALL (g_get_worker_context) (), + is_remote (pathname), FALSE, error); +} + +GLocalFileMonitor * +g_local_file_monitor_new_in_worker (const char *pathname, + GFileMonitorFlags flags, + GError **error) +{ + return (gpointer) _g_local_file_monitor_new (pathname, flags, + GLIB_PRIVATE_CALL (g_get_worker_context) (), + is_remote (pathname), FALSE, error); } diff --git a/gio/glocalfilemonitor.c b/gio/glocalfilemonitor.c index 765b98ff4..a4cbeae0b 100644 --- a/gio/glocalfilemonitor.c +++ b/gio/glocalfilemonitor.c @@ -122,7 +122,9 @@ static void g_local_file_monitor_class_init (GLocalFileMonitorClass *klass) GFileMonitor* _g_local_file_monitor_new (const char *pathname, GFileMonitorFlags flags, + GMainContext *context, gboolean is_remote_fs, + gboolean do_start, GError **error) { GFileMonitor *monitor = NULL; @@ -139,12 +141,12 @@ _g_local_file_monitor_new (const char *pathname, G_STRUCT_OFFSET (GLocalFileMonitorClass, is_supported)); if (type != G_TYPE_INVALID) - monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, "flags", flags, NULL)); + monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, "flags", flags, "context", context, NULL)); else g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Unable to find default local file monitor type")); - if (monitor) + if (monitor && do_start) g_local_file_monitor_start (G_LOCAL_FILE_MONITOR (monitor)); return monitor; diff --git a/gio/glocalfilemonitor.h b/gio/glocalfilemonitor.h index e6bb62314..74baaa48e 100644 --- a/gio/glocalfilemonitor.h +++ b/gio/glocalfilemonitor.h @@ -63,10 +63,17 @@ GType g_local_file_monitor_get_type (void) G_GNUC_CONST; GFileMonitor * _g_local_file_monitor_new (const char *pathname, GFileMonitorFlags flags, + GMainContext *context, gboolean is_remote_fs, + gboolean do_start, GError **error); void g_local_file_monitor_start (GLocalFileMonitor *local_monitor); +/* Actually in glocalfile.c */ +GLocalFileMonitor * g_local_file_monitor_new_in_worker (const char *pathname, + GFileMonitorFlags flags, + GError **error); + G_END_DECLS #endif /* __G_LOCAL_FILE_MONITOR_H__ */