[gnome-builder] threadpool: sort workitem by priority



commit e5718dbb00a28794269a85d9c9f13cf394f2fa63
Author: Christian Hergert <chergert redhat com>
Date:   Sun Mar 25 18:25:44 2018 -0700

    threadpool: sort workitem by priority
    
    This allows sorting work items by their priority, and ensures we specify
    that when pushing GTask onto the work queue.

 src/libide/threading/ide-thread-pool.c | 33 +++++++++++++++++++++++++++++++++
 src/libide/threading/ide-thread-pool.h | 17 +++++++++++------
 2 files changed, 44 insertions(+), 6 deletions(-)
---
diff --git a/src/libide/threading/ide-thread-pool.c b/src/libide/threading/ide-thread-pool.c
index c7f5e6f47..2d8d4d683 100644
--- a/src/libide/threading/ide-thread-pool.c
+++ b/src/libide/threading/ide-thread-pool.c
@@ -29,6 +29,7 @@
 typedef struct
 {
   int type;
+  int priority;
   union {
     struct {
       GTask           *task;
@@ -105,6 +106,7 @@ ide_thread_pool_push_task (IdeThreadPoolKind  kind,
 
       work_item = g_slice_new0 (WorkItem);
       work_item->type = TYPE_TASK;
+      work_item->priority = g_task_get_priority (task);
       work_item->task.task = g_object_ref (task);
       work_item->task.func = func;
 
@@ -132,6 +134,24 @@ void
 ide_thread_pool_push (IdeThreadPoolKind kind,
                       IdeThreadFunc     func,
                       gpointer          func_data)
+{
+  ide_thread_pool_push_with_priority (kind, G_PRIORITY_DEFAULT, func, func_data);
+}
+
+/**
+ * ide_thread_pool_push_with_priority:
+ * @kind: the threadpool kind to use.
+ * @priority: the priority for func
+ * @func: (scope async) (closure func_data): A function to call in the worker thread.
+ * @func_data: user data for @func.
+ *
+ * Runs the callback on the thread pool thread.
+ */
+void
+ide_thread_pool_push_with_priority (IdeThreadPoolKind kind,
+                                    gint              priority,
+                                    IdeThreadFunc     func,
+                                    gpointer          func_data)
 {
   GThreadPool *pool;
 
@@ -151,6 +171,7 @@ ide_thread_pool_push (IdeThreadPoolKind kind,
 
       work_item = g_slice_new0 (WorkItem);
       work_item->type = TYPE_FUNC;
+      work_item->priority = priority;
       work_item->func.callback = func;
       work_item->func.data = func_data;
 
@@ -195,6 +216,17 @@ ide_thread_pool_worker (gpointer data,
   g_slice_free (WorkItem, work_item);
 }
 
+static gint
+thread_pool_sort_func (gconstpointer a,
+                       gconstpointer b,
+                       gpointer      user_data)
+{
+  const WorkItem *a_item = a;
+  const WorkItem *b_item = b;
+
+  return a_item->priority - b_item->priority;
+}
+
 void
 _ide_thread_pool_init (gboolean is_worker)
 {
@@ -214,6 +246,7 @@ _ide_thread_pool_init (gboolean is_worker)
                                        is_worker ? p->worker_max_threads : p->max_threads,
                                        p->exclusive,
                                        &error);
+          g_thread_pool_set_sort_function (p->pool, thread_pool_sort_func, NULL);
 
           if (error != NULL)
             g_error ("Failed to initialize thread pool %u: %s",
diff --git a/src/libide/threading/ide-thread-pool.h b/src/libide/threading/ide-thread-pool.h
index 312f19a84..c2eb93e6e 100644
--- a/src/libide/threading/ide-thread-pool.h
+++ b/src/libide/threading/ide-thread-pool.h
@@ -43,12 +43,17 @@ typedef enum
 typedef void (*IdeThreadFunc) (gpointer user_data);
 
 IDE_AVAILABLE_IN_ALL
-void     ide_thread_pool_push      (IdeThreadPoolKind     kind,
-                                    IdeThreadFunc         func,
-                                    gpointer              func_data);
+void     ide_thread_pool_push               (IdeThreadPoolKind  kind,
+                                             IdeThreadFunc      func,
+                                             gpointer           func_data);
+IDE_AVAILABLE_IN_3_30
+void     ide_thread_pool_push_with_priority (IdeThreadPoolKind  kind,
+                                             gint               priority,
+                                             IdeThreadFunc      func,
+                                             gpointer           func_data);
 IDE_AVAILABLE_IN_ALL
-void     ide_thread_pool_push_task (IdeThreadPoolKind     kind,
-                                    GTask                *task,
-                                    GTaskThreadFunc       func);
+void     ide_thread_pool_push_task          (IdeThreadPoolKind  kind,
+                                             GTask             *task,
+                                             GTaskThreadFunc    func);
 
 G_END_DECLS


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