]> git.street.me.uk Git - andy/viking.git/blob - src/background.c
Split background threads into two pools.
[andy/viking.git] / src / background.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  * Copyright (c) 2015, Rob Norris <rw_norris@hotmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include <gtk/gtk.h>
24 #include <glib/gi18n.h>
25
26 #include "background.h"
27 #include "settings.h"
28 #include "util.h"
29
30 static GThreadPool *thread_pool_remote = NULL;
31 static GThreadPool *thread_pool_local = NULL;
32 static gboolean stop_all_threads = FALSE;
33
34 static GtkWidget *bgwindow = NULL;
35 static GtkWidget *bgtreeview = NULL;
36 static GtkListStore *bgstore = NULL;
37
38 // Still only actually updating the statusbar though
39 static GSList *windows_to_update = NULL;
40
41 static gint bgitemcount = 0;
42
43 #define VIK_BG_NUM_ARGS 7
44
45 enum
46 {
47   TITLE_COLUMN = 0,
48   PROGRESS_COLUMN,
49   DATA_COLUMN,
50   N_COLUMNS,
51 };
52
53 void a_background_update_status ( VikWindow *vw, gpointer data )
54 {
55   static gchar buf[20];
56   g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount);
57   vik_window_statusbar_update ( vw, buf, VIK_STATUSBAR_ITEMS );
58 }
59
60 static void background_thread_update ()
61 {
62   g_slist_foreach ( windows_to_update, (GFunc) a_background_update_status, NULL );
63 }
64
65 int a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
66 {
67   gpointer *args = (gpointer *) callbackdata;
68   int res = a_background_testcancel ( callbackdata );
69   if (args[5] != NULL) {
70     gdk_threads_enter();
71     gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction*100, -1 );
72     gdk_threads_leave();
73   }
74
75   args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1);
76   bgitemcount--;
77   background_thread_update();
78   return res;
79 }
80
81 static void thread_die ( gpointer args[VIK_BG_NUM_ARGS] )
82 {
83   vik_thr_free_func userdata_free_func = args[3];
84
85   if ( userdata_free_func != NULL )
86     userdata_free_func ( args[2] );
87
88   if ( GPOINTER_TO_INT(args[6]) )
89   {
90     bgitemcount -= GPOINTER_TO_INT(args[6]);
91     background_thread_update ();
92   }
93
94   g_free ( args[5] ); /* free iter */
95   g_free ( args );
96 }
97
98 int a_background_testcancel ( gpointer callbackdata )
99 {
100   gpointer *args = (gpointer *) callbackdata;
101   if ( stop_all_threads ) 
102     return -1;
103   if ( args && args[0] )
104   {
105     vik_thr_free_func cleanup = args[4];
106     if ( cleanup )
107       cleanup ( args[2] );
108     return -1;
109   }
110   return 0;
111 }
112
113 static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data )
114 {
115   /* unpack args */
116   vik_thr_func func = args[1];
117   gpointer userdata = args[2];
118
119   g_debug(__FUNCTION__);
120
121   func ( userdata, args );
122
123   gdk_threads_enter();
124   if ( ! args[0] )
125     gtk_list_store_remove ( bgstore, (GtkTreeIter *) args[5] );
126   gdk_threads_leave();
127
128   thread_die ( args );
129 }
130
131 /**
132  * a_background_thread:
133  * @bp:      Which pool this thread should run in
134  * @parent:
135  * @message:
136  * @func: worker function
137  * @userdata:
138  * @userdata_free_func: free function for userdata
139  * @userdata_cancel_cleanup_func:
140  * @number_items:
141  *
142  * Function to enlist new background function.
143  */
144 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 )
145 {
146   GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) );
147   gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS );
148
149   g_debug(__FUNCTION__);
150
151   args[0] = GINT_TO_POINTER(0);
152   args[1] = func;
153   args[2] = userdata;
154   args[3] = userdata_free_func;
155   args[4] = userdata_cancel_cleanup_func;
156   args[5] = piter;
157   args[6] = GINT_TO_POINTER(number_items);
158
159   bgitemcount += number_items;
160
161   gtk_list_store_append ( bgstore, piter );
162   gtk_list_store_set ( bgstore, piter,
163                        TITLE_COLUMN, message,
164                        PROGRESS_COLUMN, 0.0,
165                        DATA_COLUMN, args,
166                        -1 );
167
168   /* run the thread in the background */
169   if ( bp == BACKGROUND_POOL_REMOTE )
170     g_thread_pool_push( thread_pool_remote, args, NULL );
171   else
172     g_thread_pool_push( thread_pool_local, args, NULL );
173 }
174
175 /**
176  * a_background_show_window:
177  *
178  * Display the background window.
179  */
180 void a_background_show_window ()
181 {
182   gtk_widget_show_all ( bgwindow );
183 }
184
185 static void cancel_job_with_iter ( GtkTreeIter *piter )
186 {
187     gpointer *args;
188
189     g_debug(__FUNCTION__);
190
191     gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 );
192
193     /* we know args still exists because it is free _after_ the list item is destroyed */
194     /* need MUTEX ? */
195     args[0] = GINT_TO_POINTER(1); /* set killswitch */
196
197     gtk_list_store_remove ( bgstore, piter );
198     args[5] = NULL;
199 }
200
201 static void bgwindow_response (GtkDialog *dialog, gint arg1 )
202 {
203   /* note this function is a signal handler called back from the GTK main loop, 
204    * so GDK is already locked.  We need to release the lock before calling 
205    * thread-safe routines
206    */
207   if ( arg1 == 1 ) /* cancel */
208     {
209       GtkTreeIter iter;
210       if ( gtk_tree_selection_get_selected ( gtk_tree_view_get_selection ( GTK_TREE_VIEW(bgtreeview) ), NULL, &iter ) )
211         cancel_job_with_iter ( &iter );
212       gdk_threads_leave();
213       background_thread_update();
214       gdk_threads_enter();
215     }
216   else if ( arg1 == 2 ) /* clear */
217     {
218       GtkTreeIter iter;
219       while ( gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(bgstore), &iter ) )
220         cancel_job_with_iter ( &iter );
221       gdk_threads_leave();
222       background_thread_update();
223       gdk_threads_enter();
224     }
225   else /* OK */
226     gtk_widget_hide ( bgwindow );
227 }
228
229 #define VIK_SETTINGS_BACKGROUND_MAX_THREADS "background_max_threads"
230 #define VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL "background_max_threads_local"
231
232 /**
233  * a_background_init:
234  *
235  * Initialize background feature.
236  */
237 void a_background_init()
238 {
239   // initialize thread pools
240   gint max_threads = 10;  /* limit maximum number of threads running at one time */
241   gint maxt;
242   if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS, &maxt ) )
243     max_threads = maxt;
244
245   thread_pool_remote = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
246
247   if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL, &maxt ) )
248     max_threads = maxt;
249   else {
250     guint cpus = util_get_number_of_cpus ();
251     max_threads = cpus > 1 ? cpus-1 : 1; // Don't use all available CPUs!
252   }
253
254   thread_pool_local = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
255
256   GtkCellRenderer *renderer;
257   GtkTreeViewColumn *column;
258   GtkWidget *scrolled_window;
259
260   g_debug(__FUNCTION__);
261
262   /* store & treeview */
263   bgstore = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_POINTER );
264   bgtreeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(bgstore) );
265   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (bgtreeview), TRUE);
266   gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (bgtreeview)),
267                                GTK_SELECTION_SINGLE);
268
269   /* add columns */
270   renderer = gtk_cell_renderer_text_new ();
271   column = gtk_tree_view_column_new_with_attributes ( _("Job"), renderer, "text", TITLE_COLUMN, NULL );
272   gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
273
274   renderer = gtk_cell_renderer_progress_new ();
275   column = gtk_tree_view_column_new_with_attributes ( _("Progress"), renderer, "value", PROGRESS_COLUMN, NULL );
276   gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
277
278   /* setup window */
279   scrolled_window = gtk_scrolled_window_new ( NULL, NULL );
280   gtk_container_add ( GTK_CONTAINER(scrolled_window), bgtreeview );
281   gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
282
283   bgwindow = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL );
284   gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
285   GtkWidget *response_w = NULL;
286 #if GTK_CHECK_VERSION (2, 20, 0)
287   response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
288 #endif
289   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bgwindow))), scrolled_window, TRUE, TRUE, 0 );
290   gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 );
291   gtk_window_set_title ( GTK_WINDOW(bgwindow), _("Viking Background Jobs") );
292   if ( response_w )
293     gtk_widget_grab_focus ( response_w );
294   /* don't destroy win */
295   g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL );
296
297   g_signal_connect ( G_OBJECT(bgwindow), "response", G_CALLBACK(bgwindow_response), 0 );
298
299 }
300
301 /**
302  * a_background_uninit:
303  *
304  * Uninitialize background feature.
305  */
306 void a_background_uninit()
307 {
308   stop_all_threads = TRUE;
309   // wait until these threads stop
310   g_thread_pool_free ( thread_pool_remote, TRUE, TRUE );
311   // Don't wait for these
312   g_thread_pool_free ( thread_pool_local, TRUE, FALSE );
313
314   gtk_list_store_clear ( bgstore );
315   g_object_unref ( bgstore );
316
317   gtk_widget_destroy ( bgwindow );
318 }
319
320 void a_background_add_window (VikWindow *vw)
321 {
322   windows_to_update = g_slist_prepend(windows_to_update,vw);
323 }
324
325 void a_background_remove_window (VikWindow *vw)
326 {
327   windows_to_update = g_slist_remove(windows_to_update,vw);
328 }