[nautilus] thumbnails: replace pthread API with GTask and GThread



commit b2ee410dc353a871992eebcbdbb4399daa59d57b
Author: Razvan Chitu <razvan ch95 gmail com>
Date:   Fri Feb 19 10:24:34 2016 +0200

    thumbnails: replace pthread API with GTask and GThread
    
    In Nautilus, thumbnail creation is handled on a separate thread. This thread is
    created and managed using the POSIX threads API. For better threading support,
    GTask and GThread should be used instead.
    
    Replace the pthread with a GTask. Replace pthread_mutex with GMutex.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=762234

 libnautilus-private/nautilus-thumbnails.c |   50 ++++++++++++++--------------
 1 files changed, 25 insertions(+), 25 deletions(-)
---
diff --git a/libnautilus-private/nautilus-thumbnails.c b/libnautilus-private/nautilus-thumbnails.c
index d52677c..c8f7bb0 100644
--- a/libnautilus-private/nautilus-thumbnails.c
+++ b/libnautilus-private/nautilus-thumbnails.c
@@ -38,7 +38,6 @@
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
-#include <pthread.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <signal.h>
@@ -57,7 +56,10 @@
 /* Cool-off period between last file modification time and thumbnail creation */
 #define THUMBNAIL_CREATION_DELAY_SECS 3
 
-static gpointer thumbnail_thread_start (gpointer data);
+static void thumbnail_thread_func (GTask        *task,
+                                   gpointer      source_object,
+                                   gpointer      task_data,
+                                   GCancellable *cancellable);
 
 /* structure used for making thumbnails, associating a uri with where the thumbnail is to be stored */
 
@@ -78,7 +80,7 @@ static guint thumbnail_thread_starter_id = 0;
 /* Our mutex used when accessing data shared between the main thread and the
    thumbnail thread, i.e. the thumbnail_thread_is_running flag and the
    thumbnails_to_make list. */
-static pthread_mutex_t thumbnails_mutex = PTHREAD_MUTEX_INITIALIZER;
+static GMutex thumbnails_mutex;
 
 /* A flag to indicate whether a thumbnail thread is running, so we don't
    start more than one. Lock thumbnails_mutex when accessing this. */
@@ -150,19 +152,13 @@ get_thumbnail_factory (void)
 static gboolean
 thumbnail_thread_starter_cb (gpointer data)
 {
-       pthread_attr_t thread_attributes;
-       pthread_t thumbnail_thread;
+       GTask *task;
 
        /* Don't do this in thread, since g_object_ref is not threadsafe */
        if (thumbnail_factory == NULL) {
                thumbnail_factory = get_thumbnail_factory ();
        }
 
-       /* We create the thread in the detached state, as we don't need/want
-          to join with it at any point. */
-       pthread_attr_init (&thread_attributes);
-       pthread_attr_setdetachstate (&thread_attributes,
-                                    PTHREAD_CREATE_DETACHED);
 #ifdef DEBUG_THUMBNAILS
        g_message ("(Main Thread) Creating thumbnails thread\n");
 #endif
@@ -172,11 +168,12 @@ thumbnail_thread_starter_cb (gpointer data)
           twice, as we also check thumbnail_thread_starter_id before
           scheduling this idle function. */
        thumbnail_thread_is_running = TRUE;
-       pthread_create (&thumbnail_thread, &thread_attributes,
-                       thumbnail_thread_start, NULL);
-
+       task = g_task_new (NULL, NULL, NULL, NULL);
+       g_task_run_in_thread (task, thumbnail_thread_func);
        thumbnail_thread_starter_id = 0;
 
+       g_object_unref (task);
+
        return FALSE;
 }
 
@@ -188,7 +185,7 @@ nautilus_thumbnail_remove_from_queue (const char *file_uri)
 #ifdef DEBUG_THUMBNAILS
        g_message ("(Remove from queue) Locking mutex\n");
 #endif
-       pthread_mutex_lock (&thumbnails_mutex);
+       g_mutex_lock (&thumbnails_mutex);
 
        /*********************************
         * MUTEX LOCKED
@@ -211,7 +208,7 @@ nautilus_thumbnail_remove_from_queue (const char *file_uri)
 #ifdef DEBUG_THUMBNAILS
        g_message ("(Remove from queue) Unlocking mutex\n");
 #endif
-       pthread_mutex_unlock (&thumbnails_mutex);
+       g_mutex_unlock (&thumbnails_mutex);
 }
 
 void
@@ -222,7 +219,7 @@ nautilus_thumbnail_prioritize (const char *file_uri)
 #ifdef DEBUG_THUMBNAILS
        g_message ("(Prioritize) Locking mutex\n");
 #endif
-       pthread_mutex_lock (&thumbnails_mutex);
+       g_mutex_lock (&thumbnails_mutex);
 
        /*********************************
         * MUTEX LOCKED
@@ -244,7 +241,7 @@ nautilus_thumbnail_prioritize (const char *file_uri)
 #ifdef DEBUG_THUMBNAILS
        g_message ("(Prioritize) Unlocking mutex\n");
 #endif
-       pthread_mutex_unlock (&thumbnails_mutex);
+       g_mutex_unlock (&thumbnails_mutex);
 }
 
 
@@ -396,7 +393,7 @@ nautilus_create_thumbnail (NautilusFile *file)
 #ifdef DEBUG_THUMBNAILS
        g_message ("(Main Thread) Locking mutex\n");
 #endif
-       pthread_mutex_lock (&thumbnails_mutex);
+       g_mutex_lock (&thumbnails_mutex);
        
        /*********************************
         * MUTEX LOCKED
@@ -446,12 +443,15 @@ nautilus_create_thumbnail (NautilusFile *file)
 #ifdef DEBUG_THUMBNAILS
        g_message ("(Main Thread) Unlocking mutex\n");
 #endif
-       pthread_mutex_unlock (&thumbnails_mutex);
+       g_mutex_unlock (&thumbnails_mutex);
 }
 
 /* thumbnail_thread is invoked as a separate thread to to make thumbnails. */
-static gpointer
-thumbnail_thread_start (gpointer data)
+static void
+thumbnail_thread_func (GTask        *task,
+                       gpointer      source_object,
+                       gpointer      task_data,
+                       GCancellable *cancellable)
 {
        NautilusThumbnailInfo *info = NULL;
        GdkPixbuf *pixbuf;
@@ -465,7 +465,7 @@ thumbnail_thread_start (gpointer data)
 #ifdef DEBUG_THUMBNAILS
                g_message ("(Thumbnail Thread) Locking mutex\n");
 #endif
-               pthread_mutex_lock (&thumbnails_mutex);
+               g_mutex_lock (&thumbnails_mutex);
 
                /*********************************
                 * MUTEX LOCKED
@@ -497,8 +497,8 @@ thumbnail_thread_start (gpointer data)
                        g_message ("(Thumbnail Thread) Exiting\n");
 #endif
                        thumbnail_thread_is_running = FALSE;
-                       pthread_mutex_unlock (&thumbnails_mutex);
-                       pthread_exit (NULL);
+                       g_mutex_unlock (&thumbnails_mutex);
+                       return;
                }
 
                /* Get the next one to make. We leave it on the list until it
@@ -514,7 +514,7 @@ thumbnail_thread_start (gpointer data)
 #ifdef DEBUG_THUMBNAILS
                g_message ("(Thumbnail Thread) Unlocking mutex\n");
 #endif
-               pthread_mutex_unlock (&thumbnails_mutex);
+               g_mutex_unlock (&thumbnails_mutex);
 
                time (&current_time);
 


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