From 03e096475cdd7019faec713d8845843152f69c0f Mon Sep 17 00:00:00 2001 From: Oliver Jowett Date: Tue, 17 Feb 2015 21:07:36 +0000 Subject: [PATCH] Wait for at most 100ms for more data from the rx thread, so that we do not starve background tasks if the rx thread blocks (e.g on USB disconnect). Fixes #20. --- dump1090.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/dump1090.c b/dump1090.c index 0789207..be69752 100644 --- a/dump1090.c +++ b/dump1090.c @@ -988,16 +988,27 @@ int main(int argc, char **argv) { struct timespec start_time; if (Modes.iDataReady == 0) { - pthread_cond_wait(&Modes.data_cond,&Modes.data_mutex); // This unlocks Modes.data_mutex, and waits for Modes.data_cond - continue; // Once (Modes.data_cond) occurs, it locks Modes.data_mutex + /* wait for more data. + * we should be getting data every 50-60ms. wait for max 100ms before we give up and do some background work. + * this is fairly aggressive as all our network I/O runs out of the background work! + */ + + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_nsec += 100000000; + normalize_timespec(&ts); + + pthread_cond_timedwait(&Modes.data_cond, &Modes.data_mutex, &ts); // This unlocks Modes.data_mutex, and waits for Modes.data_cond + // Once (Modes.data_cond) occurs, it locks Modes.data_mutex } + // Modes.data_mutex is Locked, and possibly (Modes.iDataReady != 0) + // copy out reader CPU time and reset it add_timespecs(&Modes.reader_cpu_accumulator, &Modes.stats_current.reader_cpu, &Modes.stats_current.reader_cpu); Modes.reader_cpu_accumulator.tv_sec = 0; Modes.reader_cpu_accumulator.tv_nsec = 0; - // Modes.data_mutex is Locked, and (Modes.iDataReady != 0) if (Modes.iDataReady) { // Check we have new data, just in case!! start_cpu_timing(&start_time);