summaryrefslogtreecommitdiff
path: root/broccoli
diff options
context:
space:
mode:
authorRobert Gustafsson <robg@student.chalmers.se>2017-10-14 10:29:44 +0200
committerRobert Gustafsson <robg@student.chalmers.se>2017-10-14 10:29:44 +0200
commit89d12d836a0e34d7d2c55a1f96d947de48933813 (patch)
tree791502a89410c96732c793acb9fe2274a61cad2d /broccoli
parent9dcd19548b57e80726d53ec4f9253f1809a2d04a (diff)
downloadmidbro-89d12d836a0e34d7d2c55a1f96d947de48933813.tar.gz
midbro-89d12d836a0e34d7d2c55a1f96d947de48933813.tar.bz2
Add define to use policy drop first element
Diffstat (limited to 'broccoli')
-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);