Skip to content
Permalink
Browse files

[GRASS] mutex function called by browser item (fixes crash in simulta…

…neous loading, e.g. state restore)
  • Loading branch information
blazek committed Apr 1, 2015
1 parent d8bd20e commit da527e7927456751475c8e1c7aece7d2b109cf28
Showing with 17 additions and 0 deletions.
  1. +13 −0 src/providers/grass/qgsgrass.cpp
  2. +4 −0 src/providers/grass/qgsgrass.h
@@ -57,6 +57,9 @@ extern "C"
#define G__setenv(name,value) G__setenv( ( char * ) (name), (char *) (value) )
#endif

#define GRASS_LOCK sMutex.lock();
#define GRASS_UNLOCK sMutex.unlock();

#ifdef Q_OS_WIN
#include <windows.h>
QString GRASS_LIB_EXPORT QgsGrass::shortPath( const QString &path )
@@ -411,13 +414,18 @@ QString QgsGrass::mMapsetLock;
QString QgsGrass::mGisrc;
QString QgsGrass::mTmp;

QMutex QgsGrass::sMutex;

int QgsGrass::error_routine( char *msg, int fatal )
{
return error_routine(( const char* ) msg, fatal );
}

int QgsGrass::error_routine( const char *msg, int fatal )
{
// G_fatal_error obviously is not thread safe (everything static in GRASS, especially fatal_jmp_buf)
// it means that anything which may end up with G_fatal_error must use mutex

// Unfortunately the exceptions thrown here can only be caught if GRASS libraries are compiled
// with -fexception option on Linux (works on Windows)
// GRASS developers are reluctant to add -fexception by default
@@ -802,6 +810,7 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString mapsetPath )
QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
QString location, QString mapset, QString mapName )
{
GRASS_LOCK
QgsDebugMsg( QString( "gisdbase = %1 location = %2 mapset = %3 mapName = %4" ).arg( gisdbase ).arg( location ).arg( mapset ).arg( mapName ) );
QStringList list;

@@ -826,6 +835,7 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Cannot open GRASS vector: %1" ).arg( e.what() ) );
GRASS_UNLOCK
return list;
}

@@ -839,12 +849,14 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
#ifndef Q_OS_WIN
Vect_close( &map );
#endif
GRASS_UNLOCK
return list;
}
else if ( level < 1 )
{
QgsDebugMsg( "Cannot open vector" );
QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot open vector %1 in mapset %2" ).arg( mapName ).arg( mapset ) );
GRASS_UNLOCK
return list;
}

@@ -926,6 +938,7 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,

Vect_close( &map );

GRASS_UNLOCK
return list;
}

@@ -16,6 +16,8 @@
#ifndef QGSGRASS_H
#define QGSGRASS_H

#include <QMutex>

#include <setjmp.h>

// GRASS header files
@@ -308,6 +310,8 @@ class QgsGrass
static QString mGisrc;
// Temporary directory where GISRC and sockets are stored
static QString mTmp;
// Mutex for common locking when calling GRASS functions which are mostly non thread safe
static QMutex sMutex;
};

#endif // QGSGRASS_H

0 comments on commit da527e7

Please sign in to comment.
You can’t perform that action at this time.