82fbcc1da9812a08c382a3142810289f5669e00f
[IRC.git] / Robust / src / Runtime / bamboo / pmc_queue.c
1 #include "pmc_queue.h"
2
3 void pmc_queueinit(struct pmc_queue *queue) {
4   queue->head=queue->tail=RUNMALLOC(struct pmc_queue_segment);
5   queue->headindex=queue->tailindex=0;
6 }
7
8 void * pmc_dequeue(struct pmc_queue *queue) {
9   void *value=NULL;
10   tmc_spin_mutex_lock(&queue->lock);
11   //do-while loop allows sharing cleanup code
12   do {
13     //check for possible rollover
14     if (queue->tailindex==NUM_PMC_QUEUE_OBJECTS) {
15       if (queue->tail!=queue->head) {
16         struct pmc_queue_segment *oldtail=queue->tail;
17         queue->tail=oldtail->next;
18         queue->tailindex=0;
19         RUNFREE(oldtail);
20       } else break;
21     }
22     //now try to decrement
23     if (queue->tailindex!=queue->headindex) {
24       value=queue->tail[queue->tailindex];
25       queue->tailindex++;
26     }
27   } while(false);
28   tmc_spin_mutex_unlock(&queue->lock);
29   return status;
30 }
31
32 void pmc_enqueue(struct pmc_queue* queue, void *ptr) {
33   if (queue->headindex<NUM_PMC_QUEUE_OBJECTS) {
34     queue->head->objects[queue->headindex]=ptr;
35     //need fence to prevent reordering
36     __insn_mf();
37     queue->headindex++;
38     return;
39   } else {
40     struct pmc_queue_segment * seg=RUNMALLOC(struct pmc_queue_segment);
41     seg->objects[0]=ptr;
42     //simplify everything by grabbing a lock on segment change
43     tmc_spin_mutex_lock(&queue->lock);
44     queue->headindex=1;
45     queue->head->next=seg;
46     queue->head=seg;
47     tmc_spin_mutex_unlock(&queue->lock);
48   }
49 }
50
51 bool pmc_isEmpty(struct pmc_queue *queue) {
52   tmc_spin_mutex_lock(&queue->lock);
53   bool status=(queue->head==queue->tail)&&(queue->headindex==queue->tailindex);
54   tmc_spin_mutex_unlock(&queue->lock);
55   return status;
56 }