From 595c3b8574ee6577379f0cb6ceb5b119c7146695 Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Fri, 8 Mar 2013 10:41:39 +0000 Subject: [PATCH] epgdb: some simplifications and corrections to the epg periodic save saving is now done from the gtimer system since a global lock is required to be able to edit the EPG data. I've also added missing global lock processing elsewhere. --- src/epg.h | 2 +- src/epgdb.c | 39 +++++---------------------------------- src/epggrab.c | 23 +++++++++++++++++++++-- src/main.c | 6 +++++- src/webui/extjs.c | 4 ++-- src/webui/simpleui.c | 4 +++- 6 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/epg.h b/src/epg.h index bff1a5c2..f8724e7b 100644 --- a/src/epg.h +++ b/src/epg.h @@ -556,7 +556,7 @@ void epg_query(epg_query_result_t *eqr, const char *channel, const char *tag, * ***********************************************************************/ void epg_init (void); -void epg_save (void); +void epg_save (void*); void epg_updated (void); /* ************************************************************************ diff --git a/src/epgdb.c b/src/epgdb.c index bb61fbc1..ebc1513b 100644 --- a/src/epgdb.c +++ b/src/epgdb.c @@ -36,9 +36,6 @@ extern epg_object_tree_t epg_brands; extern epg_object_tree_t epg_seasons; extern epg_object_tree_t epg_episodes; extern epg_object_tree_t epg_serieslinks; -static pthread_cond_t _epgdbsave_cond; -static void* _epgdbsave_thread ( void *p ); -pthread_mutex_t epgdbsave_mutex; /* ************************************************************************** * Load @@ -139,29 +136,6 @@ static void _epgdb_v2_process ( htsmsg_t *m, epggrab_stats_t *stats ) } } -/* - * Thread functions - */ -static void *_epgdbsave_thread ( void *p ) -{ - struct timespec ts; - ts.tv_nsec = 0; - tvhlog(LOG_DEBUG, "epgdb", "epgdbsave setting: %i", epggrab_epgdb_periodicsave); - while (1) { - if (epggrab_epgdb_periodicsave != 0) { - tvhlog(LOG_DEBUG, "epgdb", "epgdbsave setting: %i", - epggrab_epgdb_periodicsave); - epg_save(); - }; - pthread_mutex_lock(&epgdbsave_mutex); - time(&ts.tv_sec); - ts.tv_sec += epggrab_epgdb_periodicsave * 3600; /* User defined in hours */ - pthread_cond_timedwait(&_epgdbsave_cond, &epgdbsave_mutex, &ts); - pthread_mutex_unlock(&epgdbsave_mutex); - }; -return NULL; -} - /* * Load data */ @@ -253,13 +227,6 @@ void epg_init ( void ) /* Close file */ munmap(mem, st.st_size); close(fd); - - /* Create thread */ - pthread_mutex_init(&epgdbsave_mutex, NULL); - pthread_cond_init(&_epgdbsave_cond, NULL); - /* Start thread */ - pthread_t tid; - pthread_create(&tid, NULL, _epgdbsave_thread, NULL); } /* ************************************************************************** @@ -296,13 +263,17 @@ static int _epg_write_sect ( int fd, const char *sect ) return _epg_write(fd, m); } -void epg_save ( void ) +void epg_save ( void *p ) { int fd; epg_object_t *eo; epg_broadcast_t *ebc; channel_t *ch; epggrab_stats_t stats; + extern gtimer_t epggrab_save_timer; + + if (epggrab_epgdb_periodicsave) + gtimer_arm(&epggrab_save_timer, epg_save, NULL, epggrab_epgdb_periodicsave); fd = hts_settings_open_file(1, "epgdb.v%d", EPG_DB_VERSION); diff --git a/src/epggrab.c b/src/epggrab.c index 18b97e91..8d110376 100644 --- a/src/epggrab.c +++ b/src/epggrab.c @@ -38,9 +38,9 @@ #include "service.h" /* Thread protection */ -static int epggrab_confver; +static int epggrab_confver; pthread_mutex_t epggrab_mutex; -static pthread_cond_t epggrab_cond; +static pthread_cond_t epggrab_cond; /* Config */ uint32_t epggrab_interval; @@ -51,6 +51,8 @@ uint32_t epggrab_channel_renumber; uint32_t epggrab_channel_reicon; uint32_t epggrab_epgdb_periodicsave; +gtimer_t epggrab_save_timer; + /* ************************************************************************** * Internal Grab Thread * *************************************************************************/ @@ -140,6 +142,9 @@ static void _epggrab_load ( void ) htsmsg_get_u32(m, "channel_renumber", &epggrab_channel_renumber); htsmsg_get_u32(m, "channel_reicon", &epggrab_channel_reicon); htsmsg_get_u32(m, "epgdb_periodicsave", &epggrab_epgdb_periodicsave); + if (epggrab_epgdb_periodicsave) + gtimer_arm(&epggrab_save_timer, epg_save, NULL, + epggrab_epgdb_periodicsave); if (!htsmsg_get_u32(m, old ? "grab-interval" : "interval", &epggrab_interval)) { if (old) epggrab_interval *= 3600; @@ -310,6 +315,12 @@ int epggrab_set_periodicsave ( uint32_t e ) int save = 0; if ( e != epggrab_epgdb_periodicsave ) { epggrab_epgdb_periodicsave = e; + pthread_mutex_lock(&global_lock); + if (!e) + gtimer_disarm(&epggrab_save_timer); + else + epg_save(NULL); // will arm the timer + pthread_mutex_unlock(&global_lock); save = 1; } return save; @@ -359,6 +370,14 @@ void epggrab_resched ( void ) */ void epggrab_init ( void ) { + /* Defaults */ + epggrab_interval = 0; + epggrab_module = NULL; + epggrab_channel_rename = 0; + epggrab_channel_renumber = 0; + epggrab_channel_reicon = 0; + epggrab_epgdb_periodicsave = 0; + /* Lists */ #if ENABLE_LINUXDVB extern TAILQ_HEAD(, epggrab_ota_mux) ota_mux_all; diff --git a/src/main.c b/src/main.c index 15d0100a..7ac61715 100644 --- a/src/main.c +++ b/src/main.c @@ -686,11 +686,15 @@ main(int argc, char **argv) mainloop(); - epg_save(); + // Note: the locking is obviously a bit redundant, but without + // we need to disable the gtimer_arm call in epg_save() + pthread_mutex_lock(&global_lock); + epg_save(NULL); #if ENABLE_TIMESHIFT timeshift_term(); #endif + pthread_mutex_unlock(&global_lock); tvhlog(LOG_NOTICE, "STOP", "Exiting HTS Tvheadend"); diff --git a/src/webui/extjs.c b/src/webui/extjs.c index 07673914..6e793562 100644 --- a/src/webui/extjs.c +++ b/src/webui/extjs.c @@ -589,7 +589,7 @@ extjs_epggrab(http_connection_t *hc, const char *remain, void *opaque) htsmsg_add_u32(r, "channel_rename", epggrab_channel_rename); htsmsg_add_u32(r, "channel_renumber", epggrab_channel_renumber); htsmsg_add_u32(r, "channel_reicon", epggrab_channel_reicon); - htsmsg_add_u32(r, "epgdb_periodicsave", epggrab_epgdb_periodicsave); + htsmsg_add_u32(r, "epgdb_periodicsave", epggrab_epgdb_periodicsave / 3600); pthread_mutex_unlock(&epggrab_mutex); out = json_single_record(r, "epggrabSettings"); @@ -621,7 +621,7 @@ extjs_epggrab(http_connection_t *hc, const char *remain, void *opaque) str = http_arg_get(&hc->hc_req_args, "channel_reicon"); save |= epggrab_set_channel_reicon(str ? 1 : 0); if ( (str = http_arg_get(&hc->hc_req_args, "epgdb_periodicsave")) ) - save |= epggrab_set_periodicsave(atoi(str)); + save |= epggrab_set_periodicsave(atoi(str) * 3600); if ( (str = http_arg_get(&hc->hc_req_args, "interval")) ) save |= epggrab_set_interval(atoi(str)); if ( (str = http_arg_get(&hc->hc_req_args, "module")) ) diff --git a/src/webui/simpleui.c b/src/webui/simpleui.c index 36dcda10..30f87699 100644 --- a/src/webui/simpleui.c +++ b/src/webui/simpleui.c @@ -483,7 +483,9 @@ page_epgsave(http_connection_t *hc, htsbuf_qprintf(hq, "\n" "1\n"); - epg_save(); + pthread_mutex_lock(&global_lock); + epg_save(NULL); + pthread_mutex_unlock(&global_lock); http_output_content(hc, "text/xml");