#include "queue.h"
-prefetchthreadqueue_t queue; //Global shared prefetch queue
+volatile int headoffset, tailoffset;
+char * memory;
+pthread_mutex_t qlock;
+pthread_mutexattr_t qlockattr;
+pthread_cond_t qcond;
-void queueInsert(int *array) {
- pthread_mutex_lock(&queue.qlock);
- queue.rear = queue.rear % ARRAY_SIZE;
- if(queue.front == queue.rear && queue.buffer[queue.front] != NULL) {
- printf("The Circular Queue is Full : OVERFLOW\n");
- pthread_mutex_unlock(&queue.qlock);
- return;
- } else {
- queue.buffer[queue.rear] = array;
- queue.rear++;
- }
- pthread_mutex_unlock(&queue.qlock);
+
+#define QSIZE 1000000 //1 MB
+
+void queueInit(void) {
+ /* Intitialize primary queue */
+ headoffset=0;
+ tailoffset=0;
+ memory=malloc(QSIZE);
+ pthread_mutexattr_init(&qlockattr);
+ pthread_mutexattr_settype(&qlockattr, PTHREAD_MUTEX_RECURSIVE_NP);
+ pthread_mutex_init(&qlock, &qlockattr);
+ pthread_cond_init(&qcond, NULL);
+}
+
+void * getmemory(int size) {
+ int tmpoffset=headoffset+size+sizeof(int);
+ if (tmpoffset>QSIZE) {
+ //Wait for tail to go past end
+ tmpoffset=size+sizeof(int);
+ while(headoffset<tailoffset)
+ ;
+ //Wait for tail to go past new start
+ while(tailoffset<tmpoffset)
+ ;
+ *((int *)(memory+headoffset))=-1;
+ *((int*)memory)=size+sizeof(int);
+ return memory+sizeof(int);
+ } else {
+ while(headoffset<tailoffset&&tailoffset<tmpoffset)
+ ;
+ *((int*)(memory+headoffset))=size+sizeof(int);
+ return memory+headoffset+sizeof(int);
+ }
}
-int *queueDelete() {
- int *i;
- i = NULL;
- pthread_mutex_lock(&queue.qlock);
- if(queue.front == queue.rear && queue.buffer[queue.front] == NULL) {
- printf("The Circular Queue is Empty : UNDERFLOW\n");
- pthread_mutex_unlock(&queue.qlock);
- return NULL;
- } else {
- i = queue.buffer[queue.front];
- queue.buffer[queue.front] = NULL;
- queue.front++;
- queue.front = queue.front % ARRAY_SIZE;
- pthread_mutex_unlock(&queue.qlock);
- return i;
- }
+void movehead(int size) {
+ int tmpoffset=headoffset+size+sizeof(int);
+ if (tmpoffset>QSIZE) {
+ headoffset=size+sizeof(int);
+ } else
+ headoffset=tmpoffset;
+ pthread_cond_signal(&qcond);//wake the other thread up
}
-void queueInit() {
- int i;
- queue.front = 0;
- queue.rear = 0;
- for(i = 0; i< ARRAY_SIZE; i++)
- queue.buffer[i] = NULL;
- /* Initialize the pthread_mutex variable */
- pthread_mutex_init(&queue.qlock, NULL);
+void * gettail() {
+ while(tailoffset==headoffset) {
+ //Sleep
+ pthread_mutex_lock(&qlock);
+ if (tailoffset==headoffset)
+ pthread_cond_wait(&qcond, &qlock);
+ pthread_mutex_unlock(&qlock);
+ }
+ if (*((int *)(memory+tailoffset))==-1) {
+ tailoffset=0;//do loop
+ }
+
+ return memory+tailoffset+sizeof(int);
+}
+
+void inctail() {
+ int tmpoffset=tailoffset+*((int *)(memory+tailoffset));
+ if (tmpoffset>QSIZE)
+ tailoffset=0;
+ else
+ tailoffset=tmpoffset;
}
-/* For testing purposes */
-#if 0
-main() {
- int *d;
- queueIsEmpty();
- int a[] = {5, 2, 8, -1};
- int b[] = {11, 8, 4, 19, -1};
- int c[] = {16, 8, 4, -1};
- printf("Front = %d, Rear = %d\n",queue.front, queue.rear);
- d = queueDelete();
- printf("Front = %d, Rear = %d\n",queue.front, queue.rear);
- queueInsert(a);
- printf("Enqueued ptr is %x\n", a);
- printf("1st Insert Front = %d, Rear = %d\n",queue.front, queue.rear);
- queueInsert(b);
- printf("Enqueued ptr is %x\n", b);
- printf("2nd Insert Front = %d, Rear = %d\n",queue.front, queue.rear);
- queueInsert(c);
- printf("3rd Insert Front = %d, Rear = %d\n",queue.front, queue.rear);
- d = queueDelete();
- printf("Dequeued ptr is %x\n", d);
- printf("After 1st del Front = %d, Rear = %d\n",queue.front, queue.rear);
- queueInsert(c);
- printf("Enqueued ptr is %x\n", c);
- printf("After 4th insert Front = %d, Rear = %d\n",queue.front, queue.rear);
- d = queueDelete();
- printf("Dequeued ptr is %x\n", d);
- printf("After 2nd del Front = %d, Rear = %d\n",queue.front, queue.rear);
- d = queueDelete();
- printf("Dequeued ptr is %x\n", d);
- printf("After 3rd del Front = %d, Rear = %d\n",queue.front, queue.rear);
- d = queueDelete();
- printf("Dequeued ptr is %x\n", d);
- printf("After 4th del Front = %d, Rear = %d\n",queue.front, queue.rear);
+
+void predealloc() {
+ free(memory);
}
-#endif