-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathque.h
More file actions
215 lines (187 loc) · 4.89 KB
/
que.h
File metadata and controls
215 lines (187 loc) · 4.89 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
//#include <malloc.h>
#include <ucontext.h>
// #define init
// #define push_back
// #define push_front
struct mythread_q
{
struct mythread *my_thread;
struct mythread_q *prev, *next;
};
struct mythread //tcb
{
int state;
struct mythread *parent;
long int my_id;
struct mythread_q *children;
int child_count;
int blk_type;
void (*thread_fxn)(void*);
void *args;
struct mythread_q *waitq; //who all are waiting for me
struct mythread_q *wait_forq; //who am i waiting for - defunct
int wait_for_count; //how many people am i waiting for
ucontext_t context;
};
void mythread_q_init(struct mythread_q **head_ptr, struct mythread *my_thread)
{
struct mythread_q *temp_node=NULL, *head=NULL;
#ifdef init
printf("init: here1: (*my_thread).state = %d\n", (*my_thread).state);
#endif
temp_node = (struct mythread_q *)malloc(sizeof(struct mythread_q));
if (temp_node == NULL)
{
printf("Could not allocate thread space\n");
exit(1);
}
temp_node->my_thread = my_thread;
temp_node->prev = temp_node;
temp_node->next = temp_node;
#ifdef init
printf("init: here2\n");
#endif
*head_ptr = temp_node;
}
void mythread_push_back (struct mythread_q **head_ptr, struct mythread *my_thread)
{
struct mythread_q *temp_node=NULL, *head=NULL;
if (*head_ptr == NULL)
{
mythread_q_init(head_ptr, my_thread);
return;
}
#ifdef push_back
printf("push_back: here1- (*my_thread).state = %d\n", (*my_thread).state);
#endif
head = *head_ptr;
temp_node = (struct mythread_q *)malloc(sizeof(struct mythread_q));
#ifdef push_back
printf("push_back: here2\n");
#endif
if (temp_node == NULL)
{
printf("Error allocating memory for push\n");
exit(1);
}
#ifdef push_back
printf("push_back: here3\n");
#endif
temp_node->my_thread = my_thread;
#ifdef push_back
printf("push_back: here3.1: (*my_thread).state = %d \n", (*my_thread).state);
#endif
temp_node->prev = head->prev;
#ifdef push_back1
printf("push_back: here3.2: ((head->prev->my_thread).state) = %d\n", (head->prev->my_thread).state);
#endif
temp_node->next = head;
#ifdef push_back1
printf("push_back: here3.3: ((head->next->my_thread).state) = %d\n", (head->next->my_thread).state);
#endif
temp_node->prev->next = temp_node;
#ifdef push_back
printf("push_back: here3.4\n");
#endif
head->prev = temp_node;
// #ifdef push_back
// printf("push_back: here4\n");
// #endif
}
void mythread_push_front (struct mythread_q **head_ptr, struct mythread *my_thread)
{
struct mythread_q *temp_node=NULL, *head=NULL;
#ifdef push_front
printf("push_front here1: (*my_thread).state = %d\n", (*my_thread).state);
#endif
if (*head_ptr == NULL)
{
mythread_q_init(head_ptr, my_thread);
return;
}
#ifdef push_front
printf("push_front: here2\n");
#endif
head = *head_ptr;
// mythread_push_back(&head->next, my_thread);
mythread_push_back(head_ptr, my_thread);
#ifdef push_front
printf("push_front: here3\n");
#endif
*head_ptr = head->prev;
}
int mythread_pop (struct mythread_q **head_ptr, struct mythread *my_thread)
{
struct mythread_q *temp_node=NULL, *head=NULL, *thread_iter=NULL;
head = *head_ptr;
thread_iter = *head_ptr;
if (thread_iter == NULL)
return (-1);
if (thread_iter->my_thread == NULL)
return (-1);
do
{
if (thread_iter->my_thread == my_thread)
{
temp_node = thread_iter;
temp_node->next->prev = thread_iter->prev;
temp_node->prev->next = thread_iter->next;
if (temp_node == head)
{
if (temp_node->next == temp_node) //last node left
*head_ptr = NULL;
else
*head_ptr = temp_node->next;
}
return (1);
}
thread_iter = thread_iter->next;
if (thread_iter == head) //didn't find the required thread
{
return (-1);
}
} while (thread_iter != head);
return (1);
}
struct mythread * mythread_popfront (struct mythread_q **head_ptr)
{
struct mythread_q *temp_node_ptr=NULL;
temp_node_ptr = *head_ptr;
if (temp_node_ptr->next == temp_node_ptr)
{
*head_ptr = NULL;
}
else
{
(*head_ptr)->prev->next = temp_node_ptr->next;
(*head_ptr)->next->prev = temp_node_ptr->prev;
*head_ptr = temp_node_ptr->next;
}
return (temp_node_ptr->my_thread);
}
void print_q(struct mythread_q *q_ptr)
{
struct mythread_q *start = q_ptr;
struct mythread_q *temp = q_ptr;
if (!q_ptr)
{
printf("Empty Queue\n");
return;
}
printf("Not an empty queue\n");
do
{
/*if (temp == NULL || temp->my_thread == NULL)
{
printf("\n");
return;
}*/
//else
//{
printf(" %d", temp->my_thread->my_id);
//}
temp = temp->next;
} while (temp != start);
printf("\n");
}