SyncPointMapEntry AcquireSyncPoint ( int signum, ClientData clientData, int *isnewPtr) { SignalMapEntry *entryPtr; SyncPoint *spointPtr; entryPtr = CreateSigMapEntry(&syncpoints, signum, isnewPtr); if (*isnewPtr) { spointPtr = AllocSyncPoint(signum, clientData); SetSigMapValue(entryPtr, spointPtr); } else { Tcl_ThreadId thisThreadId; spointPtr = GetSigMapValue(entryPtr); thisThreadId = Tcl_GetCurrentThread(); if (spointPtr->signaled == 0) { spointPtr->threadId = thisThreadId; spointPtr->clientData = clientData; } else { if (spointPtr->threadId != thisThreadId) { SyncPoint *newPtr; QueuePush(&danglingSpoints, spointPtr); newPtr = AllocSyncPoint(signum, clientData); SetSigMapValue(entryPtr, newPtr); } else { /* Do nothing -- the syncpoint is already ours */ } } } return entryPtr; }
void main() { queue s; int i,j,k,m; QueueInit(&s); while(1) { printf("*****请选择操作*****\n"); printf("1、入队 2、出队 3、取队头元素 4、遍历队列 5、退出\n"); printf("选择:\n"); scanf("%d",&i); switch(i) { case 1: printf("输入要入队的个数:\n"); scanf("%d",&m); printf("输入%d个元素的值:\n",m); for(i=0; i<m; i++) { scanf("%d",&j); QueuePush(&s,j); } break; case 2: printf("输入要出队的个数:\n"); scanf("%d",&i); printf("出队的元素为:\n"); for(; i>0; i--) { k=QueuePop(&s,&j); if(k) printf("%d ",j); else { printf("该队没有足够的元素\n"); break; } } break; case 3: i=Getfront(&s); if(i!=0) printf("队头元素为%d\n",i); else printf("该队列没有元素!\n"); break; case 4: QueueTrav(s); break; case 5: break; default: printf("您的输入有误\n"); } system("pause"); system("cls"); } }
/** Code to test this Queue implementation This code is only included for example purposes and to ensure that this implementation works in all situations This code at present is not exhaustive. STEPS: - Test 1 -# Push some integers onto the Queue inplace of pointers. -# Pop all of the elements off the queue to make sure that they match - Test2 -# TODO \todo Write a more exhaustive test */ int QueueTest() { Queue queue; void* ary[10]; QueueInit( &queue, ary, 10 ); // Test 1 { { int i; for( i = 0 ; i < 10 ; i++ ) QueuePush( &queue, (void*) i ); } if(QueueIsFull(&queue) != TRUE) return 1; { int i; for( i = 0 ; i < 10 ; i++ ) { int val = (int) QueuePop( &queue ); if(val != i) return 2; } } if(QueueIsEmpty(&queue) != TRUE) return 3; if(QueueIsFull(&queue) != FALSE) return 4; } // Test 2 { QueuePush( &queue, (void*) 1234); if((int)QueueFront(&queue) != 1234) return 5; } return 0; }
int CTAsyncWorkQueue::TryPutTask(CTAWQTask *v_pTask) { if (m_stopped) { return -1; } return QueuePush(m_pRunQ, v_pTask); }
int main(int argc, char *argv[]) { void *p = NULL; long msg = 0; void *pMsg = NULL; p = QueueCreate(10); if (NULL == p) { printf("Create queue failed.\n"); return -1; } msg = 90; while (msg--) { if (QueuePush(p, (void *)msg) < 0) { printf("----------Queue is full now-----------\n"); break; } printf("Push ok. [msg: %ld, size: %d]\n", msg, QueueGetSize(p)); } msg = 15; while (msg--) { if (QueuePopPush(p, (void *)msg, &pMsg) < 0) { printf("----------QueuePopPush failed-----------\n"); break; } printf("PopPush ok. [msgPop: %ld, msgPush: %ld, size: %d]\n", (long)pMsg, msg, QueueGetSize(p)); } msg = 60; while (msg--) { if (QueuePop(p, &pMsg) < 0) { printf("----------Queue is empty now-----------\n"); break; } printf("Pop ok. [msg: %ld, size: %d]\n", (long)pMsg, QueueGetSize(p)); } QueueDestroy(p); system("pause"); return 0; }
int Wireless_Send(Skaro_Wireless * w, char command, char length, char * data){ unsigned char top; CPU_MSR msr = DISABLE_INTERRUPTS(); QueuePush(&(w->write_queue), (void *)(uint32)command); QueuePush(&(w->write_queue),(void *)(uint32)length); int i; for(i = 0; i < length; i++){ QueuePush(&w->write_queue, (void *)(uint32)data[i]); } // If there is no send in progress, initiate a send if (!(w->send_in_progress)) { // We are about to send it, so pop it off top = (unsigned char)(uint32)QueuePop(&(w->write_queue)); w->send_in_progress = 1; XUartLite_Send(&(w->uart), &top, 1); } RESTORE_INTERRUPTS(msr); return 1; }
void DeleteSyncPoint ( SyncPointMapEntry entry) { SyncPoint *spointPtr; spointPtr = GetSigMapValue(entry); if (spointPtr->signaled != 0) { if (spointPtr->threadId != Tcl_GetCurrentThread()) { QueuePush(&danglingSpoints, spointPtr); /* TODO notify the owner thread that it has just * lost the syncpoint and should free any state * associated with it */ } } DeleteSigMapEntry(entry); }
static void HarvestSyncpoint ( SyncPoint *spointPtr, Queue *queuePtr ) { int signaled = spointPtr->signaled; if (signaled) { do { QueuePush(queuePtr, CreateSignalEvent(spointPtr->threadId, spointPtr->signum)); --signaled; } while (signaled > 0); spointPtr->signaled = 0; } }
void *ThreadWork(void *v_pObj) { CTAWQTask *pTaskPush = NULL; CTAWQTask *pTaskPop = NULL; CTAsyncWorkQueue *pAWQ = (CTAsyncWorkQueue *)v_pObj; int ret = 0; while (1) { if (NULL == pTaskPush) { ret = QueuePop(pAWQ->m_pRunQ, (void **)&pTaskPop); } else { ret = QueuePopPush(pAWQ->m_pRunQ, pTaskPush, (void **)&pTaskPop); } if (0 != ret) { usleep(100 * 1000); pTaskPush = NULL; continue; } ret = pTaskPop->OnRun(); if (0 != ret) { pTaskPush = pTaskPop; continue; } pTaskPush = NULL; do { ret = QueuePush(pAWQ->m_pEndQ, pTaskPop); } while (0 > ret); } return NULL; }
/* Function SerialProcessIncomingData(PSERIAL pSerialPort) * Description: * - Handle read data from serial port * Input: * - pSerialPort: Serial management data struct. */ VOID SerialProcessIncomingData(PSERIAL pSerialPort) { PBYTE pReceiveBuffer = (PBYTE)malloc(255); ssize_t byReceiveByte; PBYTE pReadBuffer = pReceiveBuffer; ssize_t byTotal = 0; while(1) { byReceiveByte = read(pSerialPort->tty_fd, pReadBuffer, 255); if (byReceiveByte > 0) { byTotal = byReceiveByte; pReadBuffer += byReceiveByte; } while (byReceiveByte > 0) { //SerialHandleIncomingBuffer(pSerialPort, pReceiveBuffer, byReceiveByte); usleep(3000); byReceiveByte = read(pSerialPort->tty_fd, pReadBuffer, 255); if (byReceiveByte > 0) { byTotal += byReceiveByte; pReadBuffer += byReceiveByte; } } if (byTotal > 0) { QueuePush(pReceiveBuffer, byTotal, pSerialPort->pInputQueue); memset(pReceiveBuffer, 0, 255); pReadBuffer = pReceiveBuffer; byTotal = 0; } //if (pReceiveBuffer != NULL) //free((void*)pReceiveBuffer); usleep(10000); } if (pReceiveBuffer != NULL) free(pReceiveBuffer); }
/* Function SerialHandleIncomingByte(PSERIAL pSerialPort, BYTE byData) * Description: * - Handle read data byte from serial input using ZNP protocol * Input: * - pSerialPort: Serial management data struct. * - byData: Data read from input buffer. */ static VOID SerialHandleIncomingByte(PSERIAL pSerialPort, BYTE byData) { BYTE nIndex; if ((g_nPackageIndex == 0) && (byData == 0xFE)) { g_pReceivePackage[g_nPackageIndex] = 0xFE; g_nPackageIndex++; } else if (g_nPackageIndex == (g_pReceivePackage[1] + 4)) { BYTE byCRC = 0; unsigned int nContentLength = g_pReceivePackage[1] + 3; for (nIndex = 1; nIndex <= nContentLength; nIndex++) { byCRC ^= g_pReceivePackage[nIndex]; } if (byCRC == byData) { g_pReceivePackage[g_nPackageIndex] = byData; g_nPackageIndex++; // push validated data to queue QueuePush((void *)g_pReceivePackage, g_nPackageIndex, pSerialPort->pInputQueue); g_nPackageIndex = 0; } else { g_nPackageIndex = 0; FLUENT_LOGGER_DEBUG("Invalid package received"); printf("Invalid package received"); } } else { g_pReceivePackage[g_nPackageIndex] = byData; g_nPackageIndex++; } }
bool TheTask(JSContext *cx, TaskPrivate *pv) { bool ok; jsval argv[3] = { JSVAL_NULL }; // argv[0] is rval and code JSObject *globalObj; globalObj = JL_GetGlobal(cx); ASSERT( globalObj ); // no need to mutex this because this and the constructor are the only places that access pv->serializedCode. ok = UnserializeJsval(cx, pv->serializedCode, &argv[0]); SerializerFree(&pv->serializedCode); JL_CHK( ok ); JSFunction *fun; fun = JS_ValueToFunction(cx, argv[0]); JL_CHK( fun ); /* JSObject *funObj; funObj = JS_GetFunctionObject(fun); ASSERT( funObj ); // JL_CHK( JS_SetParent(cx, funObj, globalObj) ); // re-scope the function ASSERT( JS_GetParent(funObj) ); */ size_t index; index = 0; for (;;) { JL_CHK( JLSemaphoreAcquire(pv->requestSem) ); // -1 // wait for a request JLMutexAcquire(pv->mutex); // -- if ( pv->end ) { // manage the end of the thread JLMutexRelease(pv->mutex); // ++ break; } SerializedData *serializedRequest; serializedRequest = (SerializedData*)QueueShift(&pv->requestList); pv->pendingRequestCount--; pv->processingRequestCount++; // = 1; JLMutexRelease(pv->mutex); // ++ ASSERT( serializedRequest ); ok = UnserializeJsval(cx, serializedRequest, &argv[1]); SerializerFree(&serializedRequest); JL_CHK( ok ); argv[2] = INT_TO_JSVAL(index++); ok = JS_CallFunction(cx, globalObj, fun, COUNTOF(argv)-1, argv+1, argv); if ( ok ) { SerializedData *serializedResponse; SerializerCreate(&serializedResponse); JL_CHK( SerializeJsval(cx, serializedResponse, &argv[0]) ); JLMutexAcquire(pv->mutex); // -- QueuePush(&pv->responseList, serializedResponse); pv->pendingResponseCount++; pv->processingRequestCount--; JLMutexRelease(pv->mutex); // ++ } else { jsval ex; if ( JL_IsExceptionPending(cx) ) { // manageable error ok = JS_GetPendingException(cx, &ex); JS_ClearPendingException(cx); JL_CHK( ok ); } else { ex = JSVAL_VOID; // unknown exception } SerializedData *serializedException; SerializerCreate(&serializedException); JL_CHK( SerializeJsval(cx, serializedException, &ex) ); JLMutexAcquire(pv->mutex); // -- QueuePush(&pv->exceptionList, serializedException); QueuePush(&pv->responseList, NULL); // signals an exception pv->pendingResponseCount++; pv->processingRequestCount--; JLMutexRelease(pv->mutex); // ++ } ASSERT( pv->processingRequestCount == 0 ); JLSemaphoreRelease(pv->responseSem); // +1 // signals a response JLEventTrigger(pv->responseEvent); } return true; JL_BAD; }
bool WaitConditionImpl::Wait(Mutex *pmutex, unsigned delay) { bool result = 0; unsigned i; unsigned lockCount = pmutex->pImpl->LockCount; EventPoolEntry* pentry; // Mutex must have been locked if (lockCount == 0) return 0; // Add an object to the wait queue WaitQueueLoc.DoLock(); QueuePush(pentry = GetNewEvent()); WaitQueueLoc.Unlock(); // Finally, release a mutex or semaphore if (pmutex->pImpl->Recursive) { // Release the recursive mutex N times pmutex->pImpl->LockCount = 0; for(i=0; i<lockCount; i++) ::ReleaseMutex(pmutex->pImpl->hMutexOrSemaphore); } else { pmutex->pImpl->LockCount = 0; ::ReleaseSemaphore(pmutex->pImpl->hMutexOrSemaphore, 1, NULL); } // Note that there is a gap here between mutex.Unlock() and Wait(). However, // if notify() comes in at this point in the other thread it will set our // corresponding event so wait will just fall through, as expected. // Block and wait on the event DWORD waitResult = ::WaitForSingleObject(pentry->hEvent, (delay == OVR_WAIT_INFINITE) ? INFINITE : delay); /* repeat_wait: DWORD waitResult = ::MsgWaitForMultipleObjects(1, &pentry->hEvent, FALSE, (delay == OVR_WAIT_INFINITE) ? INFINITE : delay, QS_ALLINPUT); */ WaitQueueLoc.DoLock(); switch(waitResult) { case WAIT_ABANDONED: case WAIT_OBJECT_0: result = 1; // Wait was successful, therefore the event entry should already be removed // So just add entry back to a free list ReleaseEvent(pentry); break; /* case WAIT_OBJECT_0 + 1: // Messages in WINDOWS queue { MSG msg; PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE); WaitQueueLoc.Unlock(); goto repeat_wait; } break; */ default: // Timeout, our entry should still be in a queue QueueFindAndRemove(pentry); ReleaseEvent(pentry); } WaitQueueLoc.Unlock(); // Re-aquire the mutex for(i=0; i<lockCount; i++) pmutex->DoLock(); // Return the result return result; }
/* Function SerialOutput(PSERIAL pSerialPort, PBYTE pData, BYTE nSize) * Description: * - Write data to serial port's output queue. * Input: * - pSerialPort: Serial management data struct. * - pData: Pointer to data buffer. * - nSize: Size of data. */ BYTE SerialOutput(PSERIAL pSerialPort, PBYTE pData, BYTE nSize) { return QueuePush(pData, nSize, pSerialPort->pOutputQueue); }
JLThreadFuncDecl TaskThreadProc( void *threadArg ) { TaskPrivate *pv = NULL; jl::ChunkedBuffer<char> errBuffer; JSContext *cx = CreateHost((uint32_t)-1, (uint32_t)-1, 0); if ( cx == NULL ) // out of memory JLThreadExit(0); jl::Host& hpv; hpv = jl::Host::getJLHost(cx); // allocator must be threadsafe ! hpv->alloc.malloc = jl_malloc; hpv->alloc.calloc = jl_calloc; hpv->alloc.memalign = jl_memalign; hpv->alloc.realloc = jl_realloc; hpv->alloc.msize = jl_msize; hpv->alloc.free = jl_free; JL_CHK( InitHost(cx, _unsafeMode != 0, NULL, NULL, TaskStdErrHostOutput, &errBuffer) ); JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_DONT_REPORT_UNCAUGHT); pv = (TaskPrivate*)threadArg; ASSERT( pv ); bool ok; ok = TheTask(cx, pv); if ( !ok ) { // handle fatal error jsval ex; if ( JL_IsExceptionPending(cx) ) { ok = JS_GetPendingException(cx, &ex); JS_ClearPendingException(cx); JL_CHK( ok ); } else { JL_CHK( JL_NativeToJsval(cx, errBuffer.GetData(), errBuffer.Length(), &ex) ); } SerializedData * serializedException; SerializerCreate(&serializedException); if ( !SerializeJsval(cx, serializedException, &ex) ) { ASSERT( false ); JLSemaphoreRelease(pv->responseSem); // +1 JL_ERR( E_JSLIBS, E_STR("serializer"), E_INTERNAL ); } JLMutexAcquire(pv->mutex); // -- pv->end = true; QueuePush(&pv->exceptionList, serializedException); JLMutexRelease(pv->mutex); // ++ JLSemaphoreRelease(pv->responseSem); // +1 } good: bad: // These queues must be destroyed before cx because SerializedData * *ser hold a reference to the context that created the value. JLMutexAcquire(pv->mutex); // -- while ( !QueueIsEmpty(&pv->exceptionList) ) { SerializedData * ser = (SerializedData *)QueueShift(&pv->exceptionList); SerializerFree(&ser); } while ( !QueueIsEmpty(&pv->responseList) ) { SerializedData * ser = (SerializedData *)QueueShift(&pv->responseList); SerializerFree(&ser); } JLMutexRelease(pv->mutex); // ++ if ( cx ) DestroyHost(cx, false); // no skip cleanup, else memory leaks. JLThreadExit(0); return 0; }
static void NET_SV_SendPacket(net_addr_t *addr, net_packet_t *packet) { QueuePush(&client_queue, NET_PacketDup(packet)); }
static void NET_CL_SendPacket(net_addr_t *addr, net_packet_t *packet) { QueuePush(&server_queue, NET_PacketDup(packet)); }