Contexts and functions taking multiple geometries
Tom Payne
twpayne at gmail.com
Fri Nov 22 01:50:55 PST 2024
Hi,
tl;dr when calling a function which takes multiple geometries, like
GEOSUnion_r
<https://libgeos.org/doxygen/geos__c_8h.html#afd3d82a6d039ab5637e0a8a066694b7d>,
where
the two geometries are associated with different contexts, do I have to
ensure that both geometries' contexts are used exclusively?
Background:
I maintain the Go bindings for GEOS <https://github.com/twpayne/go-geos>,
which exclusively use the thread-safe *_r functions. Every created geometry
is associated with a context. Every context has a mutex to ensure that it
is only accessed from a single thread at time.
For functions that take multiple geometries I check if the geometries are
from different contexts, and if so, lock both mutexes. Here
<https://github.com/twpayne/go-geos/blob/c9ed31526fa2ee3599ffe0fdf4556a6cf9c0b204/geommethods.go#L865-L875>
is an example:
// Union returns the union of g and other.
func (g *Geom) Union(other *Geom) *Geom {
g.mustNotBeDestroyed()
g.context.Lock()
defer g.context.Unlock()
if other.context != g.context {
other.context.Lock()
defer other.context.Unlock()
}
return g.context.newGeom(C.GEOSUnion_r(g.context.handle, g.geom,
other.geom), nil)
}
However, there is a potential deadlock if there are two geometries A and B
owned by different contexts and A.Union(B) and B.Union(A) are called
simultaneously from different threads. In practice this pattern is unlikely
to occur, but I would like to guard against it.
I checked the documentation on GEOS's C API
<https://libgeos.org/usage/c_api/>, the GEOS developer notes
<https://github.com/libgeos/geos/blob/main/DEVELOPER-NOTES.md>, did a
superficial search of the GitHub issues
<https://github.com/search?q=repo%3Alibgeos%2Fgeos+context&type=issues>,
and a superficial search of the geos-devel
<https://www.google.com/search?q=site%3Alists.osgeo.org+%22%5Bgeos-devel%5D%22+context>
archives, and could not find an answer to this question.
Many thanks for any insight,
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/geos-devel/attachments/20241122/2a3b4171/attachment.htm>
More information about the geos-devel
mailing list