aboutsummaryrefslogtreecommitdiff
path: root/broccoli/src
diff options
context:
space:
mode:
Diffstat (limited to 'broccoli/src')
-rw-r--r--broccoli/src/fifoqueue.c16
-rw-r--r--broccoli/src/midbropasad.c12
2 files changed, 18 insertions, 10 deletions
diff --git a/broccoli/src/fifoqueue.c b/broccoli/src/fifoqueue.c
index aeeced9..9b4b75e 100644
--- a/broccoli/src/fifoqueue.c
+++ b/broccoli/src/fifoqueue.c
@@ -13,6 +13,7 @@ init_queue(int size)
q->tail = NULL;
q->maxSize = size;
q->currentSize = 0;
+ /*Queue empty from the beginning (block)*/
sem_init(&q->bufferEmptyBlock, 0, 0);
sem_init(&q->lock, 0, 1);
return q;
@@ -40,12 +41,16 @@ is_empty(Fifo_q * q)
add_to_queue(Fifo_q * q, Sensor_t * sensor)
{
- /* TODO delete first one if full */
if(q == NULL){
+ printf("Error: Queue not initialized\n");
return -1;
}
else if(is_full(q)){
- return -1;
+ /* Drop Least Recently or Drop Most Recently */
+ #ifdef DLR
+ pop_from_queue(q);
+ #endif
+ return 0;
}
sem_wait(&q->lock);
Queue_t * new_elem = (Queue_t *) malloc(sizeof(Queue_t));
@@ -75,11 +80,13 @@ pop_from_queue(Fifo_q * q)
sem_wait(&q->lock);
Queue_t * head = q->head;
Sensor_t * sensor = head->sensor;
+ /* If dequeue the last element */
if(q->currentSize == 1){
q->head = NULL;
q->tail = NULL;
+ /* Read current semaphore value */
sem_getvalue(&q->bufferEmptyBlock, &semStat);
- if(semStat == 1)
+ if(semStat == 1) /* Mark buffer as empty if last elem */
sem_wait(&q->bufferEmptyBlock);
}else{
q->head = head->next;
@@ -91,7 +98,8 @@ pop_from_queue(Fifo_q * q)
}
Sensor_t *
-create_sensor_object(int value, int uid){
+create_sensor_object(int value, int uid)
+{
Sensor_t * sensor = (Sensor_t *) malloc(sizeof(Sensor_t));
sensor->value = value;
sensor->uid = uid;
diff --git a/broccoli/src/midbropasad.c b/broccoli/src/midbropasad.c
index cc93e7f..a745091 100644
--- a/broccoli/src/midbropasad.c
+++ b/broccoli/src/midbropasad.c
@@ -6,7 +6,7 @@
#include <broccoli.h>
#endif
-Fifo_q * q;
+Fifo_q * queue;
void
request_n_values(int number, int arrayOfValues[])
@@ -14,7 +14,7 @@ request_n_values(int number, int arrayOfValues[])
int i;
Sensor_t * sensor;
for(i=0; i<number; ++i){
- sensor = pop_from_queue(q);
+ sensor = pop_from_queue(queue);
arrayOfValues[i] = sensor->value;
free(sensor);
}
@@ -25,7 +25,7 @@ request_value()
{
int value;
Sensor_t * sensor;
- sensor = pop_from_queue(q);
+ sensor = pop_from_queue(queue);
value = sensor->value;
free(sensor);
printf("Release 1 sensor data value\n");
@@ -35,11 +35,11 @@ request_value()
void
start_data_capture()
{
-
- q = init_queue(50);
int res;
+ queue = init_queue(500000); /* Initiate queue with fixed size */
pthread_t event_listener;
- res = pthread_create(&event_listener, NULL, bro_event_listener, q);
+ /* Create producer thread that listen for bro events */
+ res = pthread_create(&event_listener, NULL, bro_event_listener, queue);
if(res){
perror("Unable to create thread");
exit(-1);