aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Gustafsson <robg@student.chalmers.se>2017-10-27 09:17:21 +0200
committerRobert Gustafsson <robg@student.chalmers.se>2017-10-27 09:18:45 +0200
commita8ad8cbeda0e0dd5d29c5d4dc5afeaa9c4fccfdf (patch)
tree25adf2c37d6d0f965bc5302bcb957c8458553303
parentef2139ca2d442979daefa9e84cc9d27ce620d9e2 (diff)
downloadmidbro-a8ad8cbeda0e0dd5d29c5d4dc5afeaa9c4fccfdf.tar.gz
midbro-a8ad8cbeda0e0dd5d29c5d4dc5afeaa9c4fccfdf.tar.bz2
Add semaphore to block producer if buffer is full
-rw-r--r--broccoli/includes/types.h1
-rw-r--r--broccoli/src/fifoqueue.c18
2 files changed, 11 insertions, 8 deletions
diff --git a/broccoli/includes/types.h b/broccoli/includes/types.h
index b23da1a..149415e 100644
--- a/broccoli/includes/types.h
+++ b/broccoli/includes/types.h
@@ -30,6 +30,7 @@ struct fifo_q{
int valuesReceived;
int valuesReleased;
sem_t bufferEmptyBlock;
+ sem_t bufferFullBlock;
sem_t lock;
};
diff --git a/broccoli/src/fifoqueue.c b/broccoli/src/fifoqueue.c
index 3911eb7..a02f55c 100644
--- a/broccoli/src/fifoqueue.c
+++ b/broccoli/src/fifoqueue.c
@@ -19,6 +19,7 @@ init_queue(int size)
q->valuesReleased = 0;
/*Queue empty from the beginning (block)*/
sem_init(&q->bufferEmptyBlock, 0, 0);
+ sem_init(&q->bufferFullBlock, 0, size);
sem_init(&q->lock, 0, 1);
return q;
}
@@ -50,15 +51,16 @@ add_to_queue(Fifo_q * q, Sensor_t * sensor)
free(sensor); //free if not appended
return -1;
}
- else if(is_full(q)){
- /* Drop Least Recently or Drop Most Recently */
- #ifdef DLR
- pop_from_queue(q);
- #endif
+ /* Drop Least Recently or Drop Most Recently */
+ #ifdef DLR
+ if(is_full(q)){
+ pop_from_queue(q);
q->droppedValues++;
- free(sensor); //free if not appended
return 0;
}
+ #else
+ sem_wait(&q->bufferFullBlock);
+ #endif
sem_wait(&q->lock);
Queue_t * new_elem = (Queue_t *) malloc(sizeof(Queue_t));
new_elem->next = NULL;
@@ -94,10 +96,10 @@ pop_from_queue(Fifo_q * q)
}
free(head);
q->currentSize--;
+ sem_post(&q->lock);
#ifndef DLR
- q->valuesReleased++;
+ sem_post(&q->bufferFullBlock);
#endif
- sem_post(&q->lock);
return sensor;
}