[gdal-dev] GDAL thread safety

Even Rouault even.rouault at mines-paris.org
Wed Oct 3 04:44:44 PDT 2012


Hi

> The file I am having problems with is a VRT file that references a raw file
> using VRTRawRasterBand.
>
> I have used gdal_translate to convert it to a TIFF and that does not have
> problems.
>
> Is it possible the VRT driver is not thread safe?

On closer review, it appears that VRTRawRasterBand isn't thread-safe. It uses
CPLOpenShared() which will not work if you have different threads that open VRT,
but if those VRT points to the same raw file...

Could you try to recompile GDAL with the following patch and open a ticket in
GDAL Trac). Not that this is just an interim solution. The proper solution would
be to fix CPLOpenShared() to be more thread-friendly (that is to say to return
different file handles if called with the same filename from different threads)

Index: vrtrawrasterband.cpp
===================================================================
--- vrtrawrasterband.cpp	(revision 24972)
+++ vrtrawrasterband.cpp	(working copy)
@@ -195,14 +195,14 @@
 /* -------------------------------------------------------------------- */
 /*      Try and open the file.  We always use the large file API.       */
 /* -------------------------------------------------------------------- */
-    FILE *fp = CPLOpenShared( pszExpandedFilename, "rb+", TRUE );
+    FILE *fp = (FILE*) VSIFOpenL( pszExpandedFilename, "rb+" );

     if( fp == NULL )
-        fp = CPLOpenShared( pszExpandedFilename, "rb", TRUE );
+        fp = (FILE*) VSIFOpenL( pszExpandedFilename, "rb" );

     if( fp == NULL && ((VRTDataset *)poDS)->GetAccess() == GA_Update )
     {
-        fp = CPLOpenShared( pszExpandedFilename, "wb+", TRUE );
+        fp = (FILE*) VSIFOpenL( pszExpandedFilename, "wb+" );
     }

     if( fp == NULL )
@@ -272,7 +272,7 @@
         /* since data can be flushed in the destructor */
         if( fp != NULL )
         {
-            CPLCloseShared( (FILE*) fp );
+            VSIFCloseL( fp );
         }
     }
     CPLFree( pszSourceFilename );


>
> Also, is there an official list of thread safe drivers? If not could
> someone with the knowledge perhaps update the FAQ to list the 'vetted'
> drivers?

No, there's no such list. And it would be tricky to establish. For example, I
would have sincerely and wrongly put the VRT driver in the vetted list, before
you raised that particular issue...

Best regards,

Even


More information about the gdal-dev mailing list