VOID SCALL LogFileFormatV(const CHAR *format, va_list argList) { DWORD errorCode; LOG_ENTRY *entry; size_t remaining; // Preserve system error code errorCode = GetLastError(); ASSERT(format != NULL); // Retrieve a spare entry structure (also checks log status) entry = GetSpareEntry(); if (entry != NULL) { GetLocalTime(&entry->time); StringCchVPrintfExA(entry->message, ELEMENT_COUNT(entry->message), NULL, &remaining, 0, format, argList); entry->length = ELEMENT_COUNT(entry->message) - remaining; // Insert log entry into the write queue QueueInsert(entry); } // Restore system error code SetLastError(errorCode); }
/* after pushing, producer can produce -1 items, and consumer can consume +1 items in comparison to before */ ADTErr SafeQueuePush(SafeQueue* _queue, void* _data) { ADTErr error; if (NULL == _queue) { return ERR_NOT_INITIALIZED; } error = SemDown(&_queue->m_prodSem); if (error != ERR_OK) { return error; } pthread_mutex_lock(&_queue->m_mutex); error = QueueInsert(_queue->m_queue, _data); if (error != ERR_OK) { return error; } pthread_mutex_unlock(&_queue->m_mutex); error = SemUp(&_queue->m_consSem); if (error != ERR_OK) { return error; } return ERR_OK; }
// // Function: Rcv_ProcessResponceSDU // // Description: Process Responce Message Data // // Arguments: // // Name Type Direction Description // ----------- ----------- ----------- ----------------------------------- // NewDataBlock DataRecord * OUT Block of data ( For return status ) // Buff t_uchar * IN Buffer with data to process // Len int IN Length of buffer // // Return Values: // // Name Explanation // ----------- --------------------------------------------------------------- // RetStatus Responce type ( error code ) // int Rcv_ProcessResponceSDU( DataRecord *pModemStatus, t_uchar *Buff, int Len ) { int RetStatus = RM_SUCCESS; QueueNodeDef *pNode = NULL; int InternalID = Buff[LEFT_SDU_TAG]; //Left Byte of ID //Buff is only data block - without [SOH], [INTEGRITY] and [EOT] InternalID = (InternalID<<8) + Buff[RIGHT_SDU_TAG]; if ( (pNode = QueueFindNode( SEND_QUEUE, InternalID )) == NULL ) { //Create node for command that is NOT in my SEND_QUEUE pNode = QueueInsert( SEND_QUEUE, NULL ); //Will be freed in QueueDeleteNode(); pNode->Message.DataBuffer = (t_uchar *)Mem_Allocate( RM_MINI_BLOCK_SIZE ); pNode->Message.DataBuffer[OPERATOR] = 0xFF; //TYPE UNKNOWN }//end if //Main function to process responce RetStatus = Resp_DisplayFormatted( pNode, Buff, Len ); QueueDeleteNode( SEND_QUEUE, pNode ); pModemStatus->extendedStatus = RM_RESPONCE; return( RetStatus ); //Responce Result }//Rcv_ProcessResponceSDU
DWORD QueuePut (QUEUE_OBJECT *q, PVOID msg, DWORD msize, DWORD MaxWait) { DWORD TotalWaitTime = 0; BOOL TimedOut = FALSE; WaitForSingleObject (q->qGuard, INFINITE); if (q->msgArray == NULL) return 1; /* Queue has been destroyed */ while (QueueFull (q) && !TimedOut) { ReleaseMutex (q->qGuard); WaitForSingleObject (q->qNf, CV_TIMEOUT); if (MaxWait != INFINITE) { TotalWaitTime += CV_TIMEOUT; TimedOut = (TotalWaitTime > MaxWait); } WaitForSingleObject (q->qGuard, INFINITE); } /* Put the message in the queue */ if (!TimedOut) QueueInsert (q, msg, msize); /* Signal that the queue is not empty as we've inserted a message */ PulseEvent (q->qNe); ReleaseMutex (q->qGuard); return TimedOut ? WAIT_TIMEOUT : 0; }
VOID SCALL LogFileTraceV(const CHAR *file, const CHAR *func, INT line, const CHAR *format, va_list argList) { #if 0 CHAR location[MAX_PATH]; #endif CHAR *messageEnd; DWORD errorCode; DWORD processId; DWORD threadId; LOG_ENTRY *entry; size_t remaining; // Preserve system error code errorCode = GetLastError(); ASSERT(file != NULL); ASSERT(func != NULL); ASSERT(format != NULL); processId = GetCurrentProcessId(); threadId = GetCurrentThreadId(); // Retrieve a spare entry structure (also checks log status) entry = GetSpareEntry(); if (entry != NULL) { // // Available trace information: // // file - Source file // func - Function name in the source file // line - Line number in the source file // processId - Current process ID // threadId - Current thread ID // GetLocalTime(&entry->time); #if 0 StringCchPrintfA(location, ELEMENT_COUNT(location), "%s:%d", LogFileName(file), line); StringCchPrintfExA(entry->message, ELEMENT_COUNT(entry->message), &messageEnd, &remaining, 0, "%-20s - %-20s - ", location, func); #else StringCchPrintfExA(entry->message, ELEMENT_COUNT(entry->message), &messageEnd, &remaining, 0, "%s - ", func); #endif StringCchVPrintfExA(messageEnd, remaining, NULL, &remaining, 0, format, argList); entry->length = ELEMENT_COUNT(entry->message) - remaining; // Insert log entry into the write queue QueueInsert(entry); } // Restore system error code SetLastError(errorCode); }
//返回TRUE,说明原来是空队列 bool BytesEnqueue(QueueStruct *q, byte *data, U16 length) //进队列 { bool flag = FALSE; U16 i; if(q->Count == 0) flag = TRUE; for(i = 0; i < length; i++) QueueInsert(q, data[i]); return flag; }
int main() { ULONG i, j; Queue Queue; PCACHE_BLOCK *blocks, pblock; blocks = (PCACHE_BLOCK*)malloc(QUEUE_SIZE*sizeof(PCACHE_BLOCK)); assert(blocks); for (i = 0; i < QUEUE_SIZE; i++) { assert(blocks[i] = (PCACHE_BLOCK)malloc(sizeof(CACHE_BLOCK))); blocks[i]->Value = i; } assert(InitQueue(&Queue, QUEUE_SIZE/2)); QueueInsert(&Queue, blocks[0]); QueueInsert(&Queue, blocks[1]); QueueRemove(&Queue); QueueRemove(&Queue); i = 0, j = 0; while (QueueInsert(&Queue, blocks[i++]) == TRUE); while ((pblock = QueueRemove(&Queue)) != NULL) assert(pblock->Value == blocks[j++]->Value); srand((unsigned int)time(NULL)); i = 0, j = 0; while (i < QUEUE_SIZE) { if (rand() & 1) QueueInsert(&Queue, blocks[i++]); else if ((pblock = QueueRemove(&Queue)) != NULL) assert(pblock->Value == blocks[j++]->Value); } while ((pblock = QueueRemove(&Queue)) != NULL) assert(pblock->Value == blocks[j++]->Value); DestroyQueue(&Queue); for (i = 0; i < QUEUE_SIZE; i++) free(blocks[i]); free(blocks); return 0; }
int main() { static int x[QUEUE_SIZE]; static int RP= -1,FP=-1; int data; StackStatus status; char option; char exitFalg = 0; while(exitFalg == 0) { do { printf("\nPlease Select \n 1: Insert \n 2: Delete \n 3: Exit\n"); scanf("%d",&option); }while(!(option>=1 && option<= 3)); switch (option) { case 1: printf("\n Please Enter data\n"); scanf("%d",&data); status = QueueInsert(x,data,&RP); if(status == OK) { QueueDisplay(x,RP,FP); } else { printf("Queue is full"); } break; case 2: status = QueueDelete(x,&data,&RP,&FP); if(status == OK) { printf("Data = %d",data); } else { printf("Queue is Empty"); } break; case 3: exitFalg =1; break; } } return 0; }
// Transfer one byte over UART void UARTTransmit(uint8_t out) { if (!QueueInsert(&UARTTXBuf, out)) { // Handle Queue overflow __disable_interrupt(); for (;;); // This is here to intentionally break the code } // Turn the transmit interrupt back on if(QueueSize(&UARTTXBuf) == 1) { IE2 |= UCA0TXIE; } }
DWORD QueuePut (QUEUE_OBJECT *q, PVOID msg, DWORD mSize, DWORD maxWait) { WaitForSingleObject (q->qGuard, INFINITE); if (q->msgArray == NULL) return 1; /* Queue has been destroyed */ while (QueueFull (q)) { SignalObjectAndWait (q->qGuard, q->qNf, INFINITE, FALSE); WaitForSingleObject (q->qGuard, INFINITE); } /* Put the message in the queue */ QueueInsert (q, msg, mSize); /* Signal that the queue is not empty as we've inserted a message */ PulseEvent (q->qNe); ReleaseMutex (q->qGuard); return 0; }
DWORD QueuePut (QUEUE_OBJECT *q, PVOID msg, DWORD msize, DWORD MaxWait) { AcquireSRWLockExclusive (&q->qGuard); if (q->msgArray == NULL) return 1; /* Queue has been destroyed */ while (QueueFull (q)) { if (!SleepConditionVariableSRW(&q->qNf, &q->qGuard, INFINITE, 0)) ReportError(_T("QueuePut failed. SleepConditionVariableCS."), 1, TRUE); } /* Put the message in the queue */ QueueInsert (q, msg, msize); /* Signal that the queue is not empty as we've inserted a message */ WakeConditionVariable (&q->qNe); ReleaseSRWLockExclusive (&q->qGuard); return 0; }
DWORD QueuePut (QUEUE_OBJECT *q, PVOID msg, DWORD mSize, DWORD maxWait) { if (q->msgArray == NULL) return 1; /* Queue has been destroyed */ WaitForSingleObject (q->qGuard, INFINITE); while (!ShutDownPut && QueueFull (q)) { if (SignalObjectAndWait(q->qGuard, q->qNf, INFINITE, TRUE) == WAIT_IO_COMPLETION && ShutDownPut) { continue; } WaitForSingleObject (q->qGuard, INFINITE); } /* Put the message in the queue */ if (!ShutDownPut) { QueueInsert (q, msg, mSize); /* Signal that the queue is not empty as we've inserted a message */ SetEvent (q->qNe); ReleaseMutex (q->qGuard); } return ShutDownPut ? WAIT_TIMEOUT : 0; }
/////////////////////////////////////////////////////////////////////////////// // ReqMgrGet // // Description: // Retrieves a number of objects, either synchronously or asynchronously. // // Inputs: // pReqMgr - Pointer to a request manager. // pCount - Contains the number of objects to retrieve. // ReqType - Type of get operation. Can be ReqGetSync, ReqGetAsync, or // ReqGetAsyncPartialOk. // pfnCallback - Pointer to a callback function to store for the request. // Context1 - First of two contexts passed to the request callback. // Context2 - Second of two contexts passed to the request callback. // // Outputs: // pCount - Contains the number of objects available. // // Returns: // FSUCCESS - The request callback has been invoked with elements to // satisfy the request. If the request allowed partial // completions, all elements are guaranteed to have been returned. // FPENDING - The request could not complete in it's entirety. If the // request allowed partial completion, some elements may have been // already returned. // FINSUFFICIENT_RESOURCES - There were not enough objects for the request to // succeed. // FINSUFFICIENT_MEMORY - There was not enough memory to process the // request (including queuing the request). // /////////////////////////////////////////////////////////////////////////////// FSTATUS ReqMgrGet( IN REQ_MGR* const pReqMgr, IN OUT uint32* const pCount, IN const REQ_MGR_TYPE ReqType, IN REQ_CALLBACK pfnCallback, IN void* const Context1, IN void* const Context2 ) { uint32 AvailableCount; uint32 Count; REQUEST_OBJECT *pRequest; ASSERT( pReqMgr ); ASSERT( pReqMgr->m_Initialized ); ASSERT( pCount ); ASSERT( *pCount ); // Get the number of available objects in the grow pool. AvailableCount = pReqMgr->m_pfnGetCount( pReqMgr->m_GetContext ); // Check to see if there is nothing on the queue, and there are // enough items to satisfy the whole request. if( !QueueCount( &pReqMgr->m_RequestQueue ) && *pCount <= AvailableCount ) return( FSUCCESS ); if( ReqType == ReqGetSync ) return( FINSUFFICIENT_RESOURCES ); // We need a request object to place on the request queue. pRequest = (REQUEST_OBJECT*)GrowPoolGet( &pReqMgr->m_RequestPool ); if( !pRequest ) return( FINSUFFICIENT_MEMORY ); // We can return the available number of objects but we still need // to queue a request for the remainder. if( ReqType == ReqGetAsyncPartialOk && !QueueCount( &pReqMgr->m_RequestQueue ) ) { Count = *pCount - AvailableCount; *pCount = AvailableCount; pRequest->PartialOk = TRUE; } else { // We cannot return any objects. We queue a request for all of them. Count = *pCount; *pCount = 0; pRequest->PartialOk = FALSE; } // Set the request fields and enqueue it. pRequest->pfnCallback = pfnCallback; pRequest->Context1 = Context1; pRequest->Context2 = Context2; pRequest->Count = Count; if( !QueueInsert( &pReqMgr->m_RequestQueue, pRequest ) ) { // We could not queue the request. Return the request to the pool. GrowPoolPut( &pReqMgr->m_RequestPool, pRequest ); return( FINSUFFICIENT_MEMORY ); } return( FPENDING ); }