Opened 14 years ago

Last modified 14 years ago

#3628 closed defect

race condition in static variable bTLSKeySetup Cpl_multiproc.cpp — at Initial Version

Reported by: lucafibbi Owned by: warmerdam
Priority: normal Milestone: 1.8.0
Component: default Version: unspecified
Severity: normal Keywords: thread safe
Cc: warmerdam

Description

In the Cpl_multiproc.cpp file when using the static variable bTLSKeySetup simultaneously from multiple threads race condition occurs.

I think that the following implementation of the functions CPLCleanupTLS and CPLGetTLSList for pthred library should be thread safe:

static pthread_once_t oTLSKeySetup = PTHREAD_ONCE_INIT;

static void CPLMake_key() {

if( pthread_key_create( &oTLSKey,

(void (*)(void*)) CPLCleanupTLSList ) != 0 )

{

CPLError( CE_Fatal, CPLE_AppDefined,

"pthread_key_create() failed!" );

}

}

// /* CPLCleanupTLS() */ //

void CPLCleanupTLS()

{

void papTLSList;

papTLSList = (void ) pthread_getspecific( oTLSKey ); if( papTLSList == NULL )

return;

pthread_setspecific( oTLSKey, NULL );

CPLCleanupTLSList( papTLSList );

}

// /* CPLGetTLSList() */ //

static void CPLGetTLSList()

{

void papTLSList;

if ( pthread_once(&oTLSKeySetup, CPLMake_key) != 0 ) {

CPLError( CE_Fatal, CPLE_AppDefined,

"pthread_once() failed!" );

}

papTLSList = (void ) pthread_getspecific( oTLSKey ); if( papTLSList == NULL ) {

papTLSList = (void ) CPLCalloc(sizeof(void*),CTLS_MAX*2); if( pthread_setspecific( oTLSKey, papTLSList ) != 0 ) {

CPLError( CE_Fatal, CPLE_AppDefined,

"pthread_setspecific() failed!" );

}

}

return papTLSList;

}

Change History (0)

Note: See TracTickets for help on using tickets.