[gnome-builder/wip/chergert/tasks] threading: add new threadpool kinds



commit 7393bd66741b950f577f2ecce250f2eb346c156b
Author: Christian Hergert <chergert redhat com>
Date:   Mon Mar 12 15:39:37 2018 -0700

    threading: add new threadpool kinds
    
    This will allow us to start using IdeThreadPool for task queuing from our
    new IdeTask.

 src/libide/application/ide-application.c  |  1 +
 src/libide/threading/ide-thread-pool.c    | 66 +++++++++++++++----------------
 src/libide/threading/ide-thread-pool.h    |  3 +-
 src/libide/threading/ide-thread-private.h | 27 +++++++++++++
 4 files changed, 62 insertions(+), 35 deletions(-)
---
diff --git a/src/libide/application/ide-application.c b/src/libide/application/ide-application.c
index cf94bcf02..0d0716e3e 100644
--- a/src/libide/application/ide-application.c
+++ b/src/libide/application/ide-application.c
@@ -38,6 +38,7 @@
 #include "application/ide-application-tool.h"
 #include "modelines/modeline-parser.h"
 #include "threading/ide-thread-pool.h"
+#include "threading/ide-thread-private.h"
 #include "util/ide-battery-monitor.h"
 #include "util/ide-flatpak.h"
 #include "util/ide-posix.h"
diff --git a/src/libide/threading/ide-thread-pool.c b/src/libide/threading/ide-thread-pool.c
index e98503712..651ab2d4b 100644
--- a/src/libide/threading/ide-thread-pool.c
+++ b/src/libide/threading/ide-thread-pool.c
@@ -24,9 +24,6 @@
 
 #include "threading/ide-thread-pool.h"
 
-#define COMPILER_MAX_THREADS (g_get_num_processors())
-#define INDEXER_MAX_THREADS  (MAX (1, g_get_num_processors() / 2))
-
 typedef struct
 {
   int type;
@@ -42,10 +39,25 @@ typedef struct
   };
 } WorkItem;
 
+struct _IdeThreadPool
+{
+  GThreadPool       *pool;
+  IdeThreadPoolKind  kind;
+  guint              max_threads;
+  guint              worker_max_threads;
+  gboolean           exclusive;
+};
+
 DZL_DEFINE_COUNTER (TotalTasks, "ThreadPool", "Total Tasks", "Total number of tasks processed.")
 DZL_DEFINE_COUNTER (QueuedTasks, "ThreadPool", "Queued Tasks", "Current number of pending tasks.")
 
-static GThreadPool *thread_pools [IDE_THREAD_POOL_LAST];
+static IdeThreadPool thread_pools[] = {
+  { NULL, IDE_THREAD_POOL_DEFAULT, 10, 1, FALSE },
+  { NULL, IDE_THREAD_POOL_COMPILER, 2, 1, FALSE },
+  { NULL, IDE_THREAD_POOL_INDEXER,  2, 1, FALSE },
+  { NULL, IDE_THREAD_POOL_IO,       8, 1, FALSE },
+  { NULL, IDE_THREAD_POOL_LAST,     0, 0, FALSE }
+};
 
 enum {
   TYPE_TASK,
@@ -55,7 +67,7 @@ enum {
 static inline GThreadPool *
 ide_thread_pool_get_pool (IdeThreadPoolKind kind)
 {
-  return thread_pools [kind];
+  return thread_pools [kind].pool;
 }
 
 /**
@@ -184,35 +196,21 @@ ide_thread_pool_worker (gpointer data,
 void
 _ide_thread_pool_init (gboolean is_worker)
 {
-  gint compiler = COMPILER_MAX_THREADS;
-  gint indexer = INDEXER_MAX_THREADS;
-  gboolean exclusive = FALSE;
-
-  if (is_worker)
+  for (IdeThreadPoolKind kind = IDE_THREAD_POOL_DEFAULT;
+       kind < IDE_THREAD_POOL_LAST;
+       kind++)
     {
-      compiler = 1;
-      indexer = 1;
-      exclusive = TRUE;
+      IdeThreadPool *p = &thread_pools[kind];
+      g_autoptr(GError) error = NULL;
+
+      p->pool = g_thread_pool_new (ide_thread_pool_worker,
+                                   NULL,
+                                   is_worker ? p->worker_max_threads : p->max_threads,
+                                   p->exclusive,
+                                   &error);
+
+      if (error != NULL)
+        g_error ("Failed to initialize thread pool %u: %s",
+                 p->kind, error->message);
     }
-
-  /*
-   * Create our thread pool exclusive to compiler tasks (such as those from Clang).
-   * We don't want to consume threads from other GTask's such as those regarding IO so we manage
-   * these work items exclusively.
-   */
-  thread_pools [IDE_THREAD_POOL_COMPILER] = g_thread_pool_new (ide_thread_pool_worker,
-                                                               NULL,
-                                                               compiler,
-                                                               exclusive,
-                                                               NULL);
-
-  /*
-   * Create our pool exclusive to things like indexing. Such examples including building of
-   * ctags indexes or highlight indexes.
-   */
-  thread_pools [IDE_THREAD_POOL_INDEXER] = g_thread_pool_new (ide_thread_pool_worker,
-                                                              NULL,
-                                                              indexer,
-                                                              exclusive,
-                                                              NULL);
 }
diff --git a/src/libide/threading/ide-thread-pool.h b/src/libide/threading/ide-thread-pool.h
index 92fac3f63..93682f90a 100644
--- a/src/libide/threading/ide-thread-pool.h
+++ b/src/libide/threading/ide-thread-pool.h
@@ -28,8 +28,10 @@ typedef struct _IdeThreadPool IdeThreadPool;
 
 typedef enum
 {
+  IDE_THREAD_POOL_DEFAULT,
   IDE_THREAD_POOL_COMPILER,
   IDE_THREAD_POOL_INDEXER,
+  IDE_THREAD_POOL_IO,
   IDE_THREAD_POOL_LAST
 } IdeThreadPoolKind;
 
@@ -40,7 +42,6 @@ typedef enum
  */
 typedef void (*IdeThreadFunc) (gpointer user_data);
 
-void     _ide_thread_pool_init     (gboolean              is_worker) G_GNUC_INTERNAL;
 IDE_AVAILABLE_IN_ALL
 void     ide_thread_pool_push      (IdeThreadPoolKind     kind,
                                     IdeThreadFunc         func,
diff --git a/src/libide/threading/ide-thread-private.h b/src/libide/threading/ide-thread-private.h
new file mode 100644
index 000000000..a149a432c
--- /dev/null
+++ b/src/libide/threading/ide-thread-private.h
@@ -0,0 +1,27 @@
+/* ide-thread-private.h
+ *
+ * Copyright 2018 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void _ide_thread_pool_init (gboolean is_worker);
+
+G_END_DECLS


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