Shared data structure to process prefetch requests
[IRC.git] / Robust / src / Runtime / DSTM / interface / queue.c
1 #include "queue.h"
2
3 prefetchthreadqueue_t queue; //Global shared prefetch queue
4
5 void queueInsert(int *array) {
6         pthread_mutex_lock(&queue.qlock);
7         queue.rear = queue.rear % ARRAY_SIZE;
8         if(queue.front == queue.rear && queue.buffer[queue.front] != NULL) {
9                 printf("The Circular Queue is Full : OVERFLOW\n");
10                 pthread_mutex_unlock(&queue.qlock);
11                 return;
12         } else {
13                 queue.buffer[queue.rear] = array;
14                 queue.rear++;
15         }
16         pthread_mutex_unlock(&queue.qlock);
17 }
18
19 int *queueDelete() {
20         int *i; 
21         i = NULL;
22         pthread_mutex_lock(&queue.qlock);
23         if(queue.front == queue.rear && queue.buffer[queue.front] == NULL) {
24                 printf("The Circular Queue is Empty : UNDERFLOW\n");
25                 pthread_mutex_unlock(&queue.qlock);
26                 return NULL;
27         } else {
28                 i = queue.buffer[queue.front];
29                 queue.buffer[queue.front] = NULL;
30                 queue.front++;
31                 queue.front = queue.front % ARRAY_SIZE;
32                 pthread_mutex_unlock(&queue.qlock);
33                 return i;
34         }
35 }
36
37 void queueInit() {
38         int i;
39         queue.front = 0;
40         queue.rear = 0;
41         for(i = 0; i< ARRAY_SIZE; i++) 
42                 queue.buffer[i] = NULL;
43         /* Initialize the pthread_mutex variable */
44         pthread_mutex_init(&queue.qlock, NULL);
45 }
46
47 /* For testing purposes */
48 #if 0
49 main() {
50         int *d; 
51         queueIsEmpty();
52         int a[] = {5, 2, 8, -1};
53         int b[] = {11, 8, 4, 19, -1};
54         int c[] = {16, 8, 4, -1};
55         printf("Front = %d, Rear = %d\n",queue.front, queue.rear);
56         d = queueDelete();
57         printf("Front = %d, Rear = %d\n",queue.front, queue.rear);
58         queueInsert(a);
59         printf("Enqueued ptr is %x\n", a);
60         printf("1st Insert Front = %d, Rear = %d\n",queue.front, queue.rear);
61         queueInsert(b);
62         printf("Enqueued ptr is %x\n", b);
63         printf("2nd Insert Front = %d, Rear = %d\n",queue.front, queue.rear);
64         queueInsert(c);
65         printf("3rd Insert Front = %d, Rear = %d\n",queue.front, queue.rear);
66         d = queueDelete();
67         printf("Dequeued ptr is %x\n", d);
68         printf("After 1st del Front = %d, Rear = %d\n",queue.front, queue.rear);
69         queueInsert(c);
70         printf("Enqueued ptr is %x\n", c);
71         printf("After 4th insert Front = %d, Rear = %d\n",queue.front, queue.rear);
72         d = queueDelete();
73         printf("Dequeued ptr is %x\n", d);
74         printf("After 2nd del Front = %d, Rear = %d\n",queue.front, queue.rear);
75         d = queueDelete();
76         printf("Dequeued ptr is %x\n", d);
77         printf("After 3rd del Front = %d, Rear = %d\n",queue.front, queue.rear);
78         d = queueDelete();
79         printf("Dequeued ptr is %x\n", d);
80         printf("After 4th del Front = %d, Rear = %d\n",queue.front, queue.rear);
81 }
82
83 #endif