rtl_adsb: fix threading

Signed-off-by: Steve Markgraf <steve@steve-m.de>
This commit is contained in:
Kyle Keen 2014-01-23 00:17:06 -05:00 committed by Steve Markgraf
parent 27c0929939
commit 8520c7c3d7

View File

@ -59,7 +59,8 @@
#define BADSAMPLE 255 #define BADSAMPLE 255
static pthread_t demod_thread; static pthread_t demod_thread;
static pthread_mutex_t data_ready; /* locked when no data available */ static pthread_cond_t ready;
static pthread_mutex_t ready_m;
static volatile int do_exit = 0; static volatile int do_exit = 0;
static rtlsdr_dev_t *dev = NULL; static rtlsdr_dev_t *dev = NULL;
@ -77,6 +78,10 @@ int adsb_frame[14];
#define long_frame 112 #define long_frame 112
#define short_frame 56 #define short_frame 56
/* signals are not threadsafe by default */
#define safe_cond_signal(n, m) pthread_mutex_lock(m); pthread_cond_signal(n); pthread_mutex_unlock(m)
#define safe_cond_wait(n, m) pthread_mutex_lock(m); pthread_cond_wait(n, m); pthread_mutex_unlock(m)
void usage(void) void usage(void)
{ {
fprintf(stderr, fprintf(stderr,
@ -145,11 +150,11 @@ void display(int *frame, int len)
} }
int abs8(int x) int abs8(int x)
/* do not subtract 128 from the raw iq, this handles it */ /* do not subtract 127 from the raw iq, this handles it */
{ {
if (x >= 128) { if (x >= 127) {
return x - 128;} return x - 127;}
return 128 - x; return 127 - x;
} }
void squares_precompute(void) void squares_precompute(void)
@ -338,15 +343,14 @@ static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
if (do_exit) { if (do_exit) {
return;} return;}
memcpy(buffer, buf, len); memcpy(buffer, buf, len);
pthread_mutex_trylock(&data_ready); safe_cond_signal(&ready, &ready_m);
pthread_mutex_unlock(&data_ready);
} }
static void *demod_thread_fn(void *arg) static void *demod_thread_fn(void *arg)
{ {
int len; int len;
while (!do_exit) { while (!do_exit) {
pthread_mutex_lock(&data_ready); safe_cond_wait(&ready, &ready_m);
len = magnitute(buffer, DEFAULT_BUF_LENGTH); len = magnitute(buffer, DEFAULT_BUF_LENGTH);
manchester((uint16_t*)buffer, len); manchester((uint16_t*)buffer, len);
messages((uint16_t*)buffer, len); messages((uint16_t*)buffer, len);
@ -366,7 +370,8 @@ int main(int argc, char **argv)
int dev_index = 0; int dev_index = 0;
int dev_given = 0; int dev_given = 0;
int ppm_error = 0; int ppm_error = 0;
pthread_mutex_init(&data_ready, NULL); pthread_cond_init(&ready, NULL);
pthread_mutex_init(&ready_m, NULL);
squares_precompute(); squares_precompute();
while ((opt = getopt(argc, argv, "d:g:p:e:Q:VS")) != -1) while ((opt = getopt(argc, argv, "d:g:p:e:Q:VS")) != -1)
@ -477,7 +482,8 @@ int main(int argc, char **argv)
else { else {
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);} fprintf(stderr, "\nLibrary error %d, exiting...\n", r);}
rtlsdr_cancel_async(dev); rtlsdr_cancel_async(dev);
pthread_mutex_destroy(&data_ready); pthread_cond_destroy(&ready);
pthread_mutex_destroy(&ready_m);
if (file != stdout) { if (file != stdout) {
fclose(file);} fclose(file);}