ECWAsyncReader::ECWAsyncReader() { hMutex = CPLCreateMutex(); CPLReleaseMutex( hMutex ); poFileView = NULL; bUpdateReady = FALSE; bComplete = FALSE; }
int CPLCreateOrAcquireMutex( void **phMutex, double dfWaitInSeconds ) { int bSuccess = FALSE; #ifndef MUTEX_NONE static void *hCOAMutex = NULL; /* ** ironically, creation of this initial mutex is not threadsafe ** even though we use it to ensure that creation of other mutexes ** is threadsafe. */ if( hCOAMutex == NULL ) { hCOAMutex = CPLCreateMutex(); if (hCOAMutex == NULL) { *phMutex = NULL; return FALSE; } } else { CPLAcquireMutex( hCOAMutex, dfWaitInSeconds ); } if( *phMutex == NULL ) { *phMutex = CPLCreateMutex(); bSuccess = *phMutex != NULL; CPLReleaseMutex( hCOAMutex ); } else { CPLReleaseMutex( hCOAMutex ); bSuccess = CPLAcquireMutex( *phMutex, dfWaitInSeconds ); } #endif /* ndef MUTEX_NONE */ return bSuccess; }
GDALAbstractBandBlockCache::GDALAbstractBandBlockCache(GDALRasterBand* poBandIn) : hSpinLock(CPLCreateLock(LOCK_SPIN)), psListBlocksToFree(NULL), hCond(CPLCreateCond()), hCondMutex(CPLCreateMutex()), nKeepAliveCounter(0) { poBand = poBandIn; if( hCondMutex ) CPLReleaseMutex(hCondMutex); }
void *CPLCreateCond() { Win32Cond* psCond = (Win32Cond*) malloc(sizeof(Win32Cond)); if (psCond == NULL) return NULL; psCond->hInternalMutex = CPLCreateMutex(); if (psCond->hInternalMutex == NULL) { free(psCond); return NULL; } CPLReleaseMutex(psCond->hInternalMutex); psCond->psWaiterList = NULL; return psCond; }
VSICurlStreamingHandle::VSICurlStreamingHandle(VSICurlStreamingFSHandler* poFS, const char* pszURL) { this->poFS = poFS; this->pszURL = CPLStrdup(pszURL); #ifdef notdef nRecomputedChecksumOfFirst1024Bytes = 0; #endif curOffset = 0; poFS->AcquireMutex(); CachedFileProp* cachedFileProp = poFS->GetCachedFileProp(pszURL); eExists = cachedFileProp->eExists; fileSize = cachedFileProp->fileSize; bHastComputedFileSize = cachedFileProp->bHastComputedFileSize; bIsDirectory = cachedFileProp->bIsDirectory; poFS->ReleaseMutex(); bCanTrustCandidateFileSize = TRUE; bHasCandidateFileSize = FALSE; nCandidateFileSize = 0; nCachedSize = 0; pCachedData = NULL; bEOF = FALSE; hCurlHandle = NULL; hThread = NULL; hRingBufferMutex = CPLCreateMutex(); ReleaseMutex(); hCondProducer = CPLCreateCond(); hCondConsumer = CPLCreateCond(); bDownloadInProgress = FALSE; bDownloadStopped = FALSE; bAskDownloadEnd = FALSE; nRingBufferFileOffset = 0; pabyHeaderData = NULL; nHeaderSize = 0; nBodySize = 0; nHTTPCode = 0; }
int CPLCreateOrAcquireMutex( void **phMutex, double dfWaitInSeconds ) { int bSuccess = FALSE; static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&global_mutex); if( *phMutex == NULL ) { *phMutex = CPLCreateMutex(); bSuccess = *phMutex != NULL; pthread_mutex_unlock(&global_mutex); } else { pthread_mutex_unlock(&global_mutex); bSuccess = CPLAcquireMutex( *phMutex, dfWaitInSeconds ); } return bSuccess; }
VSICurlStreamingFSHandler::VSICurlStreamingFSHandler() { hMutex = CPLCreateMutex(); CPLReleaseMutex(hMutex); }
int main( int argc, char ** argv ) { int iArg; /* -------------------------------------------------------------------- */ /* Process arguments. */ /* -------------------------------------------------------------------- */ argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); if( argc < 1 ) exit( -argc ); for( iArg = 1; iArg < argc; iArg++ ) { if( EQUAL(argv[iArg],"-i") && iArg < argc-1 ) nIterations = atoi(argv[++iArg]); else if( EQUAL(argv[iArg],"-oi") && iArg < argc-1 ) nOpenIterations = atoi(argv[++iArg]); else if( EQUAL(argv[iArg],"-t") && iArg < argc-1 ) nThreadCount = atoi(argv[++iArg]); else if( EQUAL(argv[iArg],"-nlo") ) bLockOnOpen = FALSE; else if( pszFilename == NULL ) pszFilename = argv[iArg]; else { printf( "Unrecognised argument: %s\n", argv[iArg] ); Usage(); } } if( pszFilename == NULL ) { printf( "Need a file to operate on.\n" ); Usage(); exit( 1 ); } if( nOpenIterations > 0 ) bLockOnOpen = FALSE; /* -------------------------------------------------------------------- */ /* Get the checksum of band1. */ /* -------------------------------------------------------------------- */ GDALDatasetH hDS; GDALAllRegister(); hDS = GDALOpen( pszFilename, GA_ReadOnly ); if( hDS == NULL ) exit( 1 ); nChecksum = GDALChecksumImage( GDALGetRasterBand( hDS, 1 ), 0, 0, GDALGetRasterXSize( hDS ), GDALGetRasterYSize( hDS ) ); GDALClose( hDS ); printf( "Got checksum %d, launching %d worker threads on %s, %d iterations.\n", nChecksum, nThreadCount, pszFilename, nIterations ); /* -------------------------------------------------------------------- */ /* Fire off worker threads. */ /* -------------------------------------------------------------------- */ int iThread; pGlobalMutex = CPLCreateMutex(); CPLReleaseMutex( pGlobalMutex ); nPendingThreads = nThreadCount; for( iThread = 0; iThread < nThreadCount; iThread++ ) { if( CPLCreateThread( WorkerFunc, NULL ) == -1 ) { printf( "CPLCreateThread() failed.\n" ); exit( 1 ); } } while( nPendingThreads > 0 ) CPLSleep( 0.5 ); CPLReleaseMutex( pGlobalMutex ); printf( "All threads complete.\n" ); CSLDestroy( argv ); GDALDestroyDriverManager(); return 0; }
int main( int argc, char ** argv ) { /* -------------------------------------------------------------------- */ /* Process arguments. */ /* -------------------------------------------------------------------- */ argc = GDALGeneralCmdLineProcessor(argc, &argv, 0); if( argc < 1 ) exit(-argc); int nThreadCount = 4; bool bOpenInThreads = true; for( int iArg = 1; iArg < argc; iArg++ ) { if( iArg < argc-1 && EQUAL(argv[iArg], "-i") ) { nIterations = atoi(argv[++iArg]); } else if( iArg < argc-1 && EQUAL(argv[iArg], "-oi") ) { nOpenIterations = atoi(argv[++iArg]); } else if( iArg < argc-1 && EQUAL(argv[iArg], "-t") ) { nThreadCount = atoi(argv[++iArg]); } else if( EQUAL(argv[iArg], "-lock_on_open") ) { bLockOnOpen = true; } else if( EQUAL(argv[iArg], "-open_in_main") ) { bOpenInThreads = false; } else if( pszFilename == nullptr ) { pszFilename = argv[iArg]; } else { printf("Unrecognized argument: %s\n", argv[iArg]); Usage(); } } if( pszFilename == nullptr ) { printf("Need a file to operate on.\n"); Usage(); exit(1); } if( nOpenIterations > 0 ) bLockOnOpen = false; /* -------------------------------------------------------------------- */ /* Get the checksum of band1. */ /* -------------------------------------------------------------------- */ GDALDatasetH hDS = nullptr; GDALAllRegister(); for( int i = 0; i < 2; i++ ) { hDS = GDALOpen( pszFilename, GA_ReadOnly ); if( hDS == nullptr ) exit( 1 ); nChecksum = GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0, GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS)); GDALClose(hDS); } printf( "Got checksum %d, launching %d worker threads on %s, %d iterations.\n", nChecksum, nThreadCount, pszFilename, nIterations); /* -------------------------------------------------------------------- */ /* Fire off worker threads. */ /* -------------------------------------------------------------------- */ pGlobalMutex = CPLCreateMutex(); CPLReleaseMutex(pGlobalMutex); nPendingThreads = nThreadCount; std::vector<GDALDatasetH> aoDS; for( int iThread = 0; iThread < nThreadCount; iThread++ ) { hDS = nullptr; if( !bOpenInThreads ) { hDS = GDALOpen(pszFilename, GA_ReadOnly); if( !hDS ) { printf("GDALOpen() failed.\n"); exit(1); } aoDS.push_back(hDS); } if( CPLCreateThread(WorkerFunc, hDS) == -1 ) { printf("CPLCreateThread() failed.\n"); exit(1); } } while( nPendingThreads > 0 ) CPLSleep(0.5); CPLDestroyMutex(pGlobalMutex); for( size_t i = 0; i < aoDS.size(); ++i ) GDALClose(aoDS[i]); printf("All threads complete.\n"); CSLDestroy(argv); GDALDestroyDriverManager(); return 0; }