<div dir="ltr">Gentle ping on this. I would really like to understand the requirements for combining geometries from different GEOS contexts.<div><br></div><div>If my question is unclear, missing information, stupid, answered elsewhere, or there is any other problem with it then please tell me. I would really like to understand this.</div><div><br></div><div>Regards,</div><div>Tom</div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Fri, 22 Nov 2024 at 10:50, Tom Payne <<a href="mailto:twpayne@gmail.com">twpayne@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>tl;dr when calling a function which takes multiple geometries, like <a href="https://libgeos.org/doxygen/geos__c_8h.html#afd3d82a6d039ab5637e0a8a066694b7d" target="_blank">GEOSUnion_r</a>, where the two geometries are associated with different contexts, do I have to ensure that both geometries' contexts are used exclusively?</div><div><br></div><div>Background:</div><div><br></div><div>I maintain the <a href="https://github.com/twpayne/go-geos" target="_blank">Go bindings for GEOS</a>, 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.</div><div><br></div><div>For functions that take multiple geometries I check if the geometries are from different contexts, and if so, lock both mutexes. <a href="https://github.com/twpayne/go-geos/blob/c9ed31526fa2ee3599ffe0fdf4556a6cf9c0b204/geommethods.go#L865-L875" target="_blank">Here</a> is an example:<br><br>    // Union returns the union of g and other.<br>    func (g *Geom) Union(other *Geom) *Geom {<br>        g.mustNotBeDestroyed()<br>        g.context.Lock()<br>        defer g.context.Unlock()<br>        if other.context != g.context {<br>            other.context.Lock()<br>            defer other.context.Unlock()</div><div>        }<br>        return g.context.newGeom(C.GEOSUnion_r(g.context.handle, g.geom, other.geom), nil)<br>    }<br><br></div><div>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.<br><br>I checked the <a href="https://libgeos.org/usage/c_api/" target="_blank">documentation on GEOS's C API</a>, the <a href="https://github.com/libgeos/geos/blob/main/DEVELOPER-NOTES.md" target="_blank">GEOS developer notes</a>, did a superficial <a href="https://github.com/search?q=repo%3Alibgeos%2Fgeos+context&type=issues" target="_blank">search of the GitHub issues</a>, and a superficial <a href="https://www.google.com/search?q=site%3Alists.osgeo.org+%22%5Bgeos-devel%5D%22+context" target="_blank">search of the geos-devel</a> archives, and could not find an answer to this question.<br><br>Many thanks for any insight,</div><div>Tom</div></div>
</blockquote></div>