Skip to content

Commit

Permalink
Add method to force a detach for QgsSpatialIndex
Browse files Browse the repository at this point in the history
Since the underlying libspatialindex is not thread safe
on some platforms (e.g. Windows), manual calls to detach()
must be made if a QgsSpatialIndex is to be accessed across
multiple threads.

Note that for platforms on which libspatialindex is thread
safe, calling detach() has no effect and does not force the
deep copy.
  • Loading branch information
nyalldawson committed Feb 9, 2018
1 parent 98d43ea commit abcaba8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions python/core/qgsspatialindex.sip.in
Expand Up @@ -60,6 +60,20 @@ Copy constructor
~QgsSpatialIndex();


void detach();
%Docstring
Detaches the index, forcing a deep copy of the underlying
spatial index data.

Since the underlying libspatialindex is not thread safe on some platforms (e.g. Windows),
manual calls to detach() must be made if a QgsSpatialIndex is to be accessed across multiple threads.

Note that for platforms on which libspatialindex is thread safe, calling
detach() has no effect and does not force the deep copy.

.. versionadded:: 3.0
%End


bool insertFeature( const QgsFeature &f );
%Docstring
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsspatialindex.cpp
Expand Up @@ -266,6 +266,14 @@ QgsSpatialIndex &QgsSpatialIndex::operator=( const QgsSpatialIndex &other )
return *this;
}

void QgsSpatialIndex::detach()
{
// libspatialindex is not thread safe on windows - so force the deep copy
#if defined(Q_OS_WIN)
d.detach();
#endif
}

SpatialIndex::Region QgsSpatialIndex::rectToRegion( const QgsRectangle &rect )
{
double pt1[2] = { rect.xMinimum(), rect.yMinimum() },
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsspatialindex.h
Expand Up @@ -97,6 +97,20 @@ class CORE_EXPORT QgsSpatialIndex
//! Implement assignment operator
QgsSpatialIndex &operator=( const QgsSpatialIndex &other );

/**
* Detaches the index, forcing a deep copy of the underlying
* spatial index data.
*
* Since the underlying libspatialindex is not thread safe on some platforms (e.g. Windows),
* manual calls to detach() must be made if a QgsSpatialIndex is to be accessed across multiple threads.
*
* Note that for platforms on which libspatialindex is thread safe, calling
* detach() has no effect and does not force the deep copy.
*
* \since QGIS 3.0
*/
void detach();

/* operations */

//! Add feature to index
Expand Down

0 comments on commit abcaba8

Please sign in to comment.