From 963fe79290248f177b449964ccfc8f19489c8d96 Mon Sep 17 00:00:00 2001 From: Manuel Pitz Date: Sat, 6 Oct 2018 15:15:21 +0200 Subject: [PATCH] uldaq : fix race condition between daq and read from buffer --- lib/nodes/uldaq.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/nodes/uldaq.c b/lib/nodes/uldaq.c index af62127c6..f3dba1edf 100644 --- a/lib/nodes/uldaq.c +++ b/lib/nodes/uldaq.c @@ -456,21 +456,20 @@ int uldaq_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *re { struct uldaq *u = (struct uldaq *) n->_vd; - /* Wait for data available condition triggered by event callback */ - pthread_mutex_lock(&u->in.mutex); - pthread_cond_wait(&u->in.cv, &u->in.mutex); - if (u->in.status != SS_RUNNING) return -1; if (cnt != n->in.vectorize) return -1; - //long long start_index = u->in.transfer_status.currentIndex - (n->in.vectorize-1) * u->in.channel_count; + /* Wait for data available condition triggered by event callback */ + pthread_mutex_lock(&u->in.mutex); + + long long current_index = u->in.transfer_status.currentIndex + u->in.channel_count; long long start_index = u->buffer_pos; - if(start_index < 0){ - start_index += u->in.buffer_len; - } + + if (start_index + n->in.vectorize * u->in.channel_count > current_index) + pthread_cond_wait(&u->in.cv, &u->in.mutex); #if 0 debug(2, "total count = %lld", u->in.transfer_status.currentTotalCount);