@@ -57,6 +57,9 @@ extern "C"
57
57
#define G__setenv (name,value ) G__setenv( ( char * ) (name), (char *) (value) )
58
58
#endif
59
59
60
+ #define GRASS_LOCK sMutex .lock();
61
+ #define GRASS_UNLOCK sMutex .unlock();
62
+
60
63
#ifdef Q_OS_WIN
61
64
#include < windows.h>
62
65
QString GRASS_LIB_EXPORT QgsGrass::shortPath ( const QString &path )
@@ -411,13 +414,18 @@ QString QgsGrass::mMapsetLock;
411
414
QString QgsGrass::mGisrc ;
412
415
QString QgsGrass::mTmp ;
413
416
417
+ QMutex QgsGrass::sMutex ;
418
+
414
419
int QgsGrass::error_routine ( char *msg, int fatal )
415
420
{
416
421
return error_routine (( const char * ) msg, fatal );
417
422
}
418
423
419
424
int QgsGrass::error_routine ( const char *msg, int fatal )
420
425
{
426
+ // G_fatal_error obviously is not thread safe (everything static in GRASS, especially fatal_jmp_buf)
427
+ // it means that anything which may end up with G_fatal_error must use mutex
428
+
421
429
// Unfortunately the exceptions thrown here can only be caught if GRASS libraries are compiled
422
430
// with -fexception option on Linux (works on Windows)
423
431
// GRASS developers are reluctant to add -fexception by default
@@ -802,6 +810,7 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString mapsetPath )
802
810
QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers ( QString gisdbase,
803
811
QString location, QString mapset, QString mapName )
804
812
{
813
+ GRASS_LOCK
805
814
QgsDebugMsg ( QString ( " gisdbase = %1 location = %2 mapset = %3 mapName = %4" ).arg ( gisdbase ).arg ( location ).arg ( mapset ).arg ( mapName ) );
806
815
QStringList list;
807
816
@@ -826,6 +835,7 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
826
835
{
827
836
Q_UNUSED ( e );
828
837
QgsDebugMsg ( QString ( " Cannot open GRASS vector: %1" ).arg ( e.what () ) );
838
+ GRASS_UNLOCK
829
839
return list;
830
840
}
831
841
@@ -839,12 +849,14 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
839
849
#ifndef Q_OS_WIN
840
850
Vect_close ( &map );
841
851
#endif
852
+ GRASS_UNLOCK
842
853
return list;
843
854
}
844
855
else if ( level < 1 )
845
856
{
846
857
QgsDebugMsg ( " Cannot open vector" );
847
858
QMessageBox::warning ( 0 , QObject::tr ( " Warning" ), QObject::tr ( " Cannot open vector %1 in mapset %2" ).arg ( mapName ).arg ( mapset ) );
859
+ GRASS_UNLOCK
848
860
return list;
849
861
}
850
862
@@ -926,6 +938,7 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
926
938
927
939
Vect_close ( &map );
928
940
941
+ GRASS_UNLOCK
929
942
return list;
930
943
}
931
944
0 commit comments