// //Finds the first occurrence of a queue element based on the predicate and //removes it from the queue // QueueElementData Queue_RemoveElement(PQueue pQueue, void * predicate, DoesMatch pDoesMatchFunc) { QueueElementData data = NULL; PQueueElement currItem = NULL; if(!pQueue) { return NULL; } LockQueue(pQueue); if (NULL == pQueue->pHead) { UnLockQueue(pQueue); return NULL; } currItem = pQueue->pHead; while (currItem != NULL) { if (pDoesMatchFunc(currItem->data, predicate) == TRUE) { data = Queue_RemoveElementInternal(pQueue, currItem); break; } currItem = currItem->pNextItem; } UnLockQueue(pQueue); return data; }
QueueElementData Queue_Dequeue(PQueue pQueue) { QueueElement *qElm; QueueElementData data; if(!pQueue) return NULL; LockQueue(pQueue); if (NULL == pQueue->pHead) { UnLockQueue(pQueue); return NULL; } qElm = pQueue->pHead; pQueue->pHead = qElm->pNextItem; if (NULL == qElm->pNextItem)//there is only one element { pQueue->pTail = NULL; } data = qElm->data; free(qElm); qElm = NULL; pQueue->dwCount--; UnLockQueue(pQueue); return data; }
void TransferCaptaincyUpdate(void) { udword numPackets; udword sizeofPacket; ubyte *packet; ubyte *copypacket; if ((transferCaptaincyDisabled) || (sigsNumPlayers < 2)) { return; } TimeoutTimerUpdateAll(); LockQueue(&ProcessCaptaincyPktQ); numPackets = queueNumberEntries(ProcessCaptaincyPktQ); while (numPackets > 0) { sizeofPacket = Dequeue(&ProcessCaptaincyPktQ,&packet); dbgAssert(sizeofPacket > 0); copypacket = memAlloc(sizeofPacket,"cp(miscpacket)",Pyrophoric); memcpy(copypacket,packet,sizeofPacket); UnLockQueue(&ProcessCaptaincyPktQ); dbgAssert(((HWPacketHeader *)copypacket)->type == PACKETTYPE_TRANSFERCAPTAINCY); ProcessTransferCaptaincyPacket((TransferCaptaincyPacket *)copypacket); memFree(copypacket); LockQueue(&ProcessCaptaincyPktQ); numPackets = queueNumberEntries(ProcessCaptaincyPktQ); } UnLockQueue(&ProcessCaptaincyPktQ); #if 0 if (numPackets == 0) { UnLockQueue(&ProcessCaptaincyPktQ); } else { sizeofPacket = Dequeue(&ProcessCaptaincyPktQ,&packet); dbgAssert(sizeofPacket > 0); copypacket = memAlloc(sizeofPacket,"cp(miscpacket)",Pyrophoric); memcpy(copypacket,packet,sizeofPacket); UnLockQueue(&ProcessCaptaincyPktQ); dbgAssert(((HWPacketHeader *)copypacket)->type == PACKETTYPE_TRANSFERCAPTAINCY); ProcessTransferCaptaincyPacket((TransferCaptaincyPacket *)copypacket); memFree(copypacket); } #endif }
// //Removes a specified element from the queue // static QueueElementData Queue_RemoveElementInternal(PQueue pQueue, PQueueElement pQueueElem) { QueueElementData data; PQueueElement prevItem = pQueueElem->pPrevItem; PQueueElement nextItem = pQueueElem->pNextItem; if(!pQueue) { return NULL; } LockQueue(pQueue); if (NULL == pQueue->pHead) { UnLockQueue(pQueue); return NULL; } //Connect prevItem to nextItem if (prevItem != NULL) { prevItem->pNextItem = nextItem; } else { //pQueueElem is the queue head pQueue->pHead = nextItem; } //Connect nextItem to prevItem if (nextItem != NULL) { nextItem->pPrevItem = prevItem; } else { //pQueueElem is the queue tail pQueue->pTail = prevItem; } //Extract data and return... data = pQueueElem->data; free(pQueueElem); pQueueElem = NULL; pQueue->dwCount--; UnLockQueue(pQueue); return data; }
extern Bool EnQueue( Queue *que, void *data) { QueueElement *el; Bool rc; ENTER_FUNC; if ( ( que != NULL ) && ( LockQueue(que) == 0 ) ) { el = New(QueueElement); el->data = data; el->prev = que->tail; el->next = NULL; if ( que->tail == NULL ) { que->head = el; } else { que->tail->next = el; } que->tail = el; que->length++; UnLockQueue(que); ReleaseQueue(que); rc = TRUE; } else { rc = FALSE; } LEAVE_FUNC; return (rc); }
extern void CloseQueue( Queue *que) { que->curr = NULL; UnLockQueue(que); }
extern void * DeQueueTime( Queue *que, int ms) { QueueElement *el; void *ret; ENTER_FUNC; if ( ( que != NULL ) && ( LockQueue(que) == 0 ) ) { WaitQueueTime(que,ms); if ( ( el = que->head ) != NULL ) { if ( el->next == NULL ) { que->tail = NULL; } else { el->next->prev = NULL; } que->head = el->next; ret = el->data; xfree(el); que->length--; } else { ret = NULL; } UnLockQueue(que); } else { ret = NULL; } LEAVE_FUNC; return (ret); }
extern void * DeQueueNoWait( Queue *que) { QueueElement *el; void *ret; ENTER_FUNC; if ( ( que != NULL ) && ( que->head != NULL ) && ( LockQueue(que) == 0 )) { el = que->head; if ( el->next == NULL ) { que->tail = NULL; } else { el->next->prev = NULL; } que->head = el->next; ret = el->data; xfree(el); UnLockQueue(que); } else { ret = NULL; } LEAVE_FUNC; return (ret); }
BOOL Queue_Enqueue(PQueue pQueue, QueueElementData item) { QueueElement *qElm; if(!pQueue) return FALSE; //Create new queue element and add it to the tail. qElm = (QueueElement*)malloc( sizeof(QueueElement) ); if (NULL == qElm) { return FALSE; } qElm->data = item; qElm->pNextItem = NULL; LockQueue(pQueue); qElm->pPrevItem = pQueue->pTail; if (NULL == pQueue->pTail)//this is the first element { pQueue->pTail = pQueue->pHead = qElm; } else { pQueue->pTail->pNextItem = qElm; pQueue->pTail = qElm; } pQueue->dwCount++; UnLockQueue(pQueue); return TRUE; }
int Queue_Count(PQueue pQueue) { UINT32 count; if(!pQueue) return 0; LockQueue(pQueue); count = pQueue->dwCount; UnLockQueue(pQueue); return count; }