]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
epgdb: some simplifications and corrections to the epg periodic save
authorAdam Sutton <dev@adamsutton.me.uk>
Fri, 8 Mar 2013 10:41:39 +0000 (10:41 +0000)
committerAdam Sutton <dev@adamsutton.me.uk>
Fri, 8 Mar 2013 10:45:00 +0000 (10:45 +0000)
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
src/epgdb.c
src/epggrab.c
src/main.c
src/webui/extjs.c
src/webui/simpleui.c

index bff1a5c292222e1725b184b7ff14e5383cc6502e..f8724e7b7a990b3f21efb3a1816104db6751c7f8 100644 (file)
--- 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);
 
 /* ************************************************************************
index bb61fbc1fc66735fa7397958e9dbc1523e6e9246..ebc1513b96287ce17ea853974c51e9836506fa9a 100644 (file)
@@ -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);
 
index 18b97e916500a890d1be0ef607a35c58b8ccf71b..8d110376e9f29fb6e182cd136f4220104fa3d5cd 100644 (file)
@@ -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;
index 15d0100ac057725935cd3cac6473174973037542..7ac61715fce583cb3384184f793ade3948e51594 100644 (file)
@@ -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");
 
index 076739142120046fdbb07416ccd12c06b326d01c..6e793562ae50d9dcaaecfa01f26d4f646ef786f6 100644 (file)
@@ -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")) )
index 36dcda10392eee8b7714fc161e1e3c08fa2250c6..30f8769911d692e51067bde20ebc08a4d52eff4f 100644 (file)
@@ -483,7 +483,9 @@ page_epgsave(http_connection_t *hc,
   htsbuf_qprintf(hq, "<?xml version=\"1.0\"?>\n"
                  "<epgflush>1</epgflush>\n");
 
-  epg_save();
+  pthread_mutex_lock(&global_lock);
+  epg_save(NULL);
+  pthread_mutex_unlock(&global_lock);
 
   http_output_content(hc, "text/xml");