[nautilus/wip/ernestask/tasks: 2/9] Add task manager



commit 53f92e25dfff3b612fb54a843e12aced823d9f5f
Author: Ernestas Kulik <ernestask gnome org>
Date:   Thu May 11 16:29:20 2017 +0300

    Add task manager

 src/meson.build             |    2 +
 src/nautilus-task-manager.c |  137 +++++++++++++++++++++++++++++++++++++++++++
 src/nautilus-task-manager.h |   21 +++++++
 3 files changed, 160 insertions(+), 0 deletions(-)
---
diff --git a/src/meson.build b/src/meson.build
index 5f4b22b..9c31ffd 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -258,6 +258,8 @@ libnautilus_sources = [
     'nautilus-file-undo-manager.h',
     'nautilus-task.c',
     'nautilus-task.h',
+    'nautilus-task-manager.c',
+    'nautilus-task-manager.h'
 ]
 
 if get_option ('enable-tracker')
diff --git a/src/nautilus-task-manager.c b/src/nautilus-task-manager.c
new file mode 100644
index 0000000..8625ce5
--- /dev/null
+++ b/src/nautilus-task-manager.c
@@ -0,0 +1,137 @@
+#include "nautilus-task-manager.h"
+
+#define TASK_LIMIT 1
+
+typedef struct
+{
+    NautilusTask *task;
+
+    NautilusTaskCallback callback;
+    gpointer callback_data;
+} NautilusTaskDetails;
+
+struct _NautilusTaskManager
+{
+    GObject parent_instance;
+
+    GThreadPool *thread_pool;
+};
+
+G_DEFINE_TYPE (NautilusTaskManager, nautilus_task_manager, G_TYPE_OBJECT)
+
+static NautilusTaskManager *instance = NULL;
+
+static GObject *
+constructor (GType                  type,
+             guint                  n_construct_properties,
+             GObjectConstructParam *construct_properties)
+{
+    static GMutex mutex;
+    GObjectClass *parent_class;
+
+    g_mutex_lock (&mutex);
+
+    if (instance != NULL)
+    {
+        g_mutex_unlock (&mutex);
+        return g_object_ref (instance);
+    }
+
+    parent_class = G_OBJECT_CLASS (nautilus_task_manager_parent_class);
+    instance = NAUTILUS_TASK_MANAGER (parent_class->constructor (type,
+                                                                 n_construct_properties,
+                                                                 construct_properties));
+
+    g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *) &instance);
+
+    g_mutex_unlock (&mutex);
+
+    return G_OBJECT (instance);
+}
+
+static void
+finalize (GObject *object)
+{
+    NautilusTaskManager *self;
+
+    self = NAUTILUS_TASK_MANAGER (object);
+
+    g_thread_pool_free (self->thread_pool, TRUE, TRUE);
+}
+
+static void
+nautilus_task_manager_class_init (NautilusTaskManagerClass *klass)
+{
+    GObjectClass *object_class;
+
+    object_class = G_OBJECT_CLASS (klass);
+
+    object_class->constructor = constructor;
+    object_class->finalize = finalize;
+}
+
+static gboolean
+call_callback (gpointer user_data)
+{
+    NautilusTaskDetails *details;
+
+    details = user_data;
+
+    if (details->callback != NULL)
+    {
+        details->callback (details->task, details->callback_data);
+    }
+
+    g_object_unref (details->task);
+    g_free (details);
+
+    return G_SOURCE_REMOVE;
+}
+
+static void
+execute_task (gpointer data,
+              gpointer user_data)
+{
+    NautilusTaskDetails *details;
+
+    details = data;
+
+    nautilus_task_execute (details->task, NULL, NULL);
+
+    g_idle_add (call_callback, details);
+}
+
+static void
+nautilus_task_manager_init (NautilusTaskManager *self)
+{
+    self->thread_pool = g_thread_pool_new (execute_task, self,
+                                           TASK_LIMIT, FALSE,
+                                           NULL);
+}
+
+void
+nautilus_task_manager_queue_task (NautilusTaskManager  *self,
+                                  NautilusTask         *task,
+                                  NautilusTaskCallback  callback,
+                                  gpointer              user_data)
+{
+    NautilusTaskDetails *details;
+
+    g_return_if_fail (NAUTILUS_IS_TASK_MANAGER (self));
+    g_return_if_fail (NAUTILUS_IS_TASK (task));
+
+    details = g_new0 (NautilusTaskDetails, 1);
+
+    details->task = g_object_ref (task);
+    details->callback = callback;
+    details->callback_data = user_data;
+
+    g_thread_pool_push (self->thread_pool, details, NULL);
+}
+
+NautilusTaskManager *
+nautilus_task_manager_dup_singleton (void)
+{
+    return g_object_new (NAUTILUS_TYPE_TASK_MANAGER, NULL);
+}
+
diff --git a/src/nautilus-task-manager.h b/src/nautilus-task-manager.h
new file mode 100644
index 0000000..59fd86a
--- /dev/null
+++ b/src/nautilus-task-manager.h
@@ -0,0 +1,21 @@
+#ifndef NAUTILUS_TASK_MANAGER_H
+#define NAUTILUS_TASK_MANAGER_H
+
+#include "nautilus-task.h"
+
+#include <glib-object.h>
+
+#define NAUTILUS_TYPE_TASK_MANAGER (nautilus_task_manager_get_type ())
+
+G_DECLARE_FINAL_TYPE (NautilusTaskManager, nautilus_task_manager,
+                      NAUTILUS, TASK_MANAGER,
+                      GObject)
+
+void nautilus_task_manager_queue_task (NautilusTaskManager  *self,
+                                       NautilusTask         *task,
+                                       NautilusTaskCallback  callback,
+                                       gpointer              user_data);
+
+NautilusTaskManager *nautilus_task_manager_dup_singleton (void);
+
+#endif


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