-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsbuffer.c
More file actions
118 lines (94 loc) · 3.15 KB
/
Copy pathsbuffer.c
File metadata and controls
118 lines (94 loc) · 3.15 KB
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
/**
* \author Arthur Tavares Quintao
*/
#include "sbuffer.h"
/**
* basic node for the buffer, these nodes are linked together to create the buffer
*/
typedef struct sbuffer_node {
struct sbuffer_node *next; /**< a pointer to the next node*/
sensor_data_t data; /**< a structure containing the data */
} sbuffer_node_t;
/**
* a structure to keep track of the buffer
*/
struct sbuffer {
sbuffer_node_t *head; /**< a pointer to the first node in the buffer */
sbuffer_node_t *tail; /**< a pointer to the last node in the buffer */
};
// Declare global mutex and condition variables
pthread_mutex_t mutex;
pthread_cond_t empty, removing;
int sbuffer_init(sbuffer_t **buffer) {
*buffer = malloc(sizeof(sbuffer_t));
if (*buffer == NULL) return SBUFFER_FAILURE;
(*buffer)->head = NULL;
(*buffer)->tail = NULL;
// Initialize mutex and condition variables
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&empty, NULL);
pthread_cond_init(&removing, NULL);
return SBUFFER_SUCCESS;
}
int sbuffer_free(sbuffer_t **buffer) {
sbuffer_node_t *dummy;
if ((buffer == NULL) || (*buffer == NULL)) {
return SBUFFER_FAILURE;
}
while ((*buffer)->head) {
dummy = (*buffer)->head;
(*buffer)->head = (*buffer)->head->next;
free(dummy);
}
free(*buffer);
*buffer = NULL;
return SBUFFER_SUCCESS;
}
int sbuffer_remove(sbuffer_t *buffer, sensor_data_t *data) {
if (buffer == NULL) {
return SBUFFER_FAILURE;
}
pthread_mutex_lock(&mutex);
// Wait until the buffer is nonempty
while (buffer->head == NULL) {
pthread_cond_wait(&empty, &mutex);
}
// Copy the first item from the buffer
*data = buffer->head->data;
if (data->id == 0) {
// end-of-stream: leave the item in the buffer for other readers to see
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&empty); // Signal that buffer is not empty so other threads can consume this item
return SBUFFER_NO_DATA;
} // else remove the item
sbuffer_node_t *dummy = buffer->head;
buffer->head = buffer->head->next;
if (buffer->head == NULL) {
// buffer is now empty
buffer->tail = NULL;
}
pthread_mutex_unlock(&mutex);
free(dummy);
return SBUFFER_SUCCESS;
}
int sbuffer_insert(sbuffer_t *buffer, sensor_data_t *data) {
sbuffer_node_t *dummy;
if (buffer == NULL) return SBUFFER_FAILURE;
dummy = malloc(sizeof(sbuffer_node_t));
if (dummy == NULL) return SBUFFER_FAILURE;
dummy->data = *data;
dummy->next = NULL;
pthread_mutex_lock(&mutex);
if (buffer->tail == NULL) // buffer empty (buffer->head should also be NULL
{
assert(buffer->head == NULL);
buffer->head = buffer->tail = dummy;
pthread_cond_broadcast(&empty); // Signal that buffer is not empty
} else // buffer not empty
{
buffer->tail->next = dummy;
buffer->tail = buffer->tail->next;
}
pthread_mutex_unlock(&mutex);
return SBUFFER_SUCCESS;
}