Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
remove usleep/nanosleep calls - use own functions
  • Loading branch information
perexg committed Mar 5, 2016
1 parent f4c42ab commit 19eb890
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 52 deletions.
5 changes: 5 additions & 0 deletions src/clock.h
Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion src/config.c
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/htsp_server.c
Expand Up @@ -3093,7 +3093,7 @@ htsp_read_loop(htsp_connection_t *htsp)

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);
Expand Down
2 changes: 1 addition & 1 deletion src/input/mpegts/iptv/iptv_http.c
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/input/mpegts/linuxdvb/linuxdvb_adapter.c
Expand Up @@ -315,15 +315,15 @@ 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;

/* 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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/input/mpegts/linuxdvb/linuxdvb_en50494.c
Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -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",
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/input/mpegts/linuxdvb/linuxdvb_frontend.c
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/input/mpegts/linuxdvb/linuxdvb_rotor.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/input/mpegts/linuxdvb/linuxdvb_satconf.c
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/input/mpegts/linuxdvb/linuxdvb_switch.c
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down
28 changes: 9 additions & 19 deletions src/input/mpegts/tsfile/tsfile_input.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/notify.c
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/satip/rtp.c
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/tcp.c
Expand Up @@ -548,7 +548,7 @@ tcp_connection_launch
return NULL;
}
pthread_mutex_unlock(&global_lock);
usleep(250000);
tvh_safe_usleep(250000);
pthread_mutex_lock(&global_lock);
if (tvheadend_running)
goto try_again;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/udp.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/upnp.c
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/webui/comet.c
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/webui/webui.c
Expand Up @@ -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;
}
Expand Down

0 comments on commit 19eb890

Please sign in to comment.