[gnome-builder] file: use GMutex to instead of GOnce



commit fda9d755b040374b22456175354938565c46b2a0
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jun 13 22:41:59 2018 -0700

    file: use GMutex to instead of GOnce
    
    I don't trust the use of gonce here, so just conservative and use gmutex.
    Also, we can increase the ref of the sourcefile just to be safe.

 src/libide/buffers/ide-buffer-manager.c | 21 +++++++-------
 src/libide/files/ide-file.c             | 49 ++++++++++++++++++---------------
 2 files changed, 38 insertions(+), 32 deletions(-)
---
diff --git a/src/libide/buffers/ide-buffer-manager.c b/src/libide/buffers/ide-buffer-manager.c
index 0c518f21a..9669d458a 100644
--- a/src/libide/buffers/ide-buffer-manager.c
+++ b/src/libide/buffers/ide-buffer-manager.c
@@ -758,8 +758,8 @@ ide_buffer_manager__load_file_read_cb (GObject      *object,
 {
   GFile *file = (GFile *)object;
   g_autoptr(GFileInputStream) stream = NULL;
+  g_autoptr(GtkSourceFile) source_file = NULL;
   g_autoptr(IdeTask) task = user_data;
-  GtkSourceFile *source_file;
   LoadState *state;
 
   IDE_ENTRY;
@@ -1091,13 +1091,13 @@ ide_buffer_manager_save_file__load_settings_cb (GObject      *object,
   IdeFile *file = (IdeFile *)object;
   g_autoptr(IdeFileSettings) file_settings = NULL;
   g_autoptr(IdeTask) task = user_data;
-  SaveState *state;
-  GtkSourceFileSaver *saver;
-  GtkSourceFile *source_file;
-  GtkSourceNewlineType newline_type;
+  g_autoptr(GtkSourceFile) source_file = NULL;
+  g_autoptr(GError) error = NULL;
   const GtkSourceEncoding *encoding;
+  GtkSourceNewlineType newline_type;
+  GtkSourceFileSaver *saver;
   const gchar *charset;
-  g_autoptr(GError) error = NULL;
+  SaveState *state;
 
   IDE_ENTRY;
 
@@ -1153,15 +1153,16 @@ ide_buffer_manager_save_file__load_settings_cb (GObject      *object,
   if (!ide_file_equal (file, ide_buffer_get_file (state->buffer)))
     {
       IdeFile *orig_file = ide_buffer_get_file (state->buffer);
+      g_autoptr(GtkSourceFile) orig_source_file = NULL;
 
       if (orig_file)
         {
-          source_file = _ide_file_get_source_file (orig_file);
+          orig_source_file = _ide_file_get_source_file (orig_file);
 
-          if (source_file)
+          if (orig_source_file)
             {
-              encoding = gtk_source_file_get_encoding (source_file);
-              newline_type = gtk_source_file_get_newline_type (source_file);
+              encoding = gtk_source_file_get_encoding (orig_source_file);
+              newline_type = gtk_source_file_get_newline_type (orig_source_file);
             }
         }
     }
diff --git a/src/libide/files/ide-file.c b/src/libide/files/ide-file.c
index 264d01d32..e981a2b12 100644
--- a/src/libide/files/ide-file.c
+++ b/src/libide/files/ide-file.c
@@ -36,6 +36,8 @@ struct _IdeFile
 {
   IdeObject          parent_instance;
 
+  GMutex             mutex;
+
   gchar             *content_type;
   GFile             *file;
   IdeFileSettings   *file_settings;
@@ -196,24 +198,24 @@ ide_file_set_file (IdeFile *self,
  *
  * Gets the GtkSourceFile for the #IdeFile.
  *
- * Returns: (transfer none): a #GtkSourceFile.
+ * Returns: (transfer full): a #GtkSourceFile.
  */
 GtkSourceFile *
 _ide_file_get_source_file (IdeFile *self)
 {
-  g_return_val_if_fail (IDE_IS_FILE (self), NULL);
-
-  if (g_once_init_enter (&self->source_file))
-    {
-      GtkSourceFile *source_file;
+  GtkSourceFile *ret;
 
-      source_file = gtk_source_file_new ();
-      gtk_source_file_set_location (source_file, self->file);
+  g_return_val_if_fail (IDE_IS_FILE (self), NULL);
 
-      g_once_init_leave (&self->source_file, source_file);
-    }
+  g_mutex_lock (&self->mutex);
+  if (self->source_file == NULL)
+    self->source_file = g_object_new (GTK_SOURCE_TYPE_FILE,
+                                      "location", self->file,
+                                      NULL);
+  ret = g_object_ref (self->source_file);
+  g_mutex_unlock (&self->mutex);
 
-  return self->source_file;
+  return ret;
 }
 
 const gchar *
@@ -221,12 +223,11 @@ ide_file_get_path (IdeFile *self)
 {
   g_return_val_if_fail (IDE_IS_FILE (self), NULL);
 
-  if (g_once_init_enter (&self->path))
-    {
-      IdeContext *context;
-      gchar *path = NULL;
+  g_mutex_lock (&self->mutex);
 
-      context = ide_object_get_context (IDE_OBJECT (self));
+  if (self->path == NULL)
+    {
+      IdeContext *context = ide_object_get_context (IDE_OBJECT (self));
 
       if (context != NULL)
         {
@@ -234,15 +235,15 @@ ide_file_get_path (IdeFile *self)
           GFile *workdir = ide_vcs_get_working_directory (vcs);
 
           if (g_file_has_prefix (self->file, workdir))
-            path = g_file_get_relative_path (workdir, self->file);
+            self->path = g_file_get_relative_path (workdir, self->file);
         }
 
-      if (path == NULL)
-        path = g_file_get_path (self->file);
-
-      g_once_init_leave (&self->path, path);
+      if (self->path == NULL)
+        self->path = g_file_get_path (self->file);
     }
 
+  g_mutex_unlock (&self->mutex);
+
   return self->path;
 }
 
@@ -442,6 +443,8 @@ ide_file_finalize (GObject *object)
   g_clear_pointer (&self->path, g_free);
   g_clear_pointer (&self->content_type, g_free);
 
+  g_mutex_clear (&self->mutex);
+
   G_OBJECT_CLASS (ide_file_parent_class)->finalize (object);
 
   DZL_COUNTER_DEC (instances);
@@ -564,9 +567,11 @@ ide_file_class_init (IdeFileClass *klass)
 }
 
 static void
-ide_file_init (IdeFile *file)
+ide_file_init (IdeFile *self)
 {
   DZL_COUNTER_INC (instances);
+
+  g_mutex_init (&self->mutex);
 }
 
 static gboolean


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