]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
remove usleep/nanosleep calls - use own functions
authorJaroslav Kysela <perex@perex.cz>
Sat, 5 Mar 2016 16:01:33 +0000 (17:01 +0100)
committerJaroslav Kysela <perex@perex.cz>
Sat, 5 Mar 2016 16:01:33 +0000 (17:01 +0100)
19 files changed:
src/clock.h
src/config.c
src/htsp_server.c
src/input/mpegts/iptv/iptv_http.c
src/input/mpegts/linuxdvb/linuxdvb_adapter.c
src/input/mpegts/linuxdvb/linuxdvb_en50494.c
src/input/mpegts/linuxdvb/linuxdvb_frontend.c
src/input/mpegts/linuxdvb/linuxdvb_rotor.c
src/input/mpegts/linuxdvb/linuxdvb_satconf.c
src/input/mpegts/linuxdvb/linuxdvb_switch.c
src/input/mpegts/tsfile/tsfile_input.c
src/notify.c
src/satip/rtp.c
src/tcp.c
src/udp.c
src/upnp.c
src/webui/comet.c
src/webui/webui.c
src/wrappers.c

index 79d8db0966297393325e0450a4d85cb306c7ae21..c2a65d0c4ab01730529c4d12fff8c50d99693226 100644 (file)
@@ -105,4 +105,9 @@ static inline time_t time_t_out_of_range(uint64_t val)
   return r;
 }
 
+void tvh_safe_usleep(int64_t us);
+
+int64_t tvh_usleep(int64_t us);
+int64_t tvh_usleep_abs(int64_t us);
+
 #endif /* TVHEADEND_CLOCK_H */
index 8e6ff9d21578f46a088315d2f19e06a2b1d5cb93..a81060b46bf51e866cbace0355c3d5b77c76a28b 100644 (file)
@@ -1458,7 +1458,7 @@ dobackup(const char *oldver)
     code = -ENOENT;
   } else {
     while ((code = spawn_reap(pid, errtxt, sizeof(errtxt))) == -EAGAIN)
-      usleep(20000);
+      tvh_safe_usleep(20000);
     if (code == -ECHILD)
       code = 0;
     tvhinfo("config", "backup: completed");
index c47b50c2d619c3009079cfbdaebd2f02307e0801..220e9a77f0cc35b1b644347c87774215d8a57630 100644 (file)
@@ -3093,7 +3093,7 @@ readmsg:
 
            pthread_mutex_unlock(&global_lock);
             /* Classic authentication failed delay */
-            usleep(250000);
+            tvh_safe_usleep(250000);
 
             reply = htsmsg_create_map();
             htsmsg_add_u32(reply, "noaccess", 1);
index 71282ad5bae8b561673150a4159eb95c142d4412..2bcf8c9454f6e77c3d5bacf27d20365405d1be61 100644 (file)
@@ -84,7 +84,7 @@ iptv_http_safe_global_lock( http_priv_t *hp )
     if (r == 0)
       break;
     if (r == EBUSY)
-      usleep(10000);
+      tvh_safe_usleep(10000);
   }
   return 1;
 }
index 24e4a31ca1476c30e5c299938befdf055f8d26a0..c64840ef875dd60c343004f1b80ac3e5f40ee3f9 100644 (file)
@@ -315,7 +315,7 @@ linuxdvb_adapter_add ( const char *path )
     if (!i) {
       for (j = 0; j < MAX_DEV_OPEN_ATTEMPTS; j++) {
         if (!access(fe_path, R_OK | W_OK)) break;
-        usleep(100000);
+        tvh_safe_usleep(100000);
       }
     }
     if (access(fe_path, R_OK | W_OK)) continue;
@@ -323,7 +323,7 @@ linuxdvb_adapter_add ( const char *path )
     /* Get frontend info */
     for (j = 0; j < MAX_DEV_OPEN_ATTEMPTS; j++) {
       if ((fd = tvh_open(fe_path, O_RDWR, 0)) >= 0) break;
-      usleep(100000);
+      tvh_safe_usleep(100000);
     }
     if (fd < 0) {
       tvhlog(LOG_ERR, "linuxdvb", "unable to open %s", fe_path);
@@ -413,7 +413,7 @@ linuxdvb_adapter_add ( const char *path )
     /* Get ca info */
     for (j = 0; j < MAX_DEV_OPEN_ATTEMPTS; j++) {
       if ((fd = tvh_open(ca_path, O_RDWR, 0)) >= 0) break;
-      usleep(100000);
+      tvh_safe_usleep(100000);
     }
     if (fd < 0) {
       tvhlog(LOG_ERR, "linuxdvb", "unable to open %s", ca_path);
index e33b2a1381c6e396df1b0f41e8959136dce6a6ac..d3501a4140af3e7a2aa55a0a3010594edafa7b9b 100644 (file)
@@ -223,7 +223,7 @@ linuxdvb_en50494_tune
       tvherror("en50494","failed to lock for tuning");
       return -1;
     }
-    usleep(20000);
+    tvh_safe_usleep(20000);
   }
 
   /* setup en50494 switch */
@@ -234,7 +234,7 @@ linuxdvb_en50494_tune
       uint8_t rnd;
       uuid_random(&rnd, 1);
       int ms = ((int)rnd)%50 + 68;
-      usleep(ms*1000);
+      tvh_safe_usleep(ms*1000);
     }
 
     /* use 18V */
@@ -243,7 +243,7 @@ linuxdvb_en50494_tune
       tvherror("en50494", "error setting lnb voltage to 18V");
       break;
     }
-    usleep(15000); /* standard: 4ms < x < 22ms */
+    tvh_safe_usleep(15000); /* standard: 4ms < x < 22ms */
 
     /* send tune command (with/without pin) */
     tvhdebug("en50494",
@@ -269,7 +269,7 @@ linuxdvb_en50494_tune
       tvherror("en50494", "error send tune command");
       break;
     }
-    usleep(50000); /* standard: 2ms < x < 60ms */
+    tvh_safe_usleep(50000); /* standard: 2ms < x < 60ms */
 
     /* return to 13V */
     ret = linuxdvb_diseqc_set_volt(lsp, 0);
index 1437e9271b6d572fb270b2f81fd58a573fb8bb9a..4fa76389afe2f5263fcbe7d7a7b7e3722ae816a3 100644 (file)
@@ -1756,7 +1756,7 @@ linuxdvb_frontend_tune1
   rep = lfe->lfe_tune_repeats > 0 ? lfe->lfe_tune_repeats : 0;
   for (i = 0; i <= rep; i++) {
     if (i > 0)
-      usleep(15000);
+      tvh_safe_usleep(15000);
     r = linuxdvb_frontend_tune0(lfe, mmi, freq);
     if (r)
       break;
index f4844323d42bb3bdc5669e4be47d150c66eda3ed..c95d41ff34ae3145f6d5c8c8fa9071fa3b62129b 100644 (file)
@@ -376,7 +376,7 @@ linuxdvb_rotor_gotox_tune
       tvherror("diseqc", "failed to set GOTOX pos %d", lr->lr_position);
       return -1;
     }
-    usleep(MINMAX(lr->lr_cmd_time, 10, 100) * 1000);
+    tvh_safe_usleep(MINMAX(lr->lr_cmd_time, 10, 100) * 1000);
   }
 
   tvhdebug("diseqc", "rotor GOTOX pos %d sent", lr->lr_position);
@@ -412,7 +412,7 @@ linuxdvb_rotor_usals_tune
       tvherror("diseqc", "failed to send USALS command");
       return -1;
     }
-    usleep(MINMAX(lr->lr_cmd_time, 10, 100) * 1000);
+    tvh_safe_usleep(MINMAX(lr->lr_cmd_time, 10, 100) * 1000);
   }
 
   return linuxdvb_rotor_grace((linuxdvb_diseqc_t*)lr,lm);
index 53f95503aca630df97f75bba16d74bef56d21e38..0d0b4d84d5fbad07c4f86fd92f7793c437082218 100644 (file)
@@ -758,7 +758,7 @@ linuxdvb_satconf_start ( linuxdvb_satconf_t *ls, int delay, int vol )
   /* the linuxdvb_diseqc_set_volt() fcn already sleeps for 15ms */
   if (delay > 15) {
     tvhtrace("diseqc", "initial sleep %dms", delay);
-    usleep((delay-15)*1000);
+    tvh_safe_usleep((delay-15)*1000);
   }
   return 0;
 }
@@ -852,7 +852,7 @@ linuxdvb_satconf_ele_tune ( linuxdvb_satconf_ele_t *lse )
         return -1;
       }
       ls->ls_last_tone_off = band + 1;
-      usleep(20000); // Allow LNB to settle before tuning
+      tvh_safe_usleep(20000); // Allow LNB to settle before tuning
     }
   }
 
@@ -1576,7 +1576,7 @@ linuxdvb_diseqc_set_volt ( linuxdvb_satconf_t *ls, int vol )
     return -1;
   }
   if (vol >= 0)
-    usleep(15000);
+    tvh_safe_usleep(15000);
   ls->ls_last_vol = vol ? (vol < 0 ? 0 : 2) : 1;
   return 0;
 }
index a2905803e7e51bcd001e7ce27e87e3e2cd71a594..11a368f61aa61d619567ca961aae583b3f6b1a54 100644 (file)
@@ -196,14 +196,14 @@ linuxdvb_switch_tune
           if (linuxdvb_diseqc_send(fd, 0xE0 | r2, 0x10, 0x39, 1,
                                    0xF0 | ls->ls_uncommitted))
             return -1;
-          usleep(slp);
+          tvh_safe_usleep(slp);
         }
 
       /* Committed */
       if (ls->ls_committed >= 0) {
         if (linuxdvb_diseqc_send(fd, 0xE0 | r1, 0x10, 0x38, 1, com))
           return -1;
-        usleep(slp);
+        tvh_safe_usleep(slp);
       }
 
       if (!ls->ls_uncommitted_first) {
@@ -212,7 +212,7 @@ linuxdvb_switch_tune
           if (linuxdvb_diseqc_send(fd, 0xE0 | r2, 0x10, 0x39, 1,
                                    0xF0 | ls->ls_uncommitted))
             return -1;
-          usleep(slp);
+          tvh_safe_usleep(slp);
         }
       }
 
index c2b7c4836e1f8c6b1c6ff3eee6fdf005d7e345fe..34a0078ba160355ae0c31aa3e1f91bb173695508 100644 (file)
@@ -46,9 +46,7 @@ tsfile_input_thread ( void *aux )
   sbuf_t buf;
   mpegts_pcr_t pcr;
   int64_t pcr_last = PTS_UNSET;
-#if PLATFORM_LINUX
-  int64_t pcr_last_realtime = 0;
-#endif
+  int64_t pcr_last_mono = 0;
   tsfile_input_t *mi = aux;
   mpegts_mux_instance_t *mmi;
   tsfile_mux_instance_t *tmi;
@@ -90,6 +88,8 @@ tsfile_input_thread ( void *aux )
   len = 0;
   tvhtrace("tsfile", "adapter %d file size %jd rem %zu",
            mi->mi_instance, (intmax_t)st.st_size, rem);
+
+  pcr_last_mono = getfastmonoclock();
   
   /* Process input */
   while (1) {
@@ -141,8 +141,7 @@ tsfile_input_thread ( void *aux )
       /* Delay */
       if (pcr.pcr_first != PTS_UNSET) {
         if (pcr_last != PTS_UNSET) {
-          struct timespec slp;
-          int64_t delta;
+          int64_t delta, r;
 
           delta = pcr.pcr_first - pcr_last;
 
@@ -152,21 +151,12 @@ tsfile_input_thread ( void *aux )
             delta = 90000;
           delta *= 11;
 
-#if PLATFORM_LINUX
-          delta += pcr_last_realtime;
-          slp.tv_sec  = (delta / 1000000);
-          slp.tv_nsec = (delta % 1000000) * 1000;
-          clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &slp, NULL);
-#else
-          slp.tv_sec  = (delta / 1000000);
-          slp.tv_nsec = (delta % 1000000) * 1000;
-          nanosleep(&slp, NULL);
-#endif
+          do {
+            r = tvh_usleep_abs(pcr_last_mono + delta);
+          } while (ERRNO_AGAIN(r) || r > 0);
         }
-        pcr_last          = pcr.pcr_first;
-#if PLATFORM_LINUX
-        pcr_last_realtime = getfastmonoclock();
-#endif
+        pcr_last      = pcr.pcr_first;
+        pcr_last_mono = getfastmonoclock();
       }
     }
     sched_yield();
index f039f1330933afc215cd5681722eae4cc4c21e14..4c7dd8f18f9bd146fa535d028396aae99368fbd6 100644 (file)
@@ -110,7 +110,7 @@ notify_thread ( void *p )
     htsmsg_destroy(q);
 
     /* Wait */
-    usleep(500000);
+    tvh_safe_usleep(500000);
     pthread_mutex_lock(&notify_mutex);
   }
   pthread_mutex_unlock(&notify_mutex);
index 8d6a0078d00551c8b85f031e07b2b657d0915ad0..34c6847f598ac8e2d34f4f526fba3e20b4df4189 100644 (file)
@@ -861,20 +861,21 @@ static void *
 satip_rtcp_thread(void *aux)
 {
   satip_rtp_session_t *rtp;
-  struct timespec ts;
+  int64_t us;
   uint8_t msg[RTCP_PAYLOAD+1];
   char addrbuf[50];
   int r, len, err;
 
   tvhtrace("satips", "starting rtcp thread");
   while (satip_rtcp_run) {
-    ts.tv_sec  = 0;
-    ts.tv_nsec = 150000000;
+    us = 150000;
     do {
-      r = nanosleep(&ts, &ts);
+      us = tvh_usleep(us);
+      if (us < 0)
+        goto end;
       if (!satip_rtcp_run)
         goto end;
-    } while (r && ts.tv_nsec);
+    } while (us > 0);
     pthread_mutex_lock(&satip_rtp_lock);
     TAILQ_FOREACH(rtp, &satip_rtp_sessions, link) {
       if (rtp->sq == NULL) continue;
index bdb8e3808d147f6910b31f58317072dd671cd98e..a0eb978260a37751a3c9f1e832a243fd9dff89dc 100644 (file)
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -548,7 +548,7 @@ try_again:
         return NULL;
       }
       pthread_mutex_unlock(&global_lock);
-      usleep(250000);
+      tvh_safe_usleep(250000);
       pthread_mutex_lock(&global_lock);
       if (tvheadend_running)
         goto try_again;
@@ -1116,7 +1116,7 @@ tcp_server_done(void)
   while (LIST_FIRST(&tcp_server_active) != NULL) {
     if (t + sec2mono(5) < getfastmonoclock())
       tvhtrace("tcp", "tcp server %p active too long", LIST_FIRST(&tcp_server_active));
-    usleep(20000);
+    tvh_safe_usleep(20000);
   }
 
   pthread_mutex_lock(&global_lock);
index 3f33b3c4f5e42b0c5e25d51c10c43fc7c3fda1f4..7f0a6e12f233c489796bf9a1e37dc01ccce00878 100644 (file)
--- a/src/udp.c
+++ b/src/udp.c
@@ -472,7 +472,7 @@ udp_write( udp_connection_t *uc, const void *buf, size_t len,
                  sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
     if (r < 0) {
       if (ERRNO_AGAIN(errno)) {
-        usleep(100);
+        tvh_safe_usleep(100);
         continue;
       }
       break;
index 7421e56c6e7d129df1e753e681a9c93a33ca76bd..10927e149ca52f7b3598dbf5d30dc7edc34d6834 100644 (file)
@@ -216,7 +216,7 @@ upnp_thread( void *aux )
     pthread_mutex_unlock(&upnp_lock);
     if (data == NULL)
       break;
-    usleep((long)data->delay_ms * 1000);
+    tvh_safe_usleep((long)data->delay_ms * 1000);
     upnp_dump_data(data);
     udp_write_queue(unicast, &data->queue, &data->storage);
     htsbuf_queue_flush(&data->queue);
index 176754dc941e8cd66712d39fa3c06186a12f6850..f0500e49ea7ef4ca87787747b750c7a75665554e 100644 (file)
@@ -243,7 +243,7 @@ comet_mailbox_poll(http_connection_t *hc, const char *remain, void *opaque)
   htsmsg_t *m;
 
   if(!im)
-    usleep(100000); /* Always sleep 0.1 sec to avoid comet storms */
+    tvh_safe_usleep(100000); /* Always sleep 0.1 sec to avoid comet storms */
 
   pthread_mutex_lock(&comet_mutex);
   if (!comet_running) {
index 49b493865be67cf1a8eb4dfdf622ebbefdf054db..e23430f283f2626c66e2c6ff9c520393d1fab099 100644 (file)
@@ -400,7 +400,7 @@ http_stream_run(http_connection_t *hc, profile_chain_t *prch,
           while (getfastmonoclock() < mono) {
             if (tcp_socket_dead(hc->hc_fd))
               break;
-            usleep(50000);
+            tvh_safe_usleep(50000);
           }
           return;
         }
index f08c28e13e0c854068c797dee07707aee6b27fd9..5dfb8836c98fdaf8c88958f526bc59ff63ae97c1 100644 (file)
@@ -87,7 +87,7 @@ tvh_write(int fd, const void *buf, size_t len)
       if (ERRNO_AGAIN(errno)) {
         if (mdispatch_clock > limit)
           break;
-        usleep(100);
+        tvh_safe_usleep(100);
         continue;
       }
       break;
@@ -208,7 +208,7 @@ tvh_mutex_timedlock
     if (getfastmonoclock() >= finish)
       return ETIMEDOUT;
 
-    usleep(10000);
+    tvh_safe_usleep(10000);
   }
 
   return retcode;
@@ -269,6 +269,61 @@ tvh_cond_timedwait
   return pthread_cond_timedwait(&cond->cond, mutex, &ts);
 }
 
+/*
+ * clocks
+ */
+
+void
+tvh_safe_usleep(int64_t us)
+{
+  int64_t r;
+  if (us <= 0)
+    return;
+  do {
+    r = tvh_usleep(us);
+    if (r < 0) {
+      if (ERRNO_AGAIN(r))
+        continue;
+      break;
+    }
+    us = r;
+  } while (r > 0);
+}
+
+int64_t
+tvh_usleep(int64_t us)
+{
+  struct timespec ts;
+  int64_t val;
+  int r;
+  if (us <= 0)
+    return 0;
+  ts.tv_sec = us / 1000000LL;
+  ts.tv_nsec = (us % 1000000LL) * 1000LL;
+  r = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts);
+  val = (ts.tv_sec * 1000000LL) + ((ts.tv_nsec + 500LL) / 1000LL);
+  if (ERRNO_AGAIN(r))
+    return val;
+  return r ? -r : 0;
+}
+
+int64_t
+tvh_usleep_abs(int64_t us)
+{
+  struct timespec ts;
+  int64_t val;
+  int r;
+  if (us <= 0)
+    return 0;
+  ts.tv_sec = us / 1000000LL;
+  ts.tv_nsec = (us % 1000000LL) * 1000LL;
+  r = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, &ts);
+  val = (ts.tv_sec * 1000000LL) + ((ts.tv_nsec + 500LL) / 1000LL);
+  if (ERRNO_AGAIN(r))
+    return val;
+  return r ? -r : 0;
+}
+
 /*
  * qsort
  */