summaryrefslogtreecommitdiff
path: root/broccoli/fifoqueue.c
diff options
context:
space:
mode:
authorRobert Gustafsson <robg@student.chalmers.se>2017-09-28 16:39:28 +0200
committerAndreas Lindhé <andreas@lindhe.io>2017-10-09 12:14:35 +0200
commit11187a331cc132b990785dfe0012054bf686a5d4 (patch)
tree581383d8f757d06645354a32fbd639f99d7519bb /broccoli/fifoqueue.c
parentef50d0a5d1e592bc512f6d6b892dfded6d6d7aaa (diff)
downloadmidbro-11187a331cc132b990785dfe0012054bf686a5d4.tar.gz
midbro-11187a331cc132b990785dfe0012054bf686a5d4.tar.bz2
Add new thread for event handling
Diffstat (limited to 'broccoli/fifoqueue.c')
-rw-r--r--broccoli/fifoqueue.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/broccoli/fifoqueue.c b/broccoli/fifoqueue.c
index 56b1dda..6676484 100644
--- a/broccoli/fifoqueue.c
+++ b/broccoli/fifoqueue.c
@@ -1,4 +1,7 @@
#include "includes/fifoqueue.h"
+#include <pthread.h>
+
+pthread_mutex_t lock;
Fifo_q *
init_queue(int size)
@@ -8,6 +11,11 @@ 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");
+ }
return q;
}
@@ -32,6 +40,8 @@ is_empty(Fifo_q * q)
int
add_to_queue(Fifo_q * q, Sensor_t * sensor)
{
+
+ pthread_mutex_lock(&lock);
/* TODO delete first one if full */
if(q == NULL){
return -1;
@@ -48,12 +58,16 @@ add_to_queue(Fifo_q * q, Sensor_t * sensor)
q->tail->next = new_elem;
q->tail = new_elem;
q->currentSize++;
+ pthread_mutex_unlock(&lock);
+ print_queue(q);
return 1;
}
Sensor_t *
pop_from_queue(Fifo_q * q)
{
+
+ pthread_mutex_lock(&lock);
if(is_empty(q)){
perror("The queue is empty");
exit(-1);
@@ -63,12 +77,21 @@ pop_from_queue(Fifo_q * q)
Sensor_t * sensor = head->sensor;
free(head);
q->currentSize--;
+ pthread_mutex_unlock(&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)
{
+ pthread_mutex_lock(&lock);
Queue_t * current = q->head;
if(current == NULL){
printf("The queue is empty!");
@@ -79,4 +102,5 @@ print_queue(Fifo_q * q)
current->sensor->value, current->sensor->uid);
current = current->next;
}
+ pthread_mutex_unlock(&lock);
}