Skip to content

Instantly share code, notes, and snippets.

@flippmoke
Created November 28, 2014 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flippmoke/16cd26a0247422f25840 to your computer and use it in GitHub Desktop.
Save flippmoke/16cd26a0247422f25840 to your computer and use it in GitHub Desktop.
Improved CPLCreateOrAcquireMutex
#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