]> git.street.me.uk Git - andy/viking.git/blobdiff - src/background.c
Change interpolation to a faster one
[andy/viking.git] / src / background.c
index 624d78bec800ce5bcbf9aee9ef641a106cd23300..d1d0c9b5ff461fc5b02897a58908e38670c896db 100644 (file)
@@ -25,6 +25,9 @@
 #include "vikstatus.h"
 #include "background.h"
 
 #include "vikstatus.h"
 #include "background.h"
 
+static GThreadPool *thread_pool = NULL;
+static gboolean stop_all_threads = FALSE;
+
 static GtkWidget *bgwindow = NULL;
 static GtkWidget *bgtreeview = NULL;
 static GtkListStore *bgstore = NULL;
 static GtkWidget *bgwindow = NULL;
 static GtkWidget *bgtreeview = NULL;
 static GtkListStore *bgstore = NULL;
@@ -55,17 +58,20 @@ static void background_thread_update ()
   g_slist_foreach ( statusbars_to_update, (GFunc) a_background_update_status, buf );
 }
 
   g_slist_foreach ( statusbars_to_update, (GFunc) a_background_update_status, buf );
 }
 
-void a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
+int a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
 {
   gpointer *args = (gpointer *) callbackdata;
 {
   gpointer *args = (gpointer *) callbackdata;
-  a_background_testcancel ( callbackdata );
-  gdk_threads_enter();
-  gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction*100, -1 );
-  gdk_threads_leave();
+  int res = a_background_testcancel ( callbackdata );
+  if (args[5] != NULL) {
+    gdk_threads_enter();
+    gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction*100, -1 );
+    gdk_threads_leave();
+  }
 
   args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1);
   bgitemcount--;
   background_thread_update();
 
   args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1);
   bgitemcount--;
   background_thread_update();
+  return res;
 }
 
 static void thread_die ( gpointer args[6] )
 }
 
 static void thread_die ( gpointer args[6] )
@@ -82,23 +88,23 @@ static void thread_die ( gpointer args[6] )
 
   g_free ( args[5] ); /* free iter */
   g_free ( args );
 
   g_free ( args[5] ); /* free iter */
   g_free ( args );
-
-  g_thread_exit ( NULL );
 }
 
 }
 
-void a_background_testcancel ( gpointer callbackdata )
+int a_background_testcancel ( gpointer callbackdata )
 {
   gpointer *args = (gpointer *) callbackdata;
 {
   gpointer *args = (gpointer *) callbackdata;
-  if ( args[0] )
+  if ( stop_all_threads ) 
+    return -1;
+  if ( args && args[0] )
   {
     vik_thr_free_func cleanup = args[4];
     if ( cleanup )
       cleanup ( args[2] );
   {
     vik_thr_free_func cleanup = args[4];
     if ( cleanup )
       cleanup ( args[2] );
-    thread_die( args );
+    return -1;
   }
   }
+  return 0;
 }
 }
-
-void thread_helper ( gpointer args[6] )
+void thread_helper ( gpointer args[6], gpointer user_data )
 {
   /* unpack args */
   vik_thr_func func = args[1];
 {
   /* unpack args */
   vik_thr_func func = args[1];
@@ -141,7 +147,7 @@ void a_background_thread ( GtkWindow *parent, const gchar *message, vik_thr_func
                       -1 );
 
   /* run the thread in the background */
                       -1 );
 
   /* run the thread in the background */
-  g_thread_create( (GThreadFunc) thread_helper, args, FALSE, NULL );
+  g_thread_pool_push( thread_pool, args, NULL );
 }
 
 void a_background_show_window ()
 }
 
 void a_background_show_window ()
@@ -162,6 +168,7 @@ static void cancel_job_with_iter ( GtkTreeIter *piter )
     args[0] = GINT_TO_POINTER(1); /* set killswitch */
 
     gtk_list_store_remove ( bgstore, piter );
     args[0] = GINT_TO_POINTER(1); /* set killswitch */
 
     gtk_list_store_remove ( bgstore, piter );
+    args[5] = NULL;
 }
 
 static void bgwindow_response (GtkDialog *dialog, gint arg1 )
 }
 
 static void bgwindow_response (GtkDialog *dialog, gint arg1 )
@@ -194,6 +201,11 @@ static void bgwindow_response (GtkDialog *dialog, gint arg1 )
 
 void a_background_init()
 {
 
 void a_background_init()
 {
+  /* initialize thread pool */
+  /* TODO parametrize this via preference and/or command line arg */
+  gint max_threads = 10;  /* limit maximum number of threads running at one time */
+  thread_pool = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
+
   GtkCellRenderer *renderer;
   GtkTreeViewColumn *column;
   GtkWidget *scrolled_window;
   GtkCellRenderer *renderer;
   GtkTreeViewColumn *column;
   GtkWidget *scrolled_window;
@@ -232,6 +244,13 @@ 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 );
+}
+
 void a_background_add_status(VikStatusbar *vs)
 {
   statusbars_to_update = g_slist_prepend(statusbars_to_update,vs);
 void a_background_add_status(VikStatusbar *vs)
 {
   statusbars_to_update = g_slist_prepend(statusbars_to_update,vs);