[gvfs/gdbus-core] gdbus: Use private GMainContext for sync enumerator



commit 95f94747ed01fe82bb991102044beb0a8b860400
Author: Tomas Bzatek <tbzatek redhat com>
Date:   Tue Jul 3 15:41:52 2012 +0200

    gdbus: Use private GMainContext for sync enumerator
    
    It's little messy due to shared code across sync calls with private
    main context and async calls that don't have it's mainloop and can't use
    private context.

 client/gdaemonfile.c           |    4 +-
 client/gdaemonfileenumerator.c |   51 ++++++++++++++++++++++++++-------------
 client/gdaemonfileenumerator.h |    7 +++--
 3 files changed, 40 insertions(+), 22 deletions(-)
---
diff --git a/client/gdaemonfile.c b/client/gdaemonfile.c
index 64a0148..1226530 100644
--- a/client/gdaemonfile.c
+++ b/client/gdaemonfile.c
@@ -694,7 +694,7 @@ g_daemon_file_enumerate_children (GFile      *file,
   
   g_print ("g_daemon_file_enumerate_children\n");
   
-  enumerator = g_daemon_file_enumerator_new (file, attributes);
+  enumerator = g_daemon_file_enumerator_new (file, attributes, TRUE);
 
   proxy = create_proxy_for_file (file, NULL, &path, &connection, cancellable, error);
   if (proxy == NULL)
@@ -3474,7 +3474,7 @@ g_daemon_file_enumerate_children_async (GFile                      *file,
   data->io_priority = io_priority;
   if (cancellable)
     data->cancellable = g_object_ref (cancellable);
-  data->enumerator = g_daemon_file_enumerator_new (data->file, data->attributes);
+  data->enumerator = g_daemon_file_enumerator_new (data->file, data->attributes, FALSE);
 
   create_proxy_for_file_async (file,
                                cancellable,
diff --git a/client/gdaemonfileenumerator.c b/client/gdaemonfileenumerator.c
index 6f62e82..fd8c0a1 100644
--- a/client/gdaemonfileenumerator.c
+++ b/client/gdaemonfileenumerator.c
@@ -58,6 +58,7 @@ struct _GDaemonFileEnumerator
   guint timeout_tag;
   GSimpleAsyncResult *async_res;
   GMainLoop *next_files_mainloop;
+  GMainContext *next_files_context;
   guint next_files_sync_timeout_tag;
   GMutex next_files_mutex;
 
@@ -122,7 +123,10 @@ g_daemon_file_enumerator_finalize (GObject *object)
 
   if (daemon->sync_connection)
     g_object_unref (daemon->sync_connection);
-  
+
+  if (daemon->next_files_context)
+    g_main_context_unref (daemon->next_files_context);
+
   g_mutex_clear (&daemon->next_files_mutex);
   
   if (G_OBJECT_CLASS (g_daemon_file_enumerator_parent_class)->finalize)
@@ -233,6 +237,10 @@ register_vfs_filter_cb (GDBusConnection *connection,
 {
   GError *error;
   GVfsDBusEnumerator *skeleton;
+  GDaemonFileEnumerator *daemon = G_DAEMON_FILE_ENUMERATOR (callback_data);
+
+  if (daemon->next_files_context)
+    g_main_context_push_thread_default (daemon->next_files_context);
 
   skeleton = gvfs_dbus_enumerator_skeleton_new ();
   g_signal_connect (skeleton, "handle-done", G_CALLBACK (handle_done), callback_data);
@@ -251,38 +259,44 @@ register_vfs_filter_cb (GDBusConnection *connection,
       g_error_free (error);
     }
 
+  if (daemon->next_files_context)
+    g_main_context_pop_thread_default (daemon->next_files_context);
+
   return G_DBUS_INTERFACE_SKELETON (skeleton);
 }
 
 static void
 g_daemon_file_enumerator_init (GDaemonFileEnumerator *daemon)
 {
-  char *path;
-  
   daemon->id = g_atomic_int_add (&path_counter, 1);
 
   g_mutex_init (&daemon->next_files_mutex);
-  
-  path = g_daemon_file_enumerator_get_object_path (daemon);
-  g_print ("g_daemon_file_enumerator_init: daemon = %p, obj_path = '%s'\n", daemon, path);
-
-  _g_dbus_register_vfs_filter (path, 
-                               register_vfs_filter_cb,
-			       G_OBJECT (daemon));
-  g_free (path);
 }
 
 GDaemonFileEnumerator *
 g_daemon_file_enumerator_new (GFile *file,
-			      const char *attributes)
+			      const char *attributes,
+			      gboolean sync)
 {
   GDaemonFileEnumerator *daemon;
   char *treename;
+  char *path;
 
   daemon = g_object_new (G_TYPE_DAEMON_FILE_ENUMERATOR,
                          "container", file,
                          NULL);
 
+  if (sync)
+    daemon->next_files_context = g_main_context_new ();
+
+  path = g_daemon_file_enumerator_get_object_path (daemon);
+  g_print ("g_daemon_file_enumerator_new: daemon = %p, obj_path = '%s', sync = %d\n", daemon, path, sync);
+
+  _g_dbus_register_vfs_filter (path,
+                               register_vfs_filter_cb,
+                               G_OBJECT (daemon));
+  g_free (path);
+
   daemon->matcher = g_file_attribute_matcher_new (attributes);
   if (g_file_attribute_matcher_enumerate_namespace (daemon->matcher, "metadata") ||
       g_file_attribute_matcher_enumerate_next (daemon->matcher) != NULL)
@@ -437,7 +451,7 @@ g_daemon_file_enumerator_get_object_path (GDaemonFileEnumerator *enumerator)
 
 void
 g_daemon_file_enumerator_set_sync_connection (GDaemonFileEnumerator *enumerator,
-					      GDBusConnection       *connection)
+                                              GDBusConnection       *connection)
 {
   enumerator->sync_connection = g_object_ref (connection);
 }
@@ -482,19 +496,22 @@ g_daemon_file_enumerator_next_file (GFileEnumerator *enumerator,
   if (! daemon->infos && ! daemon->done)
     {
       /* Wait for incoming data */
-      daemon->next_files_sync_timeout_tag = g_timeout_add (G_VFS_DBUS_TIMEOUT_MSECS,
-                                                           sync_timeout, daemon);
-
       g_mutex_lock (&daemon->next_files_mutex);
       g_print ("g_daemon_file_enumerator_next_file: starting loop, daemon = %p\n", daemon);
-      daemon->next_files_mainloop = g_main_loop_new (NULL, FALSE);
+      daemon->next_files_mainloop = g_main_loop_new (daemon->next_files_context, FALSE);
+
       g_mutex_unlock (&daemon->next_files_mutex);
       
+      g_main_context_push_thread_default (daemon->next_files_context);
+      daemon->next_files_sync_timeout_tag = g_timeout_add (G_VFS_DBUS_TIMEOUT_MSECS,
+                                                           sync_timeout, daemon);
       g_main_loop_run (daemon->next_files_mainloop);
+      g_main_context_pop_thread_default (daemon->next_files_context);
 
       g_mutex_lock (&daemon->next_files_mutex);
       g_print ("g_daemon_file_enumerator_next_file: loop done.\n");
       g_source_remove (daemon->next_files_sync_timeout_tag);
+
       g_main_loop_unref (daemon->next_files_mainloop);
       daemon->next_files_mainloop = NULL;
       g_mutex_unlock (&daemon->next_files_mutex);
diff --git a/client/gdaemonfileenumerator.h b/client/gdaemonfileenumerator.h
index 1703804..3ded1e9 100644
--- a/client/gdaemonfileenumerator.h
+++ b/client/gdaemonfileenumerator.h
@@ -46,11 +46,12 @@ struct _GDaemonFileEnumeratorClass
 GType g_daemon_file_enumerator_get_type (void) G_GNUC_CONST;
 
 GDaemonFileEnumerator *g_daemon_file_enumerator_new                 (GFile *file,
-								     const char *attributes);
+								     const char *attributes,
+								     gboolean sync);
 char  *                g_daemon_file_enumerator_get_object_path     (GDaemonFileEnumerator *enumerator);
-void                   g_daemon_file_enumerator_set_sync_connection (GDaemonFileEnumerator *enumerator,
-								     GDBusConnection       *connection);
 
+void                   g_daemon_file_enumerator_set_sync_connection (GDaemonFileEnumerator *enumerator,
+                                                                     GDBusConnection       *connection);
 
 G_END_DECLS
 



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