bug fixes
[IRC.git] / Robust / src / Runtime / bamboo / pmc_queue.c
index 7fd822aeecc1871bd4666e2d90b95aecb68ddcaf..32abe8fbbb98696419319f391d95c2a02f51b9f2 100644 (file)
@@ -1,58 +1,50 @@
 #include <stdlib.h>
 #include "pmc_queue.h"
 #include "mem.h"
+#include "runtime_arch.h"
 
 void pmc_queueinit(struct pmc_queue *queue) {
-  queue->head=queue->tail=RUNMALLOC(sizeof(struct pmc_queue_segment));
   queue->headindex=queue->tailindex=0;
 }
 
 void * pmc_dequeue(struct pmc_queue *queue) {
   void *value=NULL;
   tmc_spin_mutex_lock(&queue->lock);
-  //do-while loop allows sharing cleanup code
-  do {
+
+  if (queue->tailindex!=queue->headindex) {
+    value=queue->objects[queue->tailindex];
+    queue->tailindex++;
     //check for possible rollover
     if (queue->tailindex==NUM_PMC_QUEUE_OBJECTS) {
-      if (queue->tail!=queue->head) {
-       struct pmc_queue_segment *oldtail=queue->tail;
-       queue->tail=oldtail->next;
-       queue->tailindex=0;
-       RUNFREE(oldtail);
-      } else break;
-    }
-    //now try to decrement
-    if (queue->tailindex!=queue->headindex) {
-      value=queue->tail->objects[queue->tailindex];
-      queue->tailindex++;
+      queue->tailindex=0;
     }
-  } while(false);
+  }
+
   tmc_spin_mutex_unlock(&queue->lock);
   return value;
 }
 
 void pmc_enqueue(struct pmc_queue* queue, void *ptr) {
-  if (queue->headindex<NUM_PMC_QUEUE_OBJECTS) {
-    queue->head->objects[queue->headindex]=ptr;
-    //need fence to prevent reordering
-    __insn_mf();
-    queue->headindex++;
-    return;
-  } else {
-    struct pmc_queue_segment * seg=RUNMALLOC(sizeof(struct pmc_queue_segment));
-    seg->objects[0]=ptr;
-    //simplify everything by grabbing a lock on segment change
-    tmc_spin_mutex_lock(&queue->lock);
-    queue->headindex=1;
-    queue->head->next=seg;
-    queue->head=seg;
-    tmc_spin_mutex_unlock(&queue->lock);
+  unsigned int currindex=queue->headindex;
+  queue->objects[currindex]=ptr;
+  //need fence to prevent reordering
+  __insn_mf();
+
+  currindex++;
+  if (currindex==NUM_PMC_QUEUE_OBJECTS)
+    currindex=0;
+
+  if (currindex==queue->tailindex) {
+    tprintf("queue full event...\n");
+    BAMBOO_EXIT();
   }
+
+  queue->headindex=currindex;
 }
 
 bool pmc_isEmpty(struct pmc_queue *queue) {
   tmc_spin_mutex_lock(&queue->lock);
-  bool status=(queue->head==queue->tail)&&(queue->headindex==queue->tailindex);
+  bool status=(queue->headindex==queue->tailindex);
   tmc_spin_mutex_unlock(&queue->lock);
   return status;
 }