-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasicqueue.c
More file actions
71 lines (66 loc) · 1.68 KB
/
basicqueue.c
File metadata and controls
71 lines (66 loc) · 1.68 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
#include "basicqueue.h"
void InitBasicQueue(int size)
{
CIRCLEQ_INIT(&head);
CIRCLEQ_INIT(&rq_head);
pthread_mutex_init(&lock, NULL);
}
void BasicEnqueue(void* task)
{
struct basicentry* n = (struct basicentry*) malloc(sizeof(struct basicentry));
n->elem = task;
pthread_mutex_lock(&lock);
CIRCLEQ_INSERT_TAIL(&head, n, entries);
pthread_mutex_unlock(&lock);
#ifdef VERBOSE
printf("Added %d to the queue\n", ((struct task_desc*)(n->elem))->task_id);
#endif
}
void* BasicDequeue()
{
// Remove a number from the queue
struct basicentry *n;
//Keep doing this until we dequeue an actual number, which also ensures queue is not empty for a successful dequeue.
do
{
pthread_mutex_lock(&lock);
n = CIRCLEQ_FIRST(&head);
CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
pthread_mutex_unlock(&lock);
sched_yield();
}
while(!n->elem);
#ifdef VERBOSE
printf("Removed %d from the queue\n", ((struct task_desc*)(n->elem))->task_id);
#endif
return n->elem;
}
void BasicEnqueue_rq(void* task)
{
struct basicentry* n = (struct basicentry*) malloc(sizeof(struct basicentry));
n->elem = task;
pthread_mutex_lock(&lock);
CIRCLEQ_INSERT_TAIL(&rq_head, n, entries);
pthread_mutex_unlock(&lock);
#ifdef VERBOSE
printf("Added %d to the result queue\n", ((struct task_desc*)(n->elem))->task_id);
#endif
}
void* BasicDequeue_rq()
{
// Remove a number from the queue
struct basicentry *n;
do
{
pthread_mutex_lock(&lock);
n = CIRCLEQ_FIRST(&rq_head);
CIRCLEQ_REMOVE(&rq_head, rq_head.cqh_first, entries);
pthread_mutex_unlock(&lock);
sched_yield();
}
while(!n->elem);
#ifdef VERBOSE
printf("Removed %d from the result queue\n", ((struct task_desc*)(n->elem))->task_id);
#endif
return n->elem;
}