-
Notifications
You must be signed in to change notification settings - Fork 3
/
fifo.c
153 lines (119 loc) · 2.9 KB
/
fifo.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "queue_ts.h"
#include <time.h>
#define DEBUG
#undef DEBUG /* uncomment when you are done! */
#ifdef DEBUG
#define print printf
#else
#define print(...)
#endif
/*****************************************
struct _node {
void *data;
struct _node * next;
};
typedef struct _node node;
struct _end_q {
node * first;
node * last;
};
typedef struct _end_q queue;
****************************************/
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* create and return a new queue
**/
queue * queue_factory()
{
queue * new_queue = malloc(sizeof(queue));
if(new_queue == NULL) {
fprintf(stderr, "Malloc failed creating the que\n");
return NULL;
}
new_queue->first = NULL;
new_queue->last = NULL;
print("Generated the que @ %p\n", new_queue);
return new_queue;
}
void queue_destroy(queue * que)
{
print("Enterd to queue_destroy\n");
if(que == NULL) {
return;
}
print("que is not null, que = %p\n", que);
pthread_mutex_lock(&mutex);
if(que->first == NULL) {
print("que->first == NULL .... \n");
free(que);
pthread_mutex_unlock(&mutex);
return;
}
print("que is there lets try to free it...\n");
node * _node = que->first;
while(_node != NULL) {
// freeing the data coz it's on the heap and no one to free it
// except for this one
print("freeing : %s\n", (char *)_node->data);
free(_node->data);
node *tmp = _node->next;
free(_node);
_node = tmp;
}
free(que);
pthread_mutex_unlock(&mutex);
}
/**
* que is a queue pointer
* data is a heap allocated memory pointer
*/
int enque(queue * que, void * data)
{
node * new_node = malloc(sizeof(node));
if(new_node == NULL) {
fprintf(stderr, "Malloc failed creating a node\n");
return -1;
}
// assumming data is in heap
new_node->data = data;
new_node->next = NULL;
pthread_mutex_lock(&mutex);
if (que->first == NULL) {
// new que
que->first = new_node;
que->last = new_node;
} else {
que->last->next = new_node;
que->last = new_node;
}
pthread_mutex_unlock(&mutex);
return 0;
}
void * deque(queue * que)
{
print("Entered to deque\n");
if (que == NULL) {
print("que is null exiting...\n");
return NULL;
}
pthread_mutex_lock(&mutex);
if (que->first == NULL) {
pthread_mutex_unlock(&mutex);
print("que->first is null exiting...\n");
return NULL;
}
void * data;
node * _node = que->first;
if (que->first == que->last) {
que->first = NULL;
que->last = NULL;
} else {
que->first = _node->next;
}
data = _node->data;
print("Freeing _node@ %p", _node);
free(_node);
pthread_mutex_unlock(&mutex);
print("Exiting deque\n");
return data;
}