From e530cc9ffec3bf41b08eead12335265f2073e477 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Wed, 18 Mar 2015 01:14:57 +0000 Subject: [PATCH] Fix initialization so that preferences aren't accessed too early. Use a two stage pass, since several initialization functions now read preferences as part of their process. This is due to the first time a_preferences_get() is called it loads any preferences values from disk, but of course for preferences not registered yet it can't actually understand them. Thus subsequent initial attempts to get unregistered preferences would not return the expected values. --- src/babel.c | 14 +++++++++++--- src/babel.h | 1 + src/background.c | 20 ++++++++++++++++---- src/background.h | 1 + src/main.c | 19 +++++++++++++++++++ src/modules.c | 18 ++++++++++++++++++ src/modules.h | 2 +- src/preferences.c | 4 ++++ src/vikmapniklayer.c | 10 +++++++++- src/vikmapniklayer.h | 1 + 10 files changed, 81 insertions(+), 9 deletions(-) diff --git a/src/babel.c b/src/babel.c index ea8a62ff..ff65bce5 100644 --- a/src/babel.c +++ b/src/babel.c @@ -678,9 +678,7 @@ static VikLayerParam prefs[] = { /** * a_babel_init: * - * Initialises babel module. - * Mainly check existence of gpsbabel progam - * and load all features available in that version. + * Just setup preferences first */ void a_babel_init () { @@ -697,7 +695,17 @@ void a_babel_init () vlpd.s = "gpsbabel"; #endif a_preferences_register(&prefs[0], vlpd, VIKING_PREFERENCES_IO_GROUP_KEY); +} +/** + * a_babel_post_init: + * + * Initialises babel module. + * Mainly check existence of gpsbabel progam + * and load all features available in that version. + */ +void a_babel_post_init () +{ // Read the current preference const gchar *gpsbabel = a_preferences_get(VIKING_PREFERENCES_IO_NAMESPACE "gpsbabel")->s; // If setting is still the UNIX default then lookup in the path - otherwise attempt to use the specified value directly. diff --git a/src/babel.h b/src/babel.h index 9138afa8..baf2cd44 100644 --- a/src/babel.h +++ b/src/babel.h @@ -108,6 +108,7 @@ gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input, gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *file, BabelStatusFunc cb, gpointer user_data ); void a_babel_init (); +void a_babel_post_init (); void a_babel_uninit (); gboolean a_babel_available (); diff --git a/src/background.c b/src/background.c index 76dab8f3..c49e517f 100644 --- a/src/background.c +++ b/src/background.c @@ -260,9 +260,24 @@ static VikLayerParam prefs_mapnik[] = { /** * a_background_init: * + * Just setup any preferences. + */ +void a_background_init () +{ +#ifdef HAVE_LIBMAPNIK + VikLayerParamData tmp; + // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues + tmp.u = 1; // Default to 1 thread due to potential crashing issues + a_preferences_register(&prefs_mapnik[0], tmp, "mapnik"); +#endif +} + +/** + * a_background_post_init: + * * Initialize background feature. */ -void a_background_init() +void a_background_post_init() { // initialize thread pools gint max_threads = 10; /* limit maximum number of threads running at one time */ @@ -282,10 +297,7 @@ void a_background_init() thread_pool_local = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL ); #ifdef HAVE_LIBMAPNIK - VikLayerParamData tmp; // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues - tmp.u = 1; // Default to 1 thread due to potential crashing issues - a_preferences_register(&prefs_mapnik[0], tmp, "mapnik"); guint mapnik_threads = a_preferences_get("mapnik.background_max_threads_local_mapnik")->u; thread_pool_local_mapnik = g_thread_pool_new ( (GFunc) thread_helper, NULL, mapnik_threads, FALSE, NULL ); #endif diff --git a/src/background.h b/src/background.h index aaa11ff9..5608ee19 100644 --- a/src/background.h +++ b/src/background.h @@ -46,6 +46,7 @@ int a_background_thread_progress ( gpointer callbackdata, gdouble fraction ); int a_background_testcancel ( gpointer callbackdata ); void a_background_show_window (); void a_background_init (); +void a_background_post_init (); void a_background_uninit (); void a_background_add_window (VikWindow *vw); void a_background_remove_window (VikWindow *vw); diff --git a/src/main.c b/src/main.c index 84376f81..46cf5674 100644 --- a/src/main.c +++ b/src/main.c @@ -179,6 +179,15 @@ int main( int argc, char *argv[] ) a_settings_init (); a_preferences_init (); + /* + * First stage initialization + * + * Should not use a_preferences_get() yet + * + * Since the first time a_preferences_get() is called it loads any preferences values from disk, + * but of course for preferences not registered yet it can't actually understand them + * so subsequent initial attempts to get those preferences return the default value, until the values have changed + */ a_vik_preferences_init (); a_layer_defaults_init (); @@ -199,6 +208,16 @@ int main( int argc, char *argv[] ) a_toolbar_init(); vik_routing_prefs_init(); + /* + * Second stage initialization + * + * Can now use a_preferences_get() + */ + a_background_post_init (); + a_babel_post_init (); + modules_post_init (); + + // May need to initialize the Positonal TimeZone lookup if ( a_vik_get_time_ref_frame() == VIK_TIME_REF_WORLD ) vu_setup_lat_lon_tz_lookup(); diff --git a/src/modules.c b/src/modules.c index 9ab5d40c..21325245 100644 --- a/src/modules.c +++ b/src/modules.c @@ -228,6 +228,11 @@ register_loadable_types(void) g_debug("%d types loaded", (int)sizeof(types)/(int)sizeof(GType)); } +/** + * First stage initialization + * Can not use a_get_preferences() yet... + * See comment in main.c + */ void modules_init() { #ifdef VIK_CONFIG_BING @@ -266,6 +271,19 @@ void modules_init() modules_load_config (); } +/** + * modules_post_init: + * + * Secondary stage initialization + * Can now use a_get_preferences() + */ +void modules_post_init () +{ +#ifdef HAVE_LIBMAPNIK + vik_mapnik_layer_post_init(); +#endif +} + /** * */ diff --git a/src/modules.h b/src/modules.h index 5f39bd00..3672f190 100644 --- a/src/modules.h +++ b/src/modules.h @@ -23,7 +23,7 @@ #define __VIKING_MODULES_H void modules_init(); - +void modules_post_init (); void modules_uninit(); #endif diff --git a/src/preferences.c b/src/preferences.c index 4cf76ad8..975f54d3 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -204,6 +204,9 @@ void a_preferences_show_window(GtkWindow *parent) { void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key ) { + // All preferences should be registered before loading + if ( loaded ) + g_critical ( "REGISTERING preference %s after LOADING from " VIKING_PREFS_FILE, pref->name ); /* copy value */ VikLayerParam *newpref = g_new(VikLayerParam,1); *newpref = *pref; @@ -242,6 +245,7 @@ void a_preferences_uninit() VikLayerParamData *a_preferences_get(const gchar *key) { if ( ! loaded ) { + g_debug ( "%s: First time: %s\n", __FUNCTION__, key ); /* since we can't load the file in a_preferences_init (no params registered yet), * do it once before we get the first key. */ preferences_load_from_file(); diff --git a/src/vikmapniklayer.c b/src/vikmapniklayer.c index ca84ecd2..dc64985d 100644 --- a/src/vikmapniklayer.c +++ b/src/vikmapniklayer.c @@ -272,7 +272,7 @@ static GHashTable *requests = NULL; /** * vik_mapnik_layer_init: * - * Mostly to initialize preferences + * Just initialize preferences */ void vik_mapnik_layer_init (void) { @@ -293,7 +293,15 @@ void vik_mapnik_layer_init (void) tmp.s = "carto"; a_preferences_register(&prefs[i++], tmp, MAPNIK_PREFS_GROUP_KEY); +} +/** + * vik_mapnik_layer_post_init: + * + * Initialize data structures - now that reading preferences is OK to perform + */ +void vik_mapnik_layer_post_init (void) +{ tp_mutex = vik_mutex_new(); // Just storing keys only diff --git a/src/vikmapniklayer.h b/src/vikmapniklayer.h index daf88ab1..bb4690d1 100644 --- a/src/vikmapniklayer.h +++ b/src/vikmapniklayer.h @@ -39,6 +39,7 @@ GType vik_mapnik_layer_get_type (); typedef struct _VikMapnikLayer VikMapnikLayer; void vik_mapnik_layer_init (void); +void vik_mapnik_layer_post_init (void); void vik_mapnik_layer_uninit (void); G_END_DECLS -- 2.39.5