Created
November 28, 2014 18:11
-
-
Save flippmoke/16cd26a0247422f25840 to your computer and use it in GitHub Desktop.
Improved CPLCreateOrAcquireMutex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CPL_MULTIPROC_PTHREAD | |
#ifndef MUTEX_NONE | |
static void *hCOAMutex = NULL; | |
#endif | |
int CPLCreateOrAcquireMutex( void **phMutex, double dfWaitInSeconds ) | |
{ | |
int bSuccess = FALSE; | |
#ifndef MUTEX_NONE | |
/* | |
** ironically, creation of this initial mutex is not threadsafe | |
** even though we use it to ensure that creation of other mutexes | |
** is threadsafe. | |
*/ | |
if ( NULL != *phMutex ) | |
return CPLAcquireMutex( *phMutex, dfWaitInSeconds ); | |
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; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment