Skip to content

Commit 3cfed69

Browse files
committed
PreparedLineString: Use thread-safe lazy init
1 parent 93f5d06 commit 3cfed69

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

include/geos/geom/prep/PreparedLineString.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ namespace prep { // geos::geom::prep
4040
*/
4141
class PreparedLineString : public BasicPreparedGeometry {
4242
private:
43-
std::unique_ptr<noding::FastSegmentSetIntersectionFinder> segIntFinder;
43+
mutable std::unique_ptr<noding::FastSegmentSetIntersectionFinder> segIntFinder;
4444
mutable noding::SegmentString::ConstVect segStrings;
4545
mutable std::unique_ptr<operation::distance::IndexedFacetDistance> indexedDistance;
4646

47+
mutable std::once_flag segIntFinderFlag;
48+
mutable std::once_flag indexedDistanceFlag;
49+
4750
protected:
4851
public:
4952
PreparedLineString(const Geometry* geom)
@@ -54,7 +57,7 @@ class PreparedLineString : public BasicPreparedGeometry {
5457

5558
~PreparedLineString() override;
5659

57-
noding::FastSegmentSetIntersectionFinder* getIntersectionFinder();
60+
noding::FastSegmentSetIntersectionFinder* getIntersectionFinder() const;
5861

5962
bool intersects(const geom::Geometry* g) const override;
6063
std::unique_ptr<geom::CoordinateSequence> nearestPoints(const geom::Geometry* g) const override;

src/geom/prep/PreparedLineString.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <geos/noding/FastSegmentSetIntersectionFinder.h>
2828
#include <geos/operation/distance/IndexedFacetDistance.h>
2929

30+
#include <mutex>
31+
3032
namespace geos {
3133
namespace geom { // geos.geom
3234
namespace prep { // geos.geom.prep
@@ -44,12 +46,12 @@ PreparedLineString::~PreparedLineString()
4446
}
4547

4648
noding::FastSegmentSetIntersectionFinder*
47-
PreparedLineString::getIntersectionFinder()
49+
PreparedLineString::getIntersectionFinder() const
4850
{
49-
if(! segIntFinder) {
51+
std::call_once(segIntFinderFlag, [this]() {
5052
noding::SegmentStringUtil::extractSegmentStrings(&getGeometry(), segStrings);
51-
segIntFinder.reset(new noding::FastSegmentSetIntersectionFinder(&segStrings));
52-
}
53+
segIntFinder = detail::make_unique<noding::FastSegmentSetIntersectionFinder>(&segStrings);
54+
});
5355

5456
return segIntFinder.get();
5557
}
@@ -68,12 +70,12 @@ PreparedLineString::intersects(const geom::Geometry* g) const
6870

6971
/* public */
7072
operation::distance::IndexedFacetDistance*
71-
PreparedLineString::
72-
getIndexedFacetDistance() const
73+
PreparedLineString::getIndexedFacetDistance() const
7374
{
74-
if(! indexedDistance ) {
75-
indexedDistance.reset(new operation::distance::IndexedFacetDistance(&getGeometry()));
76-
}
75+
std::call_once(indexedDistanceFlag, [this]() {
76+
indexedDistance = detail::make_unique<operation::distance::IndexedFacetDistance>(&getGeometry());
77+
});
78+
7779
return indexedDistance.get();
7880
}
7981

0 commit comments

Comments
 (0)