]> git.street.me.uk Git - andy/viking.git/blobdiff - src/background.c
Split background threads into two pools.
[andy/viking.git] / src / background.c
index b388fb599be6ee8c02fe594906633cbba9f4ad98..80f6596fce77d06ae205c7c0ed1fe2ca17f1129a 100644 (file)
@@ -2,6 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (c) 2015, Rob Norris <rw_norris@hotmail.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
 
 #include "background.h"
 #include "settings.h"
+#include "util.h"
 
-static GThreadPool *thread_pool = NULL;
+static GThreadPool *thread_pool_remote = NULL;
+static GThreadPool *thread_pool_local = NULL;
 static gboolean stop_all_threads = FALSE;
 
 static GtkWidget *bgwindow = NULL;
@@ -127,6 +130,7 @@ static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data )
 
 /**
  * a_background_thread:
+ * @bp:      Which pool this thread should run in
  * @parent:
  * @message:
  * @func: worker function
@@ -137,7 +141,7 @@ static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data )
  *
  * Function to enlist new background function.
  */
-void a_background_thread ( GtkWindow *parent, const gchar *message, vik_thr_func func, gpointer userdata, vik_thr_free_func userdata_free_func, vik_thr_free_func userdata_cancel_cleanup_func, gint number_items )
+void a_background_thread ( Background_Pool_Type bp, GtkWindow *parent, const gchar *message, vik_thr_func func, gpointer userdata, vik_thr_free_func userdata_free_func, vik_thr_free_func userdata_cancel_cleanup_func, gint number_items )
 {
   GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) );
   gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS );
@@ -162,7 +166,10 @@ void a_background_thread ( GtkWindow *parent, const gchar *message, vik_thr_func
                       -1 );
 
   /* run the thread in the background */
-  g_thread_pool_push( thread_pool, args, NULL );
+  if ( bp == BACKGROUND_POOL_REMOTE )
+    g_thread_pool_push( thread_pool_remote, args, NULL );
+  else
+    g_thread_pool_push( thread_pool_local, args, NULL );
 }
 
 /**
@@ -220,6 +227,7 @@ static void bgwindow_response (GtkDialog *dialog, gint arg1 )
 }
 
 #define VIK_SETTINGS_BACKGROUND_MAX_THREADS "background_max_threads"
+#define VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL "background_max_threads_local"
 
 /**
  * a_background_init:
@@ -228,13 +236,22 @@ static void bgwindow_response (GtkDialog *dialog, gint arg1 )
  */
 void a_background_init()
 {
-  /* initialize thread pool */
+  // initialize thread pools
   gint max_threads = 10;  /* limit maximum number of threads running at one time */
   gint maxt;
   if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS, &maxt ) )
     max_threads = maxt;
 
-  thread_pool = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
+  thread_pool_remote = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
+
+  if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL, &maxt ) )
+    max_threads = maxt;
+  else {
+    guint cpus = util_get_number_of_cpus ();
+    max_threads = cpus > 1 ? cpus-1 : 1; // Don't use all available CPUs!
+  }
+
+  thread_pool_local = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
 
   GtkCellRenderer *renderer;
   GtkTreeViewColumn *column;
@@ -288,9 +305,11 @@ void a_background_init()
  */
 void a_background_uninit()
 {
-  /* wait until all running threads stop */
   stop_all_threads = TRUE;
-  g_thread_pool_free ( thread_pool, TRUE, TRUE );
+  // wait until these threads stop
+  g_thread_pool_free ( thread_pool_remote, TRUE, TRUE );
+  // Don't wait for these
+  g_thread_pool_free ( thread_pool_local, TRUE, FALSE );
 
   gtk_list_store_clear ( bgstore );
   g_object_unref ( bgstore );