diff options
author | Robert Gustafsson <robg@student.chalmers.se> | 2017-09-29 15:04:25 +0200 |
---|---|---|
committer | Robert Gustafsson <robg@student.chalmers.se> | 2017-09-29 15:04:25 +0200 |
commit | a1f91d62c35604c8d5498cc15c1b4d98cd712ec7 (patch) | |
tree | cea65b5ac29d6a3a6d4695b71237eb983535075b /broccoli/fifoqueue.c | |
parent | 1e846faaec3f5dd19128068fc52c2df5d7ba712a (diff) | |
download | midbro-a1f91d62c35604c8d5498cc15c1b4d98cd712ec7.tar.gz midbro-a1f91d62c35604c8d5498cc15c1b4d98cd712ec7.tar.bz2 |
Add blocking wait when buffer is empty
Diffstat (limited to 'broccoli/fifoqueue.c')
-rw-r--r-- | broccoli/fifoqueue.c | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/broccoli/fifoqueue.c b/broccoli/fifoqueue.c index 6676484..dfb8464 100644 --- a/broccoli/fifoqueue.c +++ b/broccoli/fifoqueue.c @@ -2,6 +2,7 @@ #include <pthread.h> pthread_mutex_t lock; +pthread_mutex_t bufferEmptyBlock; Fifo_q * init_queue(int size) @@ -11,11 +12,15 @@ init_queue(int size) q->tail = NULL; q->maxSize = size; q->currentSize = 0; - if (pthread_mutex_init(&lock, NULL) != 0) { printf("WARNING: Couldn't initialize lock\n"); } + if (pthread_mutex_init(&bufferEmptyBlock, NULL) != 0) + { + printf("WARNING: Couldn't initialize blocking lock\n"); + } + pthread_mutex_lock(&bufferEmptyBlock); return q; } @@ -52,14 +57,14 @@ add_to_queue(Fifo_q * q, Sensor_t * sensor) Queue_t * new_elem = (Queue_t *) malloc(sizeof(Queue_t *)); new_elem->next = NULL; new_elem->sensor = sensor; - if(is_empty(q)) + if(is_empty(q)){ q->head = new_elem; - else + pthread_mutex_unlock(&bufferEmptyBlock); + }else q->tail->next = new_elem; q->tail = new_elem; q->currentSize++; pthread_mutex_unlock(&lock); - print_queue(q); return 1; } @@ -67,11 +72,11 @@ add_to_queue(Fifo_q * q, Sensor_t * sensor) pop_from_queue(Fifo_q * q) { - pthread_mutex_lock(&lock); if(is_empty(q)){ perror("The queue is empty"); - exit(-1); + pthread_mutex_lock(&bufferEmptyBlock); } + pthread_mutex_lock(&lock); Queue_t * head = q->head; q->head = q->head->next; Sensor_t * sensor = head->sensor; |