-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
57 lines (40 loc) · 907 Bytes
/
queue.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
/*
* CS533 Course Project
* Thread Queue ADT
* queue.c
*
* Feel free to modify this file. Please thoroughly comment on
* any changes you make.
*/
#include "queue.h"
#include <stdlib.h>
extern void * safe_mem(int, void*);
#define malloc(arg) safe_mem(0, ((void*)(arg)))
#define free(arg) safe_mem(1, arg)
void thread_enqueue(struct queue * q, struct thread * t) {
struct queue_node * temp = malloc(sizeof(struct queue_node));
temp->t = t;
temp->next = NULL;
if(!q->head) {
q->head = q->tail = temp;
return;
}
q->tail->next = temp;
q->tail = temp;
}
struct thread * thread_dequeue(struct queue * q) {
if(!q->head) {
return NULL;
}
struct thread * t = q->head->t;
struct queue_node * temp = q->head;
q->head = q->head->next;
free(temp);
if(!q->head) {
q->tail = NULL;
}
return t;
}
int is_empty(struct queue * q) {
return !q->head;
}