Ejemplo n.º 1
0
void CqsDequeue(Queue q, void **resp)
{
#ifdef ADAPT_SCHED_MEM
    /* Added by Isaac for testing purposes: */
    if((q->length > 1) && (CmiMemoryUsage() > schedAdaptMemThresholdMB*1024*1024) ){
	/* CqsIncreasePriorityForEntryMethod(q, 153); */
	CqsIncreasePriorityForMemCriticalEntries(q); 
    }
#endif
    
  if (q->length==0) 
    { *resp = 0; return; }
  if (q->negprioq.heapnext>1)
    { *resp = CqsPrioqDequeue(&(q->negprioq)); q->length--; return; }
  if (q->zeroprio.head != q->zeroprio.tail)
    { *resp = CqsDeqDequeue(&(q->zeroprio)); q->length--; return; }
  if (q->posprioq.heapnext>1)
    { *resp = CqsPrioqDequeue(&(q->posprioq)); q->length--; return; }
  *resp = 0; return;
}
Ejemplo n.º 2
0
/** Dequeue an entry */
void *CqsPrioqDequeue(_prioq pq)
{
    _prio pri;
    _prioqelt pe, old;
    void *data;
    int heappos, heapnext;
    _prioqelt *heap = pq->heap;
    int left_child;
    _prioqelt temp1_ht_right, temp1_ht_left, temp1_ht_parent;
    _prioqelt *temp1_ht_handle;
    static int cnt_nilesh1=0;

#ifdef FASTQ
    /*  printf("Hi I'm here too!! %d\n",cnt_nilesh1++); */
#endif
    if (pq->heapnext==1) return 0;
    pe = heap[1];
    data = CqsDeqDequeue(&(pe->data));
    if (pe->data.head == pe->data.tail) {
        /* Unlink prio-bucket from hash-table */
#ifndef FASTQ
        _prioqelt next = pe->ht_next;
        _prioqelt *handle = pe->ht_handle;
        if (next) next->ht_handle = handle;
        *handle = next;
        old=pe;
#else
        old=pe;
        prioqelt *handle;
        if(pe->ht_parent)
        {
            if(pe->ht_parent->ht_left==pe) left_child=1;
            else left_child=0;
        }
        else
        {   /* it is the root in the hashtable entry, so its ht_handle should be used by whoever is the new root */
            handle = pe->ht_handle;
        }

        if(!pe->ht_left && !pe->ht_right)
        {
            if(pe->ht_parent) {
                if(left_child) pe->ht_parent->ht_left=NULL;
                else pe->ht_parent->ht_right=NULL;
            }
            else {
                *handle = NULL;
            }
        }
        else if(!pe->ht_right)
        {
            /*if the node does not have a right subtree, its left subtree root is the new child of its parent */
            pe->ht_left->ht_parent=pe->ht_parent;
            if(pe->ht_parent)
            {
                if(left_child) {
                    pe->ht_parent->ht_left = pe->ht_left;
                    pe->ht_left->ht_handle = &(pe->ht_parent->ht_left);
                }
                else {
                    pe->ht_parent->ht_right = pe->ht_left;
                    pe->ht_left->ht_handle = &(pe->ht_parent->ht_right);
                }
            }
            else {
                pe->ht_left->ht_handle = handle;
                *handle = pe->ht_left;
            }
        }
        else if(!pe->ht_left)
        {
            /*if the node does not have a left subtree, its right subtree root is the new child of its parent */
            pe->ht_right->ht_parent=pe->ht_parent;
            /*pe->ht_right->ht_left=pe->ht_left; */
            if(pe->ht_parent)
            {
                if(left_child) {
                    pe->ht_parent->ht_left = pe->ht_right;
                    pe->ht_right->ht_handle = &(pe->ht_parent->ht_left);
                }
                else {
                    pe->ht_parent->ht_right = pe->ht_right;
                    pe->ht_right->ht_handle = &(pe->ht_parent->ht_right);
                }
            }
            else {
                pe->ht_right->ht_handle = handle;
                *handle = pe->ht_right;
            }
        }
        else if(!pe->ht_right->ht_left)
        {
            pe->ht_right->ht_parent=pe->ht_parent;
            if(pe->ht_parent)
            {
                if(left_child) {
                    pe->ht_parent->ht_left = pe->ht_right;
                    pe->ht_right->ht_handle = &(pe->ht_parent->ht_left);
                }
                else {
                    pe->ht_parent->ht_right = pe->ht_right;
                    pe->ht_right->ht_handle = &(pe->ht_parent->ht_right);
                }
            }
            else {
                pe->ht_right->ht_handle = handle;
                *handle = pe->ht_right;
            }
            if(pe->ht_left) {
                pe->ht_right->ht_left = pe->ht_left;
                pe->ht_left->ht_parent = pe->ht_right;
                pe->ht_left->ht_handle = &(pe->ht_right->ht_left);
            }
        }
        else
        {
            /*if it has both subtrees, swap it with its successor */
            for(pe=pe->ht_right; pe; )
            {
                if(pe->ht_left) pe=pe->ht_left;
                else  /*found the sucessor */
                {   /*take care of the connections */
                    if(old->ht_parent)
                    {
                        if(left_child) {
                            old->ht_parent->ht_left = pe;
                            pe->ht_handle = &(old->ht_parent->ht_left);
                        }
                        else {
                            old->ht_parent->ht_right = pe;
                            pe->ht_handle = &(old->ht_parent->ht_right);
                        }
                    }
                    else {
                        pe->ht_handle = handle;
                        *handle = pe;
                    }
                    temp1_ht_right = pe->ht_right;
                    temp1_ht_left = pe->ht_left;
                    temp1_ht_parent = pe->ht_parent;
                    temp1_ht_handle = pe->ht_handle;

                    pe->ht_parent = old->ht_parent;
                    pe->ht_left = old->ht_left;
                    pe->ht_right = old->ht_right;
                    if(pe->ht_left) {
                        pe->ht_left->ht_parent = pe;
                        pe->ht_right->ht_handle = &(pe->ht_right);
                    }
                    if(pe->ht_right) {
                        pe->ht_right->ht_parent = pe;
                        pe->ht_right->ht_handle = &(pe->ht_right);
                    }
                    temp1_ht_parent->ht_left = temp1_ht_right;
                    if(temp1_ht_right) {
                        temp1_ht_right->ht_handle = &(temp1_ht_parent->ht_left);
                        temp1_ht_right->ht_parent = temp1_ht_parent;
                    }
                    break;
                }
            }
        }
#endif
        pq->hash_entry_size--;

        /* Restore the heap */
        heapnext = (--pq->heapnext);
        pe = heap[heapnext];
        pri = &(pe->pri);
        heappos = 1;
        while (1) {
            int childpos1, childpos2, childpos;
            _prioqelt ch1, ch2, child;
            childpos1 = heappos<<1;
            if (childpos1>=heapnext) break;
            childpos2 = childpos1+1;
            if (childpos2>=heapnext)
            {
                childpos=childpos1;
                child=heap[childpos1];
            }
            else {
                ch1 = heap[childpos1];
                ch2 = heap[childpos2];
                if (CqsPrioGT(&(ch1->pri), &(ch2->pri)))
                {
                    childpos=childpos2;
                    child=ch2;
                }
                else {
                    childpos=childpos1;
                    child=ch1;
                }
            }
            if (CqsPrioGT(&(child->pri), pri)) break;
            heap[heappos]=child;
            heappos=childpos;
        }
        heap[heappos]=pe;

        /* Free prio-bucket */
        if (old->data.bgn != old->data.space) CmiFree(old->data.bgn);
        CmiFree(old);
    }
    return data;
}