aboutsummaryrefslogtreecommitdiff
path: root/broccoli/src/fifoqueue.c
blob: 3911eb77f6b87157355f03c9b43ed0767e47ac6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include "types.h"
#include "fifoqueue.h"

    Fifo_q * 
init_queue(int size)
{
    Fifo_q * q = (Fifo_q *) malloc(sizeof(Fifo_q));
    q->head = NULL;
    q->tail = NULL;
    q->maxSize = size;
    q->currentSize = 0;
    q->droppedValues = 0;
    q->largestBufferSize = 0;
    q->valuesReceived = 0;
    q->valuesReleased = 0;
    /*Queue empty from the beginning (block)*/
    sem_init(&q->bufferEmptyBlock, 0, 0);
    sem_init(&q->lock, 0, 1);
    return q;
}

    boolean
is_full(Fifo_q * q)
{
    if(q->currentSize < q->maxSize)
        return false;
    else
        return true;
}  

    boolean
is_empty(Fifo_q * q)
{
    if(q->head==NULL)
        return true;
    else
        return false; 
}

    int
add_to_queue(Fifo_q * q, Sensor_t * sensor)
{

    if(q == NULL){
        printf("Error: Queue not initialized\n");
        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
        q->droppedValues++;
        free(sensor); //free if not appended
        return 0;
    }
    sem_wait(&q->lock);
    Queue_t * new_elem = (Queue_t *) malloc(sizeof(Queue_t));
    new_elem->next = NULL;
    new_elem->sensor = sensor;
    if(is_empty(q)){
        q->head = new_elem;
    }else
        q->tail->next = new_elem;
    q->tail = new_elem;
    q->currentSize++;
    q->valuesReceived++;
    if(q->currentSize > q->largestBufferSize)
        q->largestBufferSize = q->currentSize;
    sem_post(&q->lock);
    sem_post(&q->bufferEmptyBlock);
    return 1;
}

    Sensor_t *
pop_from_queue(Fifo_q * q)
{
    int semStat;
    sem_wait(&q->bufferEmptyBlock);
    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;
    }else{
        q->head = head->next;
    }
    free(head);
    q->currentSize--;
    #ifndef DLR
    q->valuesReleased++;
    #endif
    sem_post(&q->lock);
    return sensor;
} 

    Sensor_t *
create_sensor_object(int value, int uid)
{
    Sensor_t * sensor = (Sensor_t *) malloc(sizeof(Sensor_t));
    sensor->value = value;
    sensor->uid = uid;
    return sensor;
}
    void
print_queue(Fifo_q * q)
{
    sem_wait(&q->lock);
    Queue_t * current = q->head;
    printf("\nContent of the queue with size=%d\n",q->currentSize);
    if(current == NULL){
        printf("The queue is empty!\n");
        sem_post(&q->lock);
        return;
    }
    while(current != NULL){
        printf("sensor value=%d, sensor uid=%d\n",
                current->sensor->value, current->sensor->uid);
        current = current->next;
    }
    sem_post(&q->lock);
}