CPLList *CPLListInsert( CPLList *psList, void *pData, int nPosition ) { if ( nPosition < 0 ) return psList; /* Nothing to do!*/ if ( nPosition == 0) { CPLList *psNew = static_cast<CPLList *>( CPLMalloc( sizeof(CPLList) ) ); psNew->pData = pData; psNew->psNext = psList; psList = psNew; return psList; } const int nCount = CPLListCount( psList ); if ( nCount < nPosition ) { /* Allocate room for the new object */ CPLList* psLast = CPLListGetLast(psList); for ( int i = nCount; i <= nPosition - 1; i++ ) { psLast = CPLListAppend( psLast, NULL ); if (psList == NULL) psList = psLast; else psLast = psLast->psNext; } psLast = CPLListAppend( psLast, pData ); if (psList == NULL) psList = psLast; /* coverity[leaked_storage] */ return psList; } CPLList *psNew = static_cast<CPLList *>( CPLMalloc( sizeof(CPLList) ) ); psNew->pData = pData; CPLList *psCurrent = psList; for ( int i = 0; i < nPosition - 1; i++ ) psCurrent = psCurrent->psNext; psNew->psNext = psCurrent->psNext; psCurrent->psNext = psNew; return psList; }
CPLWorkerThreadJob * CPLWorkerThreadPool::GetNextJob( CPLWorkerThread* psWorkerThread ) { while(true) { CPLAcquireMutex(hMutex, 1000.0); if( eState == CPLWTS_STOP ) { CPLReleaseMutex(hMutex); return nullptr; } CPLList* psTopJobIter = psJobQueue; if( psTopJobIter ) { psJobQueue = psTopJobIter->psNext; #if DEBUG_VERBOSE CPLDebug("JOB", "%p got a job", psWorkerThread); #endif CPLWorkerThreadJob* psJob = static_cast<CPLWorkerThreadJob*>(psTopJobIter->pData); CPLReleaseMutex(hMutex); CPLFree(psTopJobIter); return psJob; } if( !psWorkerThread->bMarkedAsWaiting ) { psWorkerThread->bMarkedAsWaiting = TRUE; nWaitingWorkerThreads++; CPLAssert(nWaitingWorkerThreads <= static_cast<int>(aWT.size())); CPLList* psItem = static_cast<CPLList *>(VSI_MALLOC_VERBOSE(sizeof(CPLList))); if( psItem == nullptr ) { eState = CPLWTS_ERROR; CPLCondSignal(hCond); CPLReleaseMutex(hMutex); return nullptr; } psItem->pData = psWorkerThread; psItem->psNext = psWaitingWorkerThreadsList; psWaitingWorkerThreadsList = psItem; #if DEBUG_VERBOSE CPLAssert(CPLListCount(psWaitingWorkerThreadsList) == nWaitingWorkerThreads); #endif } CPLCondSignal(hCond); CPLAcquireMutex(psWorkerThread->hMutex, 1000.0); #if DEBUG_VERBOSE CPLDebug("JOB", "%p sleeping", psWorkerThread); #endif CPLReleaseMutex(hMutex); CPLCondWait( psWorkerThread->hCond, psWorkerThread->hMutex ); // TODO(rouault): Explain or delete. // CPLWorkerThreadJob* psJob = psWorkerThread->psNextJob; // psWorkerThread->psNextJob = nullptr; CPLReleaseMutex(psWorkerThread->hMutex); // TODO(rouault): Explain or delete. // if( psJob ) // return psJob; } }
void object::test<1>() { CPLList* list; list = CPLListInsert(NULL, (void*)0, 0); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 2); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 1); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 0); ensure(CPLListCount(list) == 0); list = NULL; list = CPLListInsert(NULL, (void*)0, 2); ensure(CPLListCount(list) == 3); list = CPLListRemove(list, 2); ensure(CPLListCount(list) == 2); list = CPLListRemove(list, 1); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 0); ensure(CPLListCount(list) == 0); list = NULL; list = CPLListAppend(list, (void*)1); ensure(CPLListGet(list,0) == list); ensure(CPLListGet(list,1) == NULL); list = CPLListAppend(list, (void*)2); list = CPLListInsert(list, (void*)3, 2); ensure(CPLListCount(list) == 3); CPLListDestroy(list); list = NULL; list = CPLListAppend(list, (void*)1); list = CPLListAppend(list, (void*)2); list = CPLListInsert(list, (void*)4, 3); CPLListGet(list,2)->pData = (void*)3; ensure(CPLListCount(list) == 4); ensure(CPLListGet(list,0)->pData == (void*)1); ensure(CPLListGet(list,1)->pData == (void*)2); ensure(CPLListGet(list,2)->pData == (void*)3); ensure(CPLListGet(list,3)->pData == (void*)4); CPLListDestroy(list); list = NULL; list = CPLListInsert(list, (void*)4, 1); CPLListGet(list,0)->pData = (void*)2; list = CPLListInsert(list, (void*)1, 0); list = CPLListInsert(list, (void*)3, 2); ensure(CPLListCount(list) == 4); ensure(CPLListGet(list,0)->pData == (void*)1); ensure(CPLListGet(list,1)->pData == (void*)2); ensure(CPLListGet(list,2)->pData == (void*)3); ensure(CPLListGet(list,3)->pData == (void*)4); list = CPLListRemove(list, 1); list = CPLListRemove(list, 1); list = CPLListRemove(list, 0); list = CPLListRemove(list, 0); ensure(list == NULL); }